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

DefaultRepositoryFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 78.56%

Importance

Changes 0
Metric Value
wmc 4
c 0
b 0
f 0
lcom 1
cbo 3
dl 0
loc 48
ccs 11
cts 14
cp 0.7856
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRepository() 0 14 2
A createRepository() 0 12 2
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
}