Completed
Pull Request — master (#1571)
by
unknown
13:33
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 $types;
30
31
    /**
32
     * @var array
33
     */
34
    private $repositories;
35
36 5
    public function __construct()
37
    {
38 5
        $this->types = [];
39 5
        $this->repositories = [];
40 5
    }
41
42 5
    public function addType($indexTypeName, FinderInterface $finder, $repositoryName = null)
43
    {
44 5
        $this->types[$indexTypeName] = [
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 $typeName
57
     *
58
     * @return Repository
59
     */
60 5
    public function getRepository($typeName)
61
    {
62 5
        if (isset($this->repositories[$typeName])) {
63
            return $this->repositories[$typeName];
64
        }
65
66 5
        if (!$this->hasRepository($typeName)) {
67 1
            throw new RuntimeException(sprintf('No search finder configured for %s', $typeName));
68
        }
69
70 4
        $repository = $this->createRepository($typeName);
71 3
        $this->repositories[$typeName] = $repository;
72
73 3
        return $repository;
74
    }
75
76
    /**
77
     * Checks if repository exists for type
78
     *
79
     * @param $typeName
80
     * @return bool
81
     */
82 5
    public function hasRepository($typeName): bool
83
    {
84 5
        return isset($this->types[$typeName]);
85
    }
86
87
    /**
88
     * @param $typeName
89
     *
90
     * @return string
91
     */
92 4
    protected function getRepositoryName($typeName)
93
    {
94 4
        if (isset($this->types[$typeName]['repositoryName'])) {
95 3
            return $this->types[$typeName]['repositoryName'];
96
        }
97
98 1
        return 'FOS\ElasticaBundle\Repository';
99
    }
100
101
    /**
102
     * @param $typeName
103
     *
104
     * @return mixed
105
     */
106 4
    private function createRepository($typeName)
107
    {
108 4
        if (!class_exists($repositoryName = $this->getRepositoryName($typeName))) {
109 1
            throw new RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $typeName));
110
        }
111
112 3
        return new $repositoryName($this->types[$typeName]['finder']);
113
    }
114
}
115