RepositoryManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://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 3
    public function __construct(ManagerRegistry $managerRegistry, RepositoryManagerInterface $repositoryManager)
42
    {
43 3
        $this->managerRegistry = $managerRegistry;
44 3
        $this->repositoryManager = $repositoryManager;
45 3
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function addIndex(string $indexName, FinderInterface $finder, ?string $repositoryName = null): void
51
    {
52
        throw new \LogicException(__METHOD__.' should not be called. Call addIndex on the main repository manager');
53
    }
54
55 3
    public function addEntity($entityName, $indexName)
56
    {
57 3
        $this->entities[$entityName] = $indexName;
58 3
    }
59
60
    /**
61
     * Returns custom repository if one specified otherwise returns a basic repository.
62
     *
63
     * {@inheritdoc}
64
     */
65 3
    public function getRepository(string $entityName): Repository
66
    {
67 3
        $realEntityName = $entityName;
68 3
        if (false !== \strpos($entityName, ':')) {
69 1
            [$namespaceAlias, $simpleClassName] = \explode(':', $entityName);
0 ignored issues
show
Bug introduced by
The variable $namespaceAlias does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $simpleClassName does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
70 1
            $realEntityName = $this->managerRegistry->getAliasNamespace($namespaceAlias).'\\'.$simpleClassName;
71
        }
72
73 3
        if (isset($this->entities[$realEntityName])) {
74 3
            $realEntityName = $this->entities[$realEntityName];
75
        }
76
77 3
        return $this->repositoryManager->getRepository($realEntityName);
78
    }
79
80
    public function hasRepository(string $indexName): bool
81
    {
82
        return $this->repositoryManager->hasRepository($indexName);
83
    }
84
}
85