Completed
Push — master ( 1ec641...92e389 )
by Maciej
63:17 queued 60:26
created

DocumentManager::createDBRef()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Doctrine\ODM\MongoDB;
4
5
use Doctrine\Common\EventManager;
6
use Doctrine\Common\Persistence\ObjectManager;
7
use Doctrine\Common\Persistence\ObjectRepository;
8
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
9
use Doctrine\ODM\MongoDB\Mapping\MappingException;
10
use Doctrine\ODM\MongoDB\Hydrator\HydratorFactory;
11
use Doctrine\ODM\MongoDB\Proxy\ProxyFactory;
12
use Doctrine\ODM\MongoDB\Query\FilterCollection;
13
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
14
use MongoDB\Client;
15
use MongoDB\Collection;
16
use MongoDB\Database;
17
use MongoDB\Driver\ReadPreference;
18
19
/**
20
 * The DocumentManager class is the central access point for managing the
21
 * persistence of documents.
22
 *
23
 *     <?php
24
 *
25
 *     $config = new Configuration();
26
 *     $dm = DocumentManager::create(new Connection(), $config);
27
 *
28
 * @since       1.0
29
 */
30
class DocumentManager implements ObjectManager
31
{
32
    /**
33
     * The Doctrine MongoDB connection instance.
34
     *
35
     * @var Client
36
     */
37
    private $client;
38
39
    /**
40
     * The used Configuration.
41
     *
42
     * @var \Doctrine\ODM\MongoDB\Configuration
43
     */
44
    private $config;
45
46
    /**
47
     * The metadata factory, used to retrieve the ODM metadata of document classes.
48
     *
49
     * @var \Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory
50
     */
51
    private $metadataFactory;
52
53
    /**
54
     * The UnitOfWork used to coordinate object-level transactions.
55
     *
56
     * @var UnitOfWork
57
     */
58
    private $unitOfWork;
59
60
    /**
61
     * The event manager that is the central point of the event system.
62
     *
63
     * @var \Doctrine\Common\EventManager
64
     */
65
    private $eventManager;
66
67
    /**
68
     * The Hydrator factory instance.
69
     *
70
     * @var HydratorFactory
71
     */
72
    private $hydratorFactory;
73
74
    /**
75
     * The Proxy factory instance.
76
     *
77
     * @var ProxyFactory
78
     */
79
    private $proxyFactory;
80
81
    /**
82
     * The repository factory used to create dynamic repositories.
83
     *
84
     * @var RepositoryFactory
85
     */
86
    private $repositoryFactory;
87
88
    /**
89
     * SchemaManager instance
90
     *
91
     * @var SchemaManager
92
     */
93
    private $schemaManager;
94
95
    /**
96
     * Array of cached document database instances that are lazily loaded.
97
     *
98
     * @var Database[]
99
     */
100
    private $documentDatabases = array();
101
102
    /**
103
     * Array of cached document collection instances that are lazily loaded.
104
     *
105
     * @var Collection[]
106
     */
107
    private $documentCollections = array();
108
109
    /**
110
     * Whether the DocumentManager is closed or not.
111
     *
112
     * @var bool
113
     */
114
    private $closed = false;
115
116
    /**
117
     * Collection of query filters.
118
     *
119
     * @var \Doctrine\ODM\MongoDB\Query\FilterCollection
120
     */
121
    private $filterCollection;
122
123
    /**
124
     * Creates a new Document that operates on the given Mongo connection
125
     * and uses the given Configuration.
126
     *
127
     * @param Client|null $client
128
     * @param Configuration|null $config
129
     * @param \Doctrine\Common\EventManager|null $eventManager
130
     */
131 1604
    protected function __construct(Client $client = null, Configuration $config = null, EventManager $eventManager = null)
132
    {
133 1604
        $this->config = $config ?: new Configuration();
134 1604
        $this->eventManager = $eventManager ?: new EventManager();
135 1604
        $this->client = $client ?: new Client('mongodb://127.0.0.1', [], ['typeMap' => ['root' => 'array', 'document' => 'array']]);
136
137 1604
        $metadataFactoryClassName = $this->config->getClassMetadataFactoryName();
138 1604
        $this->metadataFactory = new $metadataFactoryClassName();
139 1604
        $this->metadataFactory->setDocumentManager($this);
140 1604
        $this->metadataFactory->setConfiguration($this->config);
141 1604
        if ($cacheDriver = $this->config->getMetadataCacheImpl()) {
142
            $this->metadataFactory->setCacheDriver($cacheDriver);
143
        }
144
145 1604
        $hydratorDir = $this->config->getHydratorDir();
146 1604
        $hydratorNs = $this->config->getHydratorNamespace();
147 1604
        $this->hydratorFactory = new HydratorFactory(
148 1604
            $this,
149 1604
            $this->eventManager,
150 1604
            $hydratorDir,
151 1604
            $hydratorNs,
152 1604
            $this->config->getAutoGenerateHydratorClasses()
0 ignored issues
show
Bug introduced by
It seems like $this->config->getAutoGenerateHydratorClasses() targeting Doctrine\ODM\MongoDB\Con...nerateHydratorClasses() can also be of type boolean; however, Doctrine\ODM\MongoDB\Hyd...rFactory::__construct() does only seem to accept integer, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
153
        );
154
155 1604
        $this->unitOfWork = new UnitOfWork($this, $this->eventManager, $this->hydratorFactory);
156 1604
        $this->hydratorFactory->setUnitOfWork($this->unitOfWork);
157 1604
        $this->schemaManager = new SchemaManager($this, $this->metadataFactory);
158 1604
        $this->proxyFactory = new ProxyFactory($this,
159 1604
            $this->config->getProxyDir(),
160 1604
            $this->config->getProxyNamespace(),
161 1604
            $this->config->getAutoGenerateProxyClasses()
0 ignored issues
show
Bug introduced by
It seems like $this->config->getAutoGenerateProxyClasses() targeting Doctrine\ODM\MongoDB\Con...oGenerateProxyClasses() can also be of type boolean; however, Doctrine\ODM\MongoDB\Pro...yFactory::__construct() does only seem to accept integer, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
162
        );
163 1604
        $this->repositoryFactory = $this->config->getRepositoryFactory();
164 1604
    }
165
166
    /**
167
     * Gets the proxy factory used by the DocumentManager to create document proxies.
168
     *
169
     * @return ProxyFactory
170
     */
171 1
    public function getProxyFactory()
172
    {
173 1
        return $this->proxyFactory;
174
    }
175
176
    /**
177
     * Creates a new Document that operates on the given Mongo connection
178
     * and uses the given Configuration.
179
     *
180
     * @static
181
     * @param Client|null $client
182
     * @param Configuration|null $config
183
     * @param \Doctrine\Common\EventManager|null $eventManager
184
     * @return DocumentManager
185
     */
186 1604
    public static function create(Client $client = null, Configuration $config = null, EventManager $eventManager = null)
187
    {
188 1604
        return new static($client, $config, $eventManager);
189
    }
190
191
    /**
192
     * Gets the EventManager used by the DocumentManager.
193
     *
194
     * @return \Doctrine\Common\EventManager
195
     */
196 1647
    public function getEventManager()
197
    {
198 1647
        return $this->eventManager;
199
    }
200
201
    /**
202
     * Gets the MongoDB client instance that this DocumentManager wraps.
203
     *
204
     * @return Client
205
     */
206 1604
    public function getClient()
207
    {
208 1604
        return $this->client;
209
    }
210
211
    /**
212
     * Gets the metadata factory used to gather the metadata of classes.
213
     *
214
     * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory
215
     */
216 1604
    public function getMetadataFactory()
217
    {
218 1604
        return $this->metadataFactory;
219
    }
220
221
    /**
222
     * Helper method to initialize a lazy loading proxy or persistent collection.
223
     *
224
     * This method is a no-op for other objects.
225
     *
226
     * @param object $obj
227
     */
228
    public function initializeObject($obj)
229
    {
230
        $this->unitOfWork->initializeObject($obj);
231
    }
232
233
    /**
234
     * Gets the UnitOfWork used by the DocumentManager to coordinate operations.
235
     *
236
     * @return UnitOfWork
237
     */
238 1610
    public function getUnitOfWork()
239
    {
240 1610
        return $this->unitOfWork;
241
    }
242
243
    /**
244
     * Gets the Hydrator factory used by the DocumentManager to generate and get hydrators
245
     * for each type of document.
246
     *
247
     * @return HydratorFactory
248
     */
249 66
    public function getHydratorFactory()
250
    {
251 66
        return $this->hydratorFactory;
252
    }
253
254
    /**
255
     * Returns SchemaManager, used to create/drop indexes/collections/databases.
256
     *
257
     * @return \Doctrine\ODM\MongoDB\SchemaManager
258
     */
259 18
    public function getSchemaManager()
260
    {
261 18
        return $this->schemaManager;
262
    }
263
264
    /**
265
     * Returns the metadata for a class.
266
     *
267
     * @param string $className The class name.
268
     * @return \Doctrine\ODM\MongoDB\Mapping\ClassMetadata
269
     * @internal Performance-sensitive method.
270
     */
271 1319
    public function getClassMetadata($className)
272
    {
273 1319
        return $this->metadataFactory->getMetadataFor(ltrim($className, '\\'));
274
    }
275
276
    /**
277
     * Returns the MongoDB instance for a class.
278
     *
279
     * @param string $className The class name.
280
     * @return Database
281
     */
282 1253
    public function getDocumentDatabase($className)
283
    {
284 1253
        $className = ltrim($className, '\\');
285
286 1253
        if (isset($this->documentDatabases[$className])) {
287 35
            return $this->documentDatabases[$className];
288
        }
289
290 1249
        $metadata = $this->metadataFactory->getMetadataFor($className);
291 1249
        $db = $metadata->getDatabase();
292 1249
        $db = $db ?: $this->config->getDefaultDB();
293 1249
        $db = $db ?: 'doctrine';
294 1249
        $this->documentDatabases[$className] = $this->client->selectDatabase($db);
295
296 1249
        return $this->documentDatabases[$className];
297
    }
298
299
    /**
300
     * Gets the array of instantiated document database instances.
301
     *
302
     * @return Database[]
303
     */
304
    public function getDocumentDatabases()
305
    {
306
        return $this->documentDatabases;
307
    }
308
309
    /**
310
     * Returns the MongoCollection instance for a class.
311
     *
312
     * @param string $className The class name.
313
     * @throws MongoDBException When the $className param is not mapped to a collection
314
     * @return Collection
315
     */
316 1255
    public function getDocumentCollection($className)
317
    {
318 1255
        $className = ltrim($className, '\\');
319
320 1255
        $metadata = $this->metadataFactory->getMetadataFor($className);
321 1255
        $collectionName = $metadata->getCollection();
322
323 1255
        if ( ! $collectionName) {
324
            throw MongoDBException::documentNotMappedToCollection($className);
325
        }
326
327 1255
        if ( ! isset($this->documentCollections[$className])) {
328 1245
            $db = $this->getDocumentDatabase($className);
329
330 1245
            $options = [];
331 1245
            if ($metadata->readPreference !== null) {
0 ignored issues
show
Bug introduced by
Accessing readPreference on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
332 3
                $options['readPreference'] = new ReadPreference($metadata->readPreference, $metadata->readPreferenceTags);
0 ignored issues
show
Bug introduced by
Accessing readPreference on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing readPreferenceTags on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
333
            }
334
335 1245
            $this->documentCollections[$className] = $db->selectCollection($collectionName, $options);
336
        }
337
338 1255
        return $this->documentCollections[$className];
339
    }
340
341
    /**
342
     * Gets the array of instantiated document collection instances.
343
     *
344
     * @return Collection[]
345
     */
346
    public function getDocumentCollections()
347
    {
348
        return $this->documentCollections;
349
    }
350
351
    /**
352
     * Create a new Query instance for a class.
353
     *
354
     * @param string $documentName The document class name.
355
     * @return Query\Builder
356
     */
357 178
    public function createQueryBuilder($documentName = null)
358
    {
359 178
        return new Query\Builder($this, $documentName);
360
    }
361
362
    /**
363
     * Creates a new aggregation builder instance for a class.
364
     *
365
     * @param string $documentName The document class name.
366
     * @return Aggregation\Builder
367
     */
368 41
    public function createAggregationBuilder($documentName)
369
    {
370 41
        return new Aggregation\Builder($this, $documentName);
371
    }
372
373
    /**
374
     * Tells the DocumentManager to make an instance managed and persistent.
375
     *
376
     * The document will be entered into the database at or before transaction
377
     * commit or as a result of the flush operation.
378
     *
379
     * NOTE: The persist operation always considers documents that are not yet known to
380
     * this DocumentManager as NEW. Do not pass detached documents to the persist operation.
381
     *
382
     * @param object $document The instance to make managed and persistent.
383
     * @throws \InvalidArgumentException When the given $document param is not an object
384
     */
385 580 View Code Duplication
    public function persist($document)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
386
    {
387 580
        if ( ! is_object($document)) {
388 1
            throw new \InvalidArgumentException(gettype($document));
389
        }
390 579
        $this->errorIfClosed();
391 578
        $this->unitOfWork->persist($document);
392 574
    }
393
394
    /**
395
     * Removes a document instance.
396
     *
397
     * A removed document will be removed from the database at or before transaction commit
398
     * or as a result of the flush operation.
399
     *
400
     * @param object $document The document instance to remove.
401
     * @throws \InvalidArgumentException when the $document param is not an object
402
     */
403 23 View Code Duplication
    public function remove($document)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
404
    {
405 23
        if ( ! is_object($document)) {
406 1
            throw new \InvalidArgumentException(gettype($document));
407
        }
408 22
        $this->errorIfClosed();
409 21
        $this->unitOfWork->remove($document);
410 21
    }
411
412
    /**
413
     * Refreshes the persistent state of a document from the database,
414
     * overriding any local changes that have not yet been persisted.
415
     *
416
     * @param object $document The document to refresh.
417
     * @throws \InvalidArgumentException When the given $document param is not an object
418
     */
419 23 View Code Duplication
    public function refresh($document)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
420
    {
421 23
        if ( ! is_object($document)) {
422 1
            throw new \InvalidArgumentException(gettype($document));
423
        }
424 22
        $this->errorIfClosed();
425 21
        $this->unitOfWork->refresh($document);
426 20
    }
427
428
    /**
429
     * Detaches a document from the DocumentManager, causing a managed document to
430
     * become detached.  Unflushed changes made to the document if any
431
     * (including removal of the document), will not be synchronized to the database.
432
     * Documents which previously referenced the detached document will continue to
433
     * reference it.
434
     *
435
     * @param object $document The document to detach.
436
     * @throws \InvalidArgumentException when the $document param is not an object
437
     */
438 11
    public function detach($document)
439
    {
440 11
        if ( ! is_object($document)) {
441 1
            throw new \InvalidArgumentException(gettype($document));
442
        }
443 10
        $this->unitOfWork->detach($document);
444 10
    }
445
446
    /**
447
     * Merges the state of a detached document into the persistence context
448
     * of this DocumentManager and returns the managed copy of the document.
449
     * The document passed to merge will not become associated/managed with this DocumentManager.
450
     *
451
     * @param object $document The detached document to merge into the persistence context.
452
     * @throws LockException
453
     * @throws \InvalidArgumentException if the $document param is not an object
454
     * @return object The managed copy of the document.
455
     */
456 14 View Code Duplication
    public function merge($document)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
457
    {
458 14
        if ( ! is_object($document)) {
459 1
            throw new \InvalidArgumentException(gettype($document));
460
        }
461 13
        $this->errorIfClosed();
462 12
        return $this->unitOfWork->merge($document);
463
    }
464
465
    /**
466
     * Acquire a lock on the given document.
467
     *
468
     * @param object $document
469
     * @param int $lockMode
470
     * @param int $lockVersion
471
     * @throws \InvalidArgumentException
472
     */
473 8
    public function lock($document, $lockMode, $lockVersion = null)
474
    {
475 8
        if ( ! is_object($document)) {
476
            throw new \InvalidArgumentException(gettype($document));
477
        }
478 8
        $this->unitOfWork->lock($document, $lockMode, $lockVersion);
479 5
    }
480
481
    /**
482
     * Releases a lock on the given document.
483
     *
484
     * @param object $document
485
     * @throws \InvalidArgumentException if the $document param is not an object
486
     */
487 1
    public function unlock($document)
488
    {
489 1
        if ( ! is_object($document)) {
490
            throw new \InvalidArgumentException(gettype($document));
491
        }
492 1
        $this->unitOfWork->unlock($document);
493 1
    }
494
495
    /**
496
     * Gets the repository for a document class.
497
     *
498
     * @param string $documentName  The name of the Document.
499
     * @return ObjectRepository  The repository.
500
     */
501 326
    public function getRepository($documentName)
502
    {
503 326
        return $this->repositoryFactory->getRepository($this, $documentName);
504
    }
505
506
    /**
507
     * Flushes all changes to objects that have been queued up to now to the database.
508
     * This effectively synchronizes the in-memory state of managed objects with the
509
     * database.
510
     *
511
     * @param array $options Array of options to be used with batchInsert(), update() and remove()
512
     * @throws \InvalidArgumentException
513
     */
514 551
    public function flush(array $options = array())
515
    {
516 551
        $this->errorIfClosed();
517 550
        $this->unitOfWork->commit($options);
518 547
    }
519
520
    /**
521
     * Gets a reference to the document identified by the given type and identifier
522
     * without actually loading it.
523
     *
524
     * If partial objects are allowed, this method will return a partial object that only
525
     * has its identifier populated. Otherwise a proxy is returned that automatically
526
     * loads itself on first access.
527
     *
528
     * @param string $documentName
529
     * @param string|object $identifier
530
     * @return mixed|object The document reference.
531
     */
532 125
    public function getReference($documentName, $identifier)
533
    {
534
        /* @var $class \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo */
535 125
        $class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
536
537
        // Check identity map first, if its already in there just return it.
538 125
        if ($document = $this->unitOfWork->tryGetById($identifier, $class)) {
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\ODM\Mong...ping\ClassMetadataInfo> is not a sub-type of object<Doctrine\ODM\Mong...\Mapping\ClassMetadata>. It seems like you assume a child class of the class Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
539 55
            return $document;
540
        }
541
542 96
        $document = $this->proxyFactory->getProxy($class->name, array($class->identifier => $identifier));
543 96
        $this->unitOfWork->registerManaged($document, $identifier, array());
0 ignored issues
show
Documentation introduced by
$identifier is of type string|object, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
544
545 96
        return $document;
546
    }
547
548
    /**
549
     * Gets a partial reference to the document identified by the given type and identifier
550
     * without actually loading it, if the document is not yet loaded.
551
     *
552
     * The returned reference may be a partial object if the document is not yet loaded/managed.
553
     * If it is a partial object it will not initialize the rest of the document state on access.
554
     * Thus you can only ever safely access the identifier of a document obtained through
555
     * this method.
556
     *
557
     * The use-cases for partial references involve maintaining bidirectional associations
558
     * without loading one side of the association or to update a document without loading it.
559
     * Note, however, that in the latter case the original (persistent) document data will
560
     * never be visible to the application (especially not event listeners) as it will
561
     * never be loaded in the first place.
562
     *
563
     * @param string $documentName The name of the document type.
564
     * @param mixed $identifier The document identifier.
565
     * @return object The (partial) document reference.
566
     */
567 1
    public function getPartialReference($documentName, $identifier)
568
    {
569 1
        $class = $this->metadataFactory->getMetadataFor(ltrim($documentName, '\\'));
570
571
        // Check identity map first, if its already in there just return it.
572 1
        if ($document = $this->unitOfWork->tryGetById($identifier, $class)) {
0 ignored issues
show
Compatibility introduced by
$class of type object<Doctrine\Common\P...\Mapping\ClassMetadata> is not a sub-type of object<Doctrine\ODM\Mong...\Mapping\ClassMetadata>. It seems like you assume a concrete implementation of the interface Doctrine\Common\Persistence\Mapping\ClassMetadata to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
573
            return $document;
574
        }
575 1
        $document = $class->newInstance();
576 1
        $class->setIdentifierValue($document, $identifier);
0 ignored issues
show
Bug introduced by
The method setIdentifierValue() does not exist on Doctrine\Common\Persistence\Mapping\ClassMetadata. Did you maybe mean getIdentifierValues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
577 1
        $this->unitOfWork->registerManaged($document, $identifier, array());
578
579 1
        return $document;
580
    }
581
582
    /**
583
     * Finds a Document by its identifier.
584
     *
585
     * This is just a convenient shortcut for getRepository($documentName)->find($id).
586
     *
587
     * @param string $documentName
588
     * @param mixed $identifier
589
     * @param int $lockMode
590
     * @param int $lockVersion
591
     * @return object $document
592
     */
593 181
    public function find($documentName, $identifier, $lockMode = LockMode::NONE, $lockVersion = null)
594
    {
595 181
        return $this->getRepository($documentName)->find($identifier, $lockMode, $lockVersion);
0 ignored issues
show
Unused Code introduced by
The call to ObjectRepository::find() has too many arguments starting with $lockMode.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
596
    }
597
598
    /**
599
     * Clears the DocumentManager.
600
     *
601
     * All documents that are currently managed by this DocumentManager become
602
     * detached.
603
     *
604
     * @param string|null $documentName if given, only documents of this type will get detached
605
     */
606 371
    public function clear($documentName = null)
607
    {
608 371
        $this->unitOfWork->clear($documentName);
609 371
    }
610
611
    /**
612
     * Closes the DocumentManager. All documents that are currently managed
613
     * by this DocumentManager become detached. The DocumentManager may no longer
614
     * be used after it is closed.
615
     */
616 6
    public function close()
617
    {
618 6
        $this->clear();
619 6
        $this->closed = true;
620 6
    }
621
622
    /**
623
     * Determines whether a document instance is managed in this DocumentManager.
624
     *
625
     * @param object $document
626
     * @throws \InvalidArgumentException When the $document param is not an object
627
     * @return boolean TRUE if this DocumentManager currently manages the given document, FALSE otherwise.
628
     */
629 3
    public function contains($document)
630
    {
631 3
        if ( ! is_object($document)) {
632
            throw new \InvalidArgumentException(gettype($document));
633
        }
634 3
        return $this->unitOfWork->isScheduledForInsert($document) ||
635 3
            $this->unitOfWork->isInIdentityMap($document) &&
636 3
            ! $this->unitOfWork->isScheduledForDelete($document);
637
    }
638
639
    /**
640
     * Gets the Configuration used by the DocumentManager.
641
     *
642
     * @return Configuration
643
     */
644 718
    public function getConfiguration()
645
    {
646 718
        return $this->config;
647
    }
648
649
    /**
650
     * Returns a reference to the supplied document.
651
     *
652
     * @param object $document A document object
653
     * @param array $referenceMapping Mapping for the field that references the document
654
     *
655
     * @throws \InvalidArgumentException
656
     * @throws MappingException
657
     * @return mixed The reference for the document in question, according to the desired mapping
658
     */
659 220
    public function createReference($document, array $referenceMapping)
660
    {
661 220
        if ( ! is_object($document)) {
662
            throw new \InvalidArgumentException('Cannot create a DBRef, the document is not an object');
663
        }
664
665 220
        $class = $this->getClassMetadata(get_class($document));
666 220
        $id = $this->unitOfWork->getDocumentIdentifier($document);
667
668 220
        if ($id === null) {
669 1
            throw new \RuntimeException(
670 1
                sprintf('Cannot create a DBRef for class %s without an identifier. Have you forgotten to persist/merge the document first?', $class->name)
671
            );
672
        }
673
674 219
        $storeAs = $referenceMapping['storeAs'] ?? null;
675 219
        switch ($storeAs) {
676
            case ClassMetadataInfo::REFERENCE_STORE_AS_ID:
677 45
                if ($class->inheritanceType === ClassMetadataInfo::INHERITANCE_TYPE_SINGLE_COLLECTION) {
678 1
                    throw MappingException::simpleReferenceMustNotTargetDiscriminatedDocument($referenceMapping['targetDocument']);
679
                }
680
681 44
                return $class->getDatabaseIdentifierValue($id);
682
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
683
684
685
            case ClassMetadataInfo::REFERENCE_STORE_AS_REF:
686 19
                $reference = ['id' => $class->getDatabaseIdentifierValue($id)];
687 19
                break;
688
689
            case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF:
690
                $reference = [
691 176
                    '$ref' => $class->getCollection(),
692 176
                    '$id'  => $class->getDatabaseIdentifierValue($id),
693
                ];
694 176
                break;
695
696
            case ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB:
697
                $reference = [
698 17
                    '$ref' => $class->getCollection(),
699 17
                    '$id'  => $class->getDatabaseIdentifierValue($id),
700 17
                    '$db'  => $this->getDocumentDatabase($class->name)->getDatabaseName(),
701
                ];
702 17
                break;
703
704
            default:
705
                throw new \InvalidArgumentException("Reference type {$storeAs} is invalid.");
706
        }
707
708
        /* If the class has a discriminator (field and value), use it. A child
709
         * class that is not defined in the discriminator map may only have a
710
         * discriminator field and no value, so default to the full class name.
711
         */
712 197
        if (isset($class->discriminatorField)) {
713 18
            $reference[$class->discriminatorField] = $class->discriminatorValue ?? $class->name;
714
        }
715
716
        /* Add a discriminator value if the referenced document is not mapped
717
         * explicitly to a targetDocument class.
718
         */
719 197 View Code Duplication
        if (! isset($referenceMapping['targetDocument'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
720 33
            $discriminatorField = $referenceMapping['discriminatorField'];
721 33
            $discriminatorValue = isset($referenceMapping['discriminatorMap'])
722 8
                ? array_search($class->name, $referenceMapping['discriminatorMap'])
723 33
                : $class->name;
724
725
            /* If the discriminator value was not found in the map, use the full
726
             * class name. In the future, it may be preferable to throw an
727
             * exception here (perhaps based on some strictness option).
728
             *
729
             * @see PersistenceBuilder::prepareEmbeddedDocumentValue()
730
             */
731 33
            if ($discriminatorValue === false) {
732 2
                $discriminatorValue = $class->name;
733
            }
734
735 33
            $reference[$discriminatorField] = $discriminatorValue;
736
        }
737
738 197
        return $reference;
739
    }
740
741
    /**
742
     * Throws an exception if the DocumentManager is closed or currently not active.
743
     *
744
     * @throws MongoDBException If the DocumentManager is closed.
745
     */
746 584
    private function errorIfClosed()
747
    {
748 584
        if ($this->closed) {
749 5
            throw MongoDBException::documentManagerClosed();
750
        }
751 579
    }
752
753
    /**
754
     * Check if the Document manager is open or closed.
755
     *
756
     * @return bool
757
     */
758 1
    public function isOpen()
759
    {
760 1
        return ( ! $this->closed);
761
    }
762
763
    /**
764
     * Gets the filter collection.
765
     *
766
     * @return \Doctrine\ODM\MongoDB\Query\FilterCollection The active filter collection.
767
     */
768 503
    public function getFilterCollection()
769
    {
770 503
        if (null === $this->filterCollection) {
771 503
            $this->filterCollection = new FilterCollection($this);
772
        }
773
774 503
        return $this->filterCollection;
775
    }
776
}
777