Completed
Pull Request — master (#1427)
by Andreas
06:15
created

DefaultRepositoryFactory::getRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
ccs 6
cts 7
cp 0.8571
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2.0116
1
<?php
2
3
namespace Doctrine\ODM\MongoDB\Repository;
4
5
use Doctrine\ODM\MongoDB\DocumentManager;
6
7
/**
8
 * This factory is used to create default repository objects for entities at runtime.
9
 */
10
class DefaultRepositoryFactory implements RepositoryFactory
11
{
12
    /**
13
     * The list of DocumentRepository instances.
14
     *
15
     * @var array<\Doctrine\Common\Persistence\ObjectRepository>
16
     */
17
    private $repositoryList = array();
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 15
    public function getRepository(DocumentManager $documentManager, $documentName)
23
    {
24 15
        $documentName = ltrim($documentName, '\\');
25
26 15
        if (isset($this->repositoryList[$documentName])) {
27
            return $this->repositoryList[$documentName];
28
        }
29
30 15
        $repository = $this->createRepository($documentManager, $documentName);
31
32 1
        $this->repositoryList[$documentName] = $repository;
33
34 1
        return $repository;
35
    }
36
37
    /**
38
     * Create a new repository instance for a document class.
39
     *
40
     * @param DocumentManager $documentManager The DocumentManager instance.
41
     * @param string          $documentName    The name of the document.
42
     *
43
     * @return \Doctrine\Common\Persistence\ObjectRepository
44
     */
45 15
    protected function createRepository(DocumentManager $documentManager, $documentName)
46
    {
47 15
        $metadata            = $documentManager->getClassMetadata($documentName);
48 1
        $repositoryClassName = $metadata->customRepositoryClassName;
49
50 1
        if ($repositoryClassName === null) {
51
            $configuration       = $documentManager->getConfiguration();
52
            $repositoryClassName = $configuration->getDefaultRepositoryClassName();
53
        }
54
55 1
        return new $repositoryClassName($documentManager, $documentManager->getUnitOfWork(), $metadata);
56
    }
57
}