Completed
Pull Request — master (#1600)
by Maciej
09:15
created

AbstractRepositoryFactory::getRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Repository;
21
22
use Doctrine\ODM\MongoDB\DocumentManager;
23
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
24
25
/**
26
 * Abstract factory for creating document repositories.
27
 *
28
 * @since 1.2
29
 */
30
abstract class AbstractRepositoryFactory implements RepositoryFactory
31
{
32
    /**
33
     * The list of DocumentRepository instances.
34
     *
35
     * @var array<\Doctrine\Common\Persistence\ObjectRepository>
36
     */
37
    private $repositoryList = array();
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 364
    public function getRepository(DocumentManager $documentManager, $documentName)
43
    {
44 364
        $metadata = $documentManager->getClassMetadata($documentName);
45 364
        $hashKey = $metadata->getName() . spl_object_hash($documentManager);
46
47 364
        if (isset($this->repositoryList[$hashKey])) {
48 160
            return $this->repositoryList[$hashKey];
49
        }
50
51 364
        $repository = $this->createRepository($documentManager, ltrim($documentName, '\\'));
52
53 364
        $this->repositoryList[$hashKey] = $repository;
54
55 364
        return $repository;
56
    }
57
58
    /**
59
     * Create a new repository instance for a document class.
60
     *
61
     * @param DocumentManager $documentManager The DocumentManager instance.
62
     * @param string          $documentName    The name of the document.
63
     *
64
     * @return \Doctrine\Common\Persistence\ObjectRepository
65
     */
66 364
    protected function createRepository(DocumentManager $documentManager, $documentName)
67
    {
68 364
        $metadata            = $documentManager->getClassMetadata($documentName);
69 364
        $repositoryClassName = $metadata->customRepositoryClassName;
70
71 364
        if ($repositoryClassName === null) {
72 356
            $configuration       = $documentManager->getConfiguration();
73 356
            $repositoryClassName = $configuration->getDefaultRepositoryClassName();
74
        }
75
76 364
        return $this->instantiateRepository($repositoryClassName, $documentManager, $metadata);
77
    }
78
79
    /**
80
     * Instantiates requested repository.
81
     *
82
     * @param string $repositoryClassName
83
     * @param DocumentManager $documentManager
84
     * @param ClassMetadata $metadata
85
     * @return \Doctrine\Common\Persistence\ObjectRepository
86
     */
87
    abstract protected function instantiateRepository($repositoryClassName, DocumentManager $documentManager, ClassMetadata $metadata);
88
}
89