Skip to content

Commit 49bae27

Browse files
committed
StringUtils deleted, refactoring, Find operation
1 parent a9a67c8 commit 49bae27

22 files changed

+114
-261
lines changed

src/Command/CommandBuilder.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
use Tequila\MongoDB\Connection;
66

7-
/**
8-
* Class CommandBuilder
9-
* @package Tequila\MongoDB\Command
10-
*/
117
class CommandBuilder
128
{
139
/**

src/Command/CommandTypeInterface.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
use MongoDB\Driver\ReadPreference;
66
use Tequila\MongoDB\Options\ConfigurableInterface;
77

8-
/**
9-
* Interface CommandInterface
10-
* @package Tequila\MongoDB\Command
11-
*/
128
interface CommandTypeInterface extends ConfigurableInterface
139
{
1410
/**

src/Command/CommandWrapper.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
use Tequila\MongoDB\Connection;
99
use Tequila\MongoDB\Exception\InvalidArgumentException;
1010
use Symfony\Component\OptionsResolver\Exception\InvalidArgumentException as OptionsResolverException;
11-
use Tequila\MongoDB\Util\TypeUtils;
1211

13-
/**
14-
* Class Command
15-
* @package Tequila\MongoDB\Command
16-
*/
1712
class CommandWrapper
1813
{
1914
/**
@@ -46,16 +41,22 @@ class CommandWrapper
4641
* @param string $databaseName
4742
* @param string $commandClass
4843
*/
49-
public function __construct(
50-
Connection $connection,
51-
$databaseName,
52-
$commandClass
53-
) {
54-
TypeUtils::ensureIsSubclassOf($commandClass, CommandTypeInterface::class);
55-
56-
$this->databaseName = (string) $databaseName;
44+
public function __construct(Connection $connection, $databaseName, $commandClass)
45+
{
46+
$commandClass = (string)$commandClass;
47+
if (!is_subclass_of($commandClass, CommandTypeInterface::class)) {
48+
throw new InvalidArgumentException(
49+
sprintf(
50+
'$commandClass must be a name of class, which implements "%s", %s does not',
51+
CommandTypeInterface::class,
52+
$commandClass
53+
)
54+
);
55+
}
56+
57+
$this->databaseName = (string)$databaseName;
5758
$this->connection = $connection;
58-
$this->commandClass = (string) $commandClass;
59+
$this->commandClass = (string)$commandClass;
5960
$this->readPreference = call_user_func([$commandClass, 'getDefaultReadPreference']);
6061
}
6162

@@ -108,7 +109,7 @@ private function resolveCommandOptions(array $options)
108109

109110
try {
110111
$options = $resolver->resolve($options);
111-
} catch(OptionsResolverException $e) {
112+
} catch (OptionsResolverException $e) {
112113
throw new InvalidArgumentException($e->getMessage());
113114
}
114115

src/Command/Type/DropCollectionType.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
use Symfony\Component\OptionsResolver\OptionsResolver;
66
use Tequila\MongoDB\Command\CommandTypeInterface;
77

8-
/**
9-
* Class DropCollectionType
10-
* @package Tequila\MongoDB\Command\Type
11-
*/
128
class DropCollectionType implements CommandTypeInterface
139
{
1410
use PrimaryReadPreferenceTrait;

src/Command/Type/DropDatabaseType.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
use Symfony\Component\OptionsResolver\OptionsResolver;
66
use Tequila\MongoDB\Command\CommandTypeInterface;
77

8-
/**
9-
* Class DropDatabaseType
10-
* @package Tequila\MongoDB\Command\Type
11-
*/
128
class DropDatabaseType implements CommandTypeInterface
139
{
1410
use PrimaryReadPreferenceTrait;

src/Command/Type/ListCollectionsType.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
use Symfony\Component\OptionsResolver\OptionsResolver;
77
use Tequila\MongoDB\Command\CommandTypeInterface;
88

9-
/**
10-
* Class ListCollectionsType
11-
* @package Tequila\MongoDB\Command\Type
12-
*/
139
class ListCollectionsType implements CommandTypeInterface
1410
{
1511
use PrimaryReadPreferenceTrait;

src/Command/Type/PrimaryReadPreferenceTrait.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
use MongoDB\Driver\ReadPreference;
66

7-
/**
8-
* Trait PrimaryReadPreferenceTrait
9-
* @package Tequila\MongoDB\Command\Type
10-
*/
117
trait PrimaryReadPreferenceTrait
128
{
139
/**

src/Connection.php

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,9 @@
2222
use Tequila\MongoDB\Exception\UnexpectedResultException;
2323
use Tequila\MongoDB\Options\Connection\ConnectionOptions;
2424
use Tequila\MongoDB\Options\Driver\DriverOptions;
25-
use Tequila\MongoDB\Util\StringUtils;
26-
use Tequila\MongoDB\Util\TypeUtils;
25+
use Tequila\MongoDB\Util\TypeUtil;
2726
use Tequila\MongoDB\Traits\ReadPreferenceAndConcernsTrait;
2827

29-
/**
30-
* Class Client
31-
* @package Tequila\MongoDB
32-
*/
3328
class Connection
3429
{
3530
use ReadPreferenceAndConcernsTrait;
@@ -80,7 +75,7 @@ public function __construct(
8075
*/
8176
public function executeBulkWrite($databaseName, $collectionName, BulkWrite $bulk, WriteConcern $writeConcern = null)
8277
{
83-
$namespace = StringUtils::createNamespace($databaseName, $collectionName);
78+
$namespace = $databaseName . '.' . $collectionName;
8479

8580
return $this->manager->executeBulkWrite($namespace, $bulk, $writeConcern);
8681
}
@@ -93,13 +88,11 @@ public function executeBulkWrite($databaseName, $collectionName, BulkWrite $bulk
9388
*/
9489
public function executeCommand($databaseName, $commandOptions, ReadPreference $readPreference = null)
9590
{
96-
StringUtils::ensureValidDatabaseName($databaseName);
97-
9891
if (!is_array($commandOptions) && !is_object($commandOptions)) {
9992
throw new InvalidArgumentException(
10093
sprintf(
10194
'$commandOptions must be an array or an object, %s given',
102-
TypeUtils::getType($commandOptions)
95+
TypeUtil::getType($commandOptions)
10396
)
10497
);
10598
}
@@ -117,7 +110,7 @@ public function executeCommand($databaseName, $commandOptions, ReadPreference $r
117110
$commandOptions = (array) $commandOptions;
118111

119112
if (empty($commandOptions)) {
120-
throw new InvalidArgumentException('$command must not be empty.');
113+
throw new InvalidArgumentException('$commandOptions must not be empty.');
121114
}
122115

123116
if (!$readPreference) {
@@ -142,7 +135,7 @@ public function executeCommand($databaseName, $commandOptions, ReadPreference $r
142135
*/
143136
public function executeQuery($databaseName, $collectionName, Query $query, ReadPreference $readPreference = null)
144137
{
145-
$namespace = StringUtils::createNamespace($databaseName, $collectionName);
138+
$namespace = $databaseName . '.' . $collectionName;
146139

147140
return $this->manager->executeQuery($namespace, $query, $readPreference);
148141
}
@@ -155,9 +148,6 @@ public function executeQuery($databaseName, $collectionName, Query $query, ReadP
155148
*/
156149
public function createCollection($databaseName, $collectionName, array $options = [])
157150
{
158-
StringUtils::ensureValidDatabaseName($databaseName);
159-
StringUtils::ensureValidCollectionName($collectionName);
160-
161151
$options[CreateCollectionType::getCommandName()] = $collectionName;
162152

163153
return $this
@@ -172,10 +162,7 @@ public function createCollection($databaseName, $collectionName, array $options
172162
*/
173163
public function dropCollection($databaseName, $collectionName)
174164
{
175-
StringUtils::ensureValidDatabaseName($databaseName);
176-
StringUtils::ensureValidCollectionName($collectionName);
177-
178-
$options[DropCollectionType::getCommandName()] = (string)$collectionName;
165+
$options[DropCollectionType::getCommandName()] = $collectionName;
179166

180167
return $this
181168
->buildAndExecuteCommand($databaseName, DropCollectionType::class, $options)
@@ -202,9 +189,6 @@ public function listCollections($databaseName, array $options = [])
202189
*/
203190
public function createIndexes($databaseName, $collectionName, array $indexes)
204191
{
205-
StringUtils::ensureValidDatabaseName($databaseName);
206-
StringUtils::ensureValidCollectionName($collectionName);
207-
208192
if (empty($indexes)) {
209193
throw new InvalidArgumentException('$indexes array cannot be empty');
210194
}
@@ -217,7 +201,7 @@ public function createIndexes($databaseName, $collectionName, array $indexes)
217201
sprintf(
218202
'$indexes[%d] must be an Index instance, %s given',
219203
$i,
220-
TypeUtils::getType($index)
204+
TypeUtil::getType($index)
221205
)
222206
);
223207
}
@@ -243,9 +227,6 @@ function (Index $index) {
243227
*/
244228
public function createIndex($databaseName, $collectionName, Index $index)
245229
{
246-
StringUtils::ensureValidDatabaseName($databaseName);
247-
StringUtils::ensureValidCollectionName($collectionName);
248-
249230
$result = $this->createIndexes($databaseName, $collectionName, [$index]);
250231

251232
return current($result);
@@ -258,9 +239,6 @@ public function createIndex($databaseName, $collectionName, Index $index)
258239
*/
259240
public function listIndexes($databaseName, $collectionName)
260241
{
261-
StringUtils::ensureValidDatabaseName($databaseName);
262-
StringUtils::ensureValidCollectionName($collectionName);
263-
264242
return $this
265243
->buildAndExecuteCommand(
266244
$databaseName,
@@ -280,9 +258,6 @@ public function listIndexes($databaseName, $collectionName)
280258
*/
281259
public function dropIndex($databaseName, $collectionName, $index)
282260
{
283-
StringUtils::ensureValidDatabaseName($databaseName);
284-
StringUtils::ensureValidCollectionName($collectionName);
285-
286261
if (is_string($index)) {
287262
$indexName = $index;
288263
} else if ($index instanceof Index) {
@@ -292,8 +267,8 @@ public function dropIndex($databaseName, $collectionName, $index)
292267
} else {
293268
throw new InvalidArgumentException(
294269
sprintf(
295-
'$index must be a string, Index instance or array of index keys, %s given',
296-
TypeUtils::getType($index)
270+
'$index must be a string, Index instance or an array of index keys, %s given',
271+
TypeUtil::getType($index)
297272
)
298273
);
299274
}
@@ -316,9 +291,6 @@ public function dropIndex($databaseName, $collectionName, $index)
316291
*/
317292
public function dropIndexes($databaseName, $collectionName)
318293
{
319-
StringUtils::ensureValidDatabaseName($databaseName);
320-
StringUtils::ensureValidCollectionName($collectionName);
321-
322294
$cursor = $this->buildAndExecuteCommand(
323295
$databaseName,
324296
DropIndexesType::class,
@@ -335,8 +307,6 @@ public function dropIndexes($databaseName, $collectionName)
335307
*/
336308
public function dropDatabase($databaseName, array $options = [])
337309
{
338-
StringUtils::ensureValidDatabaseName($databaseName);
339-
340310
$cursor = $this->buildAndExecuteCommand($databaseName, DropDatabaseType::class, $options);
341311

342312
return $cursor->toArray();
@@ -369,8 +339,6 @@ public function listDatabases(array $options = [])
369339
*/
370340
public function selectDatabase($databaseName)
371341
{
372-
StringUtils::ensureValidDatabaseName($databaseName);
373-
374342
$db = new Database($this, $databaseName);
375343
$db
376344
->setReadConcern($this->getReadConcern())
@@ -387,9 +355,6 @@ public function selectDatabase($databaseName)
387355
*/
388356
public function selectCollection($databaseName, $collectionName)
389357
{
390-
StringUtils::ensureValidDatabaseName($databaseName);
391-
StringUtils::ensureValidCollectionName($collectionName);
392-
393358
return $this->selectDatabase($databaseName)->selectCollection($collectionName);
394359
}
395360

@@ -399,8 +364,6 @@ public function selectCollection($databaseName, $collectionName)
399364
*/
400365
public function createCommandBuilder($databaseName)
401366
{
402-
StringUtils::ensureValidDatabaseName($databaseName);
403-
404367
if (!isset($this->commandBuilders[$databaseName])) {
405368
$this->commandBuilders[$databaseName] = new CommandBuilder($this, $databaseName);
406369
}

src/Database.php

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

33
namespace Tequila\MongoDB;
44

5-
/**
6-
* Class Database
7-
* @package Tequila\MongoDB
8-
*/
95
class Database implements DatabaseInterface
106
{
117
use Traits\ReadPreferenceAndConcernsTrait;

src/Index.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
use Tequila\MongoDB\Exception\InvalidArgumentException;
77
use Tequila\MongoDB\Options\Indexes\IndexOptions;
88

9-
/**
10-
* Class Index
11-
* @package Tequila\MongoDB
12-
*/
139
class Index
1410
{
1511
/**

src/Operation/Find.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tequila\MongoDB\Operation;
4+
5+
use MongoDB\Driver\Query;
6+
use MongoDB\Driver\ReadPreference;
7+
use Tequila\MongoDB\Connection;
8+
use Tequila\MongoDB\Exception\InvalidArgumentException;
9+
use Tequila\MongoDB\Options\Read\FindOptions;
10+
use Tequila\MongoDB\Util\TypeUtil;
11+
12+
class Find implements OperationInterface
13+
{
14+
/**
15+
* @var array|object
16+
*/
17+
private $filter;
18+
19+
/**
20+
* @var array
21+
*/
22+
private $options;
23+
24+
/**
25+
* @var ReadPreference
26+
*/
27+
private $readPreference;
28+
29+
/**
30+
* @param array|object $filter
31+
* @param array $options
32+
*/
33+
public function __construct($filter, array $options)
34+
{
35+
if (!is_array($filter) && !is_object($filter)) {
36+
throw new InvalidArgumentException(
37+
sprintf(
38+
'$filter must be an array or an object, %s given',
39+
TypeUtil::getType($filter)
40+
)
41+
);
42+
}
43+
44+
$options = FindOptions::resolve($options);
45+
if (isset($options['readPreference'])) {
46+
$this->readPreference = $options['readPreference'];
47+
unset($options['readPreference']);
48+
}
49+
50+
$this->filter = $filter;
51+
$this->options = $options;
52+
}
53+
54+
public function execute(Connection $connection, $databaseName, $collectionName)
55+
{
56+
$query = new Query($this->filter, $this->options);
57+
58+
return $connection->executeQuery(
59+
$databaseName,
60+
$collectionName,
61+
$query,
62+
$this->readPreference
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)