Completed
Pull Request — master (#1376)
by Bilge
30:49 queued 28:17
created

DefaultRepositoryFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 93.33%
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 48
ccs 14
cts 15
cp 0.9333
rs 10

2 Methods

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