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

RepositoryManager::hasRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Manager;
13
14
use FOS\ElasticaBundle\Finder\FinderInterface;
15
use FOS\ElasticaBundle\Repository;
16
use RuntimeException;
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
    /**
27
     * @var array
28
     */
29
    private $indexes;
30
31
    /**
32
     * @var array
33
     */
34
    private $repositories;
35
36 5
    public function __construct()
37
    {
38 5
        $this->indexes = [];
39 5
        $this->repositories = [];
40 5
    }
41
42 5
    public function addIndex(string $indexName, FinderInterface $finder, string $repositoryName = null): void
43
    {
44 5
        $this->indexes[$indexName] = [
45 5
            'finder' => $finder,
46 5
            'repositoryName' => $repositoryName,
47
        ];
48 5
    }
49
50
    /**
51
     * Return repository for entity.
52
     *
53
     * Returns custom repository if one specified otherwise
54
     * returns a basic repository.
55
     *
56
     * @param string $indexName
57
     *
58
     * @return Repository
59
     */
60 5
    public function getRepository(string $indexName): Repository
61
    {
62 5
        if (isset($this->repositories[$indexName])) {
63
            return $this->repositories[$indexName];
64
        }
65
66 5
        if (!$this->hasRepository($indexName)) {
67 1
            throw new RuntimeException(sprintf('No search finder configured for %s', $indexName));
68
        }
69
70 4
        $repository = $this->createRepository($indexName);
71 3
        $this->repositories[$indexName] = $repository;
72
73 3
        return $repository;
74
    }
75
76
    /**
77
     * @param $indexName
78
     *
79
     * @return string
80
     */
81 4
    protected function getRepositoryName($indexName)
82
    {
83 4
        if (isset($this->indexes[$indexName]['repositoryName'])) {
84 3
            return $this->indexes[$indexName]['repositoryName'];
85
        }
86
87 1
        return 'FOS\ElasticaBundle\Repository';
88
    }
89
90
    /**
91
     * @param $indexName
92
     *
93
     * @return mixed
94
     */
95 4
    private function createRepository($indexName)
96
    {
97 4
        if (!class_exists($repositoryName = $this->getRepositoryName($indexName))) {
98 1
            throw new RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $indexName));
99
        }
100
101 3
        return new $repositoryName($this->indexes[$indexName]['finder']);
102
    }
103
104 5
    public function hasRepository(string $indexName): bool
105
    {
106 5
        return isset($this->indexes[$indexName]);
107
    }
108
}
109