Completed
Pull Request — master (#1109)
by Karel
20:30 queued 17:31
created

RepositoryManager::getRepository()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
3
namespace FOS\ElasticaBundle\Manager;
4
5
use Doctrine\Common\Annotations\Reader;
6
use FOS\ElasticaBundle\Finder\FinderInterface;
7
use RuntimeException;
8
9
/**
10
 * @author Richard Miller <[email protected]>
11
 *
12
 * Allows retrieval of basic or custom repository for mapped Doctrine
13
 * entities/documents.
14
 */
15
class RepositoryManager implements RepositoryManagerInterface
16
{
17
    protected $entities = array();
18
    protected $repositories = array();
19
    protected $reader;
20
21 12
    public function __construct(Reader $reader)
22
    {
23 12
        $this->reader = $reader;
24 12
    }
25
26 12
    public function addEntity($entityName, FinderInterface $finder, $repositoryName = null)
27
    {
28 12
        $this->entities[$entityName] = array();
29 12
        $this->entities[$entityName]['finder'] = $finder;
30 12
        $this->entities[$entityName]['repositoryName'] = $repositoryName;
31 12
    }
32
33
    /**
34
     * Return repository for entity.
35
     *
36
     * Returns custom repository if one specified otherwise
37
     * returns a basic repository.
38
     */
39 12
    public function getRepository($entityName)
40
    {
41 12
        if (isset($this->repositories[$entityName])) {
42 1
            return $this->repositories[$entityName];
43
        }
44
45 12
        if (!isset($this->entities[$entityName])) {
46 4
            throw new RuntimeException(sprintf('No search finder configured for %s', $entityName));
47
        }
48
49 8
        $repository = $this->createRepository($entityName);
50 7
        $this->repositories[$entityName] = $repository;
51
52 7
        return $repository;
53
    }
54
55
    /**
56
     * @param string $entityName
57
     *
58
     * @return string
59
     */
60 8
    protected function getRepositoryName($entityName)
61
    {
62 8
        if (isset($this->entities[$entityName]['repositoryName'])) {
63 4
            return $this->entities[$entityName]['repositoryName'];
64
        }
65
66 4
        $refClass   = new \ReflectionClass($entityName);
67 4
        $annotation = $this->reader->getClassAnnotation($refClass, 'FOS\\ElasticaBundle\\Annotation\\Search');
68 4
        if ($annotation) {
69 1
            $this->entities[$entityName]['repositoryName']
70 1
                = $annotation->repositoryClass;
71
72 1
            return $annotation->repositoryClass;
73
        }
74
75 3
        return 'FOS\ElasticaBundle\Repository';
76
    }
77
78
    /**
79
     * @param string $entityName
80
     *
81
     * @return mixed
82
     */
83 8
    private function createRepository($entityName)
84
    {
85 8
        if (!class_exists($repositoryName = $this->getRepositoryName($entityName))) {
86 1
            throw new RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $entityName));
87
        }
88
89 7
        return new $repositoryName($this->entities[$entityName]['finder']);
90
    }
91
}
92