Skip to content

Commit bf443ca

Browse files
committed
Meh
1 parent 0a61cd7 commit bf443ca

6 files changed

+158
-3
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "streams/mongodb",
3-
"description": "Add MongoDB support to Streams.",
3+
"description": "Adds MongoDB support to Streams.",
44
"license": "MIT",
55
"prefer-stable": true,
66
"minimum-stability": "dev",

phpunit.xml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
testdox="true"
7+
stopOnFailure="true">
8+
9+
<coverage>
10+
<include>
11+
<directory suffix=".php">./src</directory>
12+
</include>
13+
</coverage>
14+
15+
<testsuites>
16+
<testsuite name="Mongo DB">
17+
<directory suffix="Test.php">tests/</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<php>
22+
<ini name="display_errors" value="true"/>
23+
<env name="APP_ENV" value="testing"/>
24+
</php>
25+
26+
</phpunit>

src/MongoDbAdapter.php

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Streams\MongoDb;
4+
5+
use MongoDB\Client;
6+
use Streams\Core\Stream\Stream;
7+
use Illuminate\Support\Collection;
8+
use Streams\Core\Criteria\Adapter\AbstractAdapter;
9+
10+
class MongoDbAdapter extends AbstractAdapter
11+
{
12+
protected $query;
13+
14+
public function __construct(Stream $stream)
15+
{
16+
$this->stream = $stream;
17+
18+
$collection = $stream->config('source.collection');
19+
20+
$this->client = new Client('mongodb://127.0.0.1:27017');
21+
}
22+
23+
public function orderBy($field, $direction = 'asc'): self
24+
{
25+
$this->query = $this->query->orderBy($field, $direction);
26+
27+
return $this;
28+
}
29+
30+
public function limit($limit, $offset = 0): self
31+
{
32+
$this->query = $this->query->take($limit)->skip($offset);
33+
34+
return $this;
35+
}
36+
37+
public function where($field, $operator = null, $value = null, $nested = null): self
38+
{
39+
if (!$value) {
40+
$value = $operator;
41+
$operator = '=';
42+
}
43+
44+
$method = Str::studly($nested ? $nested . '_where' : 'where');
45+
46+
$this->query = $this->query->{$method}($field, $operator, $value);
47+
48+
return $this;
49+
}
50+
51+
public function withTrashed($toggle): self
52+
{
53+
if ($toggle) {
54+
$this->query = $this->query->withTrashed();
55+
}
56+
57+
return $this;
58+
}
59+
60+
public function with($relations): self
61+
{
62+
$this->query = $this->query->with($relations);
63+
64+
return $this;
65+
}
66+
67+
public function get(array $parameters = []): Collection
68+
{
69+
$this->callParameterMethods($parameters);
70+
71+
return $this->collect($this->query->get());
72+
}
73+
74+
public function count(array $parameters = []): int
75+
{
76+
$this->callParameterMethods($parameters);
77+
78+
return $this->query->count();
79+
}
80+
81+
public function save($entry): bool
82+
{
83+
return $entry->save();
84+
}
85+
86+
public function delete(array $parameters = []): bool
87+
{
88+
$this->callParameterMethods($parameters);
89+
90+
return $this->query->delete();
91+
}
92+
93+
public function truncate(): void
94+
{
95+
$this->query->truncate();
96+
}
97+
}

src/MongoDbServiceProvider.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
namespace Streams\MongoDb;
44

55
use Illuminate\Support\ServiceProvider;
6+
use Streams\Core\Repository\Repository;
67

78
class MongoDbServiceProvider extends ServiceProvider
89
{
9-
10+
public function register()
11+
{
12+
Repository::macro('newMongoAdapter', function() {
13+
return new MongoDbAdapter($this->stream);
14+
});
15+
}
1016
}

tests/MongoDbAdapterTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Streams\MongoDb\Tests;
4+
5+
use Streams\Core\Support\Facades\Streams;
6+
use Streams\MongoDb\MongoDbServiceProvider;
7+
8+
class MongoDbAdapterTest extends MongoDbTestCase
9+
{
10+
public function setUp(): void
11+
{
12+
$this->createApplication();
13+
14+
Streams::overload('films', [
15+
'config' => [
16+
'source' => [
17+
'type' => 'mongo',
18+
],
19+
],
20+
]);
21+
}
22+
23+
public function testCanReturnResults()
24+
{
25+
dd(Streams::make('films')->entries());
26+
}
27+
}

tests/MongoDbTestCase.php

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
abstract class MongoDbTestCase extends TestCase
99
{
10-
1110
protected function getPackageProviders($app)
1211
{
1312
return [MongoDbServiceProvider::class];

0 commit comments

Comments
 (0)