Skip to content

Commit 62767dd

Browse files
committed
Refactoring
1 parent 39803a0 commit 62767dd

28 files changed

+311
-332
lines changed

src/BulkCompiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Tequila\MongoDB\Exception\InvalidArgumentException;
66
use Tequila\MongoDB\Exception\LogicException;
77
use Tequila\MongoDB\OptionsResolver\BulkWrite\BulkWriteResolver;
8-
use Tequila\MongoDB\OptionsResolver\ResolverFactory;
8+
use Tequila\MongoDB\OptionsResolver\OptionsResolver;
99
use Tequila\MongoDB\Write\Model\WriteModelInterface;
1010

1111
class BulkCompiler implements BulkCompilerInterface
@@ -25,7 +25,7 @@ class BulkCompiler implements BulkCompilerInterface
2525
*/
2626
public function __construct(array $options = [])
2727
{
28-
$this->options = ResolverFactory::get(BulkWriteResolver::class)->resolve($options);
28+
$this->options = OptionsResolver::get(BulkWriteResolver::class)->resolve($options);
2929
}
3030

3131
/**

src/Client.php

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
use MongoDB\Driver\ReadConcern;
66
use MongoDB\Driver\ReadPreference;
77
use MongoDB\Driver\WriteConcern;
8-
use Tequila\MongoDB\OptionsResolver\Command\DropDatabaseResolver;
98
use Tequila\MongoDB\Exception\UnexpectedResultException;
109
use Tequila\MongoDB\OptionsResolver\TypeMapResolver;
11-
use Tequila\MongoDB\Traits\CommandBuilderTrait;
10+
use Tequila\MongoDB\Traits\CommandExecutorTrait;
1211

1312
class Client
1413
{
15-
use CommandBuilderTrait;
14+
use CommandExecutorTrait;
1615

1716
/**
1817
* @var ManagerInterface
@@ -52,20 +51,15 @@ public function __construct(ManagerInterface $manager)
5251
*/
5352
public function dropDatabase($databaseName, array $options = [])
5453
{
55-
$command = $this
56-
->getCommandBuilder()
57-
->createCommand(
54+
$cursor = $this
55+
->getCommandExecutor()
56+
->executeCommand(
57+
$this->manager,
58+
$databaseName,
5859
['dropDatabase' => 1],
59-
$options,
60-
DropDatabaseResolver::class
60+
$options
6161
);
6262

63-
$cursor = $this->manager->executeCommand(
64-
$databaseName,
65-
$command,
66-
$command->getReadPreference()
67-
);
68-
6963
$cursor->setTypeMap(TypeMapResolver::getDefault());
7064

7165
return $cursor->current();
@@ -100,7 +94,7 @@ public function getWriteConcern()
10094
*/
10195
public function listDatabases()
10296
{
103-
$cursor = $this->runCommand(
97+
$cursor = $this->manager->executeCommand(
10498
'admin',
10599
new SimpleCommand(['listDatabases' => 1]),
106100
new ReadPreference(ReadPreference::RP_PRIMARY)
@@ -117,20 +111,6 @@ public function listDatabases()
117111
return $result;
118112
}
119113

120-
/**
121-
* @param string $databaseName
122-
* @param CommandInterface $command
123-
* @param ReadPreference $readPreference
124-
* @return CursorInterface
125-
*/
126-
public function runCommand(
127-
$databaseName,
128-
CommandInterface $command,
129-
ReadPreference $readPreference = null
130-
) {
131-
return $this->manager->executeCommand($databaseName, $command, $readPreference);
132-
}
133-
134114
/**
135115
* @param string $databaseName
136116
* @param string $collectionName

src/Collection.php

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,15 @@
55
use MongoDB\Driver\ReadConcern;
66
use MongoDB\Driver\ReadPreference;
77
use MongoDB\Driver\WriteConcern;
8-
use Tequila\MongoDB\OptionsResolver\Command\AggregateResolver;
9-
use Tequila\MongoDB\OptionsResolver\Command\CountResolver;
10-
use Tequila\MongoDB\OptionsResolver\Command\CreateIndexesResolver;
11-
use Tequila\MongoDB\OptionsResolver\Command\DistinctResolver;
12-
use Tequila\MongoDB\OptionsResolver\Command\DropCollectionResolver;
13-
use Tequila\MongoDB\OptionsResolver\Command\DropIndexesResolver;
14-
use Tequila\MongoDB\OptionsResolver\Command\FindAndModifyResolver;
158
use Tequila\MongoDB\OptionsResolver\Command\FindOneAndDeleteResolver;
169
use Tequila\MongoDB\OptionsResolver\Command\FindOneAndUpdateResolver;
1710
use Tequila\MongoDB\Exception\InvalidArgumentException;
1811
use Tequila\MongoDB\Exception\UnexpectedResultException;
1912
use Tequila\MongoDB\OptionsResolver\BulkWrite\BulkWriteResolver;
2013
use Tequila\MongoDB\OptionsResolver\DatabaseOptionsResolver;
14+
use Tequila\MongoDB\OptionsResolver\OptionsResolver;
2115
use Tequila\MongoDB\OptionsResolver\QueryOptionsResolver;
22-
use Tequila\MongoDB\OptionsResolver\ResolverFactory;
23-
use Tequila\MongoDB\Traits\CommandBuilderTrait;
16+
use Tequila\MongoDB\Traits\CommandExecutorTrait;
2417
use Tequila\MongoDB\Traits\ExecuteCommandTrait;
2518
use Tequila\MongoDB\Write\Model\DeleteMany;
2619
use Tequila\MongoDB\Write\Model\DeleteOne;
@@ -36,7 +29,7 @@
3629

3730
class Collection
3831
{
39-
use CommandBuilderTrait;
32+
use CommandExecutorTrait;
4033
use ExecuteCommandTrait;
4134

4235
/**
@@ -87,7 +80,7 @@ public function __construct(ManagerInterface $manager, $databaseName, $collectio
8780
'writeConcern' => $this->manager->getWriteConcern(),
8881
];
8982

90-
$options = ResolverFactory::get(DatabaseOptionsResolver::class)->resolve($options);
83+
$options = OptionsResolver::get(DatabaseOptionsResolver::class)->resolve($options);
9184
$this->readConcern = $options['readConcern'];
9285
$this->readPreference = $options['readPreference'];
9386
$this->writeConcern = $options['writeConcern'];
@@ -106,8 +99,7 @@ public function aggregate(array $pipeline, array $options = [])
10699

107100
return $this->executeCommand(
108101
['aggregate' => $this->collectionName],
109-
['pipeline' => $pipeline] + $options,
110-
AggregateResolver::class
102+
['pipeline' => $pipeline] + $options
111103
);
112104
}
113105

@@ -150,8 +142,7 @@ public function count(array $filter = [], array $options = [])
150142
{
151143
$cursor = $this->executeCommand(
152144
['count' => $this->collectionName, 'query' => (object)$filter],
153-
$options,
154-
CountResolver::class
145+
$options
155146
);
156147

157148
$result = $cursor->current();
@@ -191,8 +182,7 @@ public function createIndexes(array $indexes, array $options = [])
191182

192183
$this->executeCommand(
193184
['createIndexes' => $this->collectionName, 'indexes' => $compiledIndexes],
194-
$options,
195-
CreateIndexesResolver::class
185+
$options
196186
);
197187

198188
return array_map(function(Index $index) {
@@ -251,8 +241,7 @@ public function distinct($fieldName, array $filter = [], array $options = [])
251241

252242
$cursor = $this->executeCommand(
253243
$command,
254-
$options,
255-
DistinctResolver::class
244+
$options
256245
);
257246

258247
$result = $cursor->current();
@@ -273,8 +262,7 @@ public function drop(array $options = [])
273262
{
274263
$cursor = $this->executeCommand(
275264
['drop' => $this->collectionName],
276-
$options,
277-
DropCollectionResolver::class
265+
$options
278266
);
279267

280268
return $cursor->current();
@@ -290,7 +278,7 @@ public function dropIndexes(array $options = [])
290278
'dropIndexes' => $this->collectionName,
291279
'index' => '*',
292280
];
293-
$cursor = $this->executeCommand($command, $options, DropIndexesResolver::class);
281+
$cursor = $this->executeCommand($command, $options);
294282

295283
return $cursor->current();
296284
}
@@ -307,7 +295,7 @@ public function dropIndex($indexName, array $options = [])
307295
'index' => $indexName,
308296
];
309297

310-
$cursor = $this->executeCommand($command, $options, DropIndexesResolver::class);
298+
$cursor = $this->executeCommand($command, $options);
311299

312300
return $cursor->current();
313301
}
@@ -319,7 +307,7 @@ public function dropIndex($indexName, array $options = [])
319307
*/
320308
public function find(array $filter = [], array $options = [])
321309
{
322-
$options = ResolverFactory::get(QueryOptionsResolver::class)->resolve($options);
310+
$options = OptionsResolver::get(QueryOptionsResolver::class)->resolve($options);
323311

324312
if (isset($options['readPreference'])) {
325313
$readPreference = $options['readPreference'];
@@ -350,9 +338,9 @@ public function findOneAndDelete(array $filter, array $options = [])
350338
'query' => (object)$filter,
351339
];
352340

353-
$options = ['remove' => true] + ResolverFactory::get(FindOneAndDeleteResolver::class)->resolve($options);
341+
$options = ['remove' => true] + OptionsResolver::get(FindOneAndDeleteResolver::class)->resolve($options);
354342

355-
return $this->executeCommand($command, $options, FindAndModifyResolver::class);
343+
return $this->executeCommand($command, $options);
356344
}
357345

358346
/**
@@ -396,10 +384,10 @@ public function findOneAndUpdate(array $filter, $update, array $options = [])
396384
'query' => (object)$filter,
397385
];
398386

399-
$options = ResolverFactory::get(FindOneAndUpdateResolver::class)->resolve($options);
387+
$options = OptionsResolver::get(FindOneAndUpdateResolver::class)->resolve($options);
400388
$options = ['update' => (object)$update] + $options;
401389

402-
return $this->executeCommand($command, $options, FindAndModifyResolver::class);
390+
return $this->executeCommand($command, $options);
403391
}
404392

405393
/**
@@ -526,7 +514,7 @@ public function updateOne($filter, $update, array $options = [])
526514
*/
527515
private static function extractBulkWriteOptions(array $options)
528516
{
529-
$definedOptions = ResolverFactory::get(BulkWriteResolver::class)->getDefinedOptions();
517+
$definedOptions = OptionsResolver::get(BulkWriteResolver::class)->getDefinedOptions();
530518
array_push($definedOptions, 'writeConcern');
531519

532520
$bulkWriteOptions = array_intersect_key($options, array_flip($definedOptions));

src/Command.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Tequila\MongoDB;
44

5-
use MongoDB\Driver\ReadPreference;
65
use Tequila\MongoDB\OptionsResolver\Command\CompatibilityResolverInterface;
76

87
class Command implements CommandInterface
@@ -15,12 +14,7 @@ class Command implements CommandInterface
1514
/**
1615
* @var CompatibilityResolverInterface
1716
*/
18-
private $resolver;
19-
20-
/**
21-
* @var ReadPreference
22-
*/
23-
private $readPreference;
17+
private $compatibilityResolver;
2418

2519
/**
2620
* @param array $options
@@ -35,38 +29,22 @@ public function __construct(array $options)
3529
*/
3630
public function getOptions(Server $server)
3731
{
38-
if (null !== $this->resolver) {
32+
if (null !== $this->compatibilityResolver) {
3933
$options = new CommandOptions($this->options);
4034
$options->setServer($server);
41-
$this->resolver->resolveCompatibilities($options);
35+
$this->compatibilityResolver->resolveCompatibilities($options);
4236

4337
$this->options = $options->toArray();
4438
}
4539

4640
return $this->options;
4741
}
4842

49-
/**
50-
* @return ReadPreference
51-
*/
52-
public function getReadPreference()
53-
{
54-
return $this->readPreference;
55-
}
56-
57-
/**
58-
* @param ReadPreference $readPreference
59-
*/
60-
public function setReadPreference(ReadPreference $readPreference)
61-
{
62-
$this->readPreference = $readPreference;
63-
}
64-
6543
/**
6644
* @param CompatibilityResolverInterface $resolver
6745
*/
6846
public function setCompatibilityResolver(CompatibilityResolverInterface $resolver)
6947
{
70-
$this->resolver = $resolver;
48+
$this->compatibilityResolver = $resolver;
7149
}
7250
}

0 commit comments

Comments
 (0)