Completed
Push — master ( d27371...785c50 )
by Andreas
16s
created

AbstractRepositoryFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 54
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getRepository() 0 15 2
A createRepository() 0 7 2
instantiateRepository() 0 1 ?
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\Common\Persistence\ObjectRepository;
23
use Doctrine\ODM\MongoDB\DocumentManager;
24
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
25
26
/**
27
 * Abstract factory for creating document repositories.
28
 *
29
 * @since 1.2
30
 */
31
abstract class AbstractRepositoryFactory implements RepositoryFactory
32
{
33
    /**
34
     * The list of DocumentRepository instances.
35
     *
36
     * @var ObjectRepository[]
37
     */
38
    private $repositoryList = array();
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 364
    public function getRepository(DocumentManager $documentManager, $documentName)
44
    {
45 364
        $metadata = $documentManager->getClassMetadata($documentName);
46 364
        $hashKey = $metadata->getName() . spl_object_hash($documentManager);
47
48 364
        if (isset($this->repositoryList[$hashKey])) {
49 160
            return $this->repositoryList[$hashKey];
50
        }
51
52 364
        $repository = $this->createRepository($documentManager, ltrim($documentName, '\\'));
53
54 364
        $this->repositoryList[$hashKey] = $repository;
55
56 364
        return $repository;
57
    }
58
59
    /**
60
     * Create a new repository instance for a document class.
61
     *
62
     * @param DocumentManager $documentManager The DocumentManager instance.
63
     * @param string          $documentName    The name of the document.
64
     *
65
     * @return ObjectRepository
66
     */
67 364
    protected function createRepository(DocumentManager $documentManager, $documentName)
68
    {
69 364
        $metadata            = $documentManager->getClassMetadata($documentName);
70 364
        $repositoryClassName = $metadata->customRepositoryClassName ?: $documentManager->getConfiguration()->getDefaultRepositoryClassName();
71
72 364
        return $this->instantiateRepository($repositoryClassName, $documentManager, $metadata);
73
    }
74
75
    /**
76
     * Instantiates requested repository.
77
     *
78
     * @param string $repositoryClassName
79
     * @param DocumentManager $documentManager
80
     * @param ClassMetadata $metadata
81
     * @return ObjectRepository
82
     */
83
    abstract protected function instantiateRepository($repositoryClassName, DocumentManager $documentManager, ClassMetadata $metadata);
84
}
85