Completed
Pull Request — master (#1376)
by Bilge
30:49 queued 28:17
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
Metric Value
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 2
    public function getRepository(DocumentManager $documentManager, $documentName)
23
    {
24 2
        $documentName = ltrim($documentName, '\\');
25
26 2
        if (isset($this->repositoryList[$documentName])) {
27
            return $this->repositoryList[$documentName];
28
        }
29
30 2
        $repository = $this->createRepository($documentManager, $documentName);
31
32 2
        $this->repositoryList[$documentName] = $repository;
33
34 2
        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 2
    protected function createRepository(DocumentManager $documentManager, $documentName)
46
    {
47 2
        $metadata            = $documentManager->getClassMetadata($documentName);
48 2
        $repositoryClassName = $metadata->customRepositoryClassName;
49
50 2
        if ($repositoryClassName === null) {
51 2
            $configuration       = $documentManager->getConfiguration();
52 2
            $repositoryClassName = $configuration->getDefaultRepositoryClassName();
53 2
        }
54
55 2
        return new $repositoryClassName($documentManager, $documentManager->getUnitOfWork(), $metadata);
56
    }
57
}