DefaultRepositoryFactory::createRepository()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
/*
4
 * doctrine-manager-builder (https://github.com/juliangut/doctrine-manager-builder).
5
 * Doctrine2 managers builder.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/doctrine-manager-builder
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Jgut\Doctrine\ManagerBuilder\CouchDB\Repository;
13
14
use Jgut\Doctrine\ManagerBuilder\CouchDB\DocumentManager;
15
16
/**
17
 * Default CouchDB document repository factory.
18
 */
19
class DefaultRepositoryFactory implements RepositoryFactory
20
{
21
    /**
22
     * The list of DocumentRepository instances.
23
     *
24
     * @var \Doctrine\Common\Persistence\ObjectRepository[]
25
     */
26
    private $repositoryList = [];
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getRepository(DocumentManager $documentManager, $documentName)
32
    {
33
        $repositoryHash =
34
            $documentManager->getClassMetadata($documentName)->getName() . spl_object_hash($documentManager);
35
36
        if (array_key_exists($repositoryHash, $this->repositoryList)) {
37
            return $this->repositoryList[$repositoryHash];
38
        }
39
40
        $this->repositoryList[$repositoryHash] = $this->createRepository($documentManager, $documentName);
41
42
        return $this->repositoryList[$repositoryHash];
43
    }
44
45
    /**
46
     * Create a new repository instance for a document class.
47
     *
48
     * @param DocumentManager $documentManager
49
     * @param string          $documentName
50
     *
51
     * @return \Doctrine\Common\Persistence\ObjectRepository
52
     */
53
    private function createRepository(DocumentManager $documentManager, $documentName)
54
    {
55
        /* @var $metadata \Doctrine\ODM\CouchDB\Mapping\ClassMetadata */
56
        $metadata = $documentManager->getClassMetadata($documentName);
57
        $repositoryClassName = $metadata->customRepositoryClassName
58
            ?: $documentManager->getDefaultRepositoryClassName();
59
60
        return new $repositoryClassName($documentManager, $metadata);
61
    }
62
}
63