Completed
Pull Request — master (#1569)
by
unknown
03:33
created

RepositoryManager::addEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Doctrine;
13
14
use Doctrine\Common\Persistence\ManagerRegistry;
15
use FOS\ElasticaBundle\Finder\FinderInterface;
16
use FOS\ElasticaBundle\Manager\RepositoryManagerInterface;
17
18
/**
19
 * @author Richard Miller <[email protected]>
20
 *
21
 * Allows retrieval of basic or custom repository for mapped Doctrine
22
 * entities/documents
23
 */
24
class RepositoryManager implements RepositoryManagerInterface
25
{
26
    /** @var array */
27
    protected $entities = [];
28
29
    /** @var array */
30
    protected $repositories = [];
31
32
    /** @var ManagerRegistry */
33
    protected $managerRegistry;
34
35
    /**
36
     * @var RepositoryManagerInterface
37
     */
38
    private $repositoryManager;
39
40
    /**
41
     * @param ManagerRegistry            $managerRegistry
42
     * @param RepositoryManagerInterface $repositoryManager
43
     */
44
    public function __construct(ManagerRegistry $managerRegistry, RepositoryManagerInterface $repositoryManager)
45
    {
46
        $this->managerRegistry = $managerRegistry;
47
        $this->repositoryManager = $repositoryManager;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function addType($indexTypeName, FinderInterface $finder, $repositoryName = null)
54
    {
55
        throw new \LogicException(__METHOD__.' should not be called. Call addType on the main repository manager');
56
    }
57
58
    public function addEntity($entityName, $indexTypeName)
59
    {
60
        $this->entities[$entityName] = $indexTypeName;
61
    }
62
63
    /**
64
     * Returns custom repository if one specified otherwise returns a basic repository.
65
     *
66
     * {@inheritdoc}
67
     */
68
    public function getRepository($entityName)
69
    {
70
        $realEntityName = $entityName;
71
        if (false !== strpos($entityName, ':')) {
72
            list($namespaceAlias, $simpleClassName) = explode(':', $entityName);
73
            $realEntityName = $this->managerRegistry->getAliasNamespace($namespaceAlias).'\\'.$simpleClassName;
74
        }
75
76
        if (isset($this->entities[$realEntityName])) {
77
            $realEntityName = $this->entities[$realEntityName];
78
        }
79
80
        return $this->repositoryManager->getRepository($realEntityName);
81
    }
82
}
83