Completed
Pull Request — master (#1638)
by Giovanni
07:23
created

RepositoryManager::addIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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($indexName, FinderInterface $finder, $repositoryName = null)
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($indexName)
61
    {
62 5
        if (isset($this->repositories[$indexName])) {
63
            return $this->repositories[$indexName];
64
        }
65
66 5
        if (!isset($this->indexes[$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