Completed
Push — master ( e17902...26729b )
by Karel
05:38 queued 11s
created

RepositoryManager::addIndex()   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 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Persistence\ManagerRegistry;
15
use FOS\ElasticaBundle\Finder\FinderInterface;
16
use FOS\ElasticaBundle\Manager\RepositoryManagerInterface;
17
use FOS\ElasticaBundle\Repository;
18
19
/**
20
 * @author Richard Miller <[email protected]>
21
 *
22
 * Allows retrieval of basic or custom repository for mapped Doctrine
23
 * entities/documents
24
 */
25
class RepositoryManager implements RepositoryManagerInterface
26
{
27
    /** @var array */
28
    protected $entities = [];
29
30
    /** @var array */
31
    protected $repositories = [];
32
33
    /** @var ManagerRegistry */
34
    protected $managerRegistry;
35
36
    /**
37
     * @var RepositoryManagerInterface
38
     */
39
    private $repositoryManager;
40
41
    /**
42
     * @param ManagerRegistry            $managerRegistry
43
     * @param RepositoryManagerInterface $repositoryManager
44
     */
45 3
    public function __construct(ManagerRegistry $managerRegistry, RepositoryManagerInterface $repositoryManager)
46
    {
47 3
        $this->managerRegistry = $managerRegistry;
48 3
        $this->repositoryManager = $repositoryManager;
49 3
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function addIndex(string $indexName, FinderInterface $finder, string $repositoryName = null): void
55
    {
56
        throw new \LogicException(__METHOD__.' should not be called. Call addIndex on the main repository manager');
57
    }
58
59 3
    public function addEntity($entityName, $indexName)
60
    {
61 3
        $this->entities[$entityName] = $indexName;
62 3
    }
63
64
    /**
65
     * Returns custom repository if one specified otherwise returns a basic repository.
66
     *
67
     * {@inheritdoc}
68
     */
69 3
    public function getRepository(string $entityName): Repository
70
    {
71 3
        $realEntityName = $entityName;
72 3
        if (false !== strpos($entityName, ':')) {
73 1
            list($namespaceAlias, $simpleClassName) = explode(':', $entityName);
74 1
            $realEntityName = $this->managerRegistry->getAliasNamespace($namespaceAlias).'\\'.$simpleClassName;
75
        }
76
77 3
        if (isset($this->entities[$realEntityName])) {
78 3
            $realEntityName = $this->entities[$realEntityName];
79
        }
80
81 3
        return $this->repositoryManager->getRepository($realEntityName);
82
    }
83
84
    public function hasRepository(string $indexName): bool
85
    {
86
        return $this->repositoryManager->hasRepository($indexName);
87
    }
88
}
89