Completed
Push — master ( 7b9f4b...1cd743 )
by Andreas
13s queued 10s
created

MongoDB/Repository/AbstractRepositoryFactory.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB\Repository;
6
7
use Doctrine\Common\Persistence\ObjectRepository;
8
use Doctrine\ODM\MongoDB\DocumentManager;
9
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
10
use function ltrim;
11
use function spl_object_hash;
12
13
/**
14
 * Abstract factory for creating document repositories.
15
 */
16
abstract class AbstractRepositoryFactory implements RepositoryFactory
17
{
18
    /**
19
     * The list of DocumentRepository instances.
20
     *
21
     * @var ObjectRepository[]
22
     */
23
    private $repositoryList = [];
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 350
    public function getRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository
29
    {
30 350
        $metadata = $documentManager->getClassMetadata($documentName);
31 350
        $hashKey  = $metadata->getName() . spl_object_hash($documentManager);
0 ignored issues
show
Consider using $metadata->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
32
33 350
        if (isset($this->repositoryList[$hashKey])) {
34 167
            return $this->repositoryList[$hashKey];
35
        }
36
37 350
        $repository = $this->createRepository($documentManager, ltrim($documentName, '\\'));
38
39 350
        $this->repositoryList[$hashKey] = $repository;
40
41 350
        return $repository;
42
    }
43
44
    /**
45
     * Create a new repository instance for a document class.
46
     *
47
     * @return ObjectRepository|GridFSRepository
48
     */
49 350
    protected function createRepository(DocumentManager $documentManager, string $documentName) : ObjectRepository
50
    {
51 350
        $metadata = $documentManager->getClassMetadata($documentName);
52
53 350
        if ($metadata->customRepositoryClassName) {
54 13
            $repositoryClassName = $metadata->customRepositoryClassName;
55 342
        } elseif ($metadata->isFile) {
56 10
            $repositoryClassName = $documentManager->getConfiguration()->getDefaultGridFSRepositoryClassName();
57
        } else {
58 333
            $repositoryClassName = $documentManager->getConfiguration()->getDefaultDocumentRepositoryClassName();
59
        }
60
61 350
        return $this->instantiateRepository($repositoryClassName, $documentManager, $metadata);
62
    }
63
64
    /**
65
     * Instantiates requested repository.
66
     */
67
    abstract protected function instantiateRepository(string $repositoryClassName, DocumentManager $documentManager, ClassMetadata $metadata) : ObjectRepository;
68
}
69