Completed
Pull Request — master (#1051)
by Karel
07:07
created

RepositoryManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 8
    protected function getRepositoryName($entityName)
56
    {
57 8
        if (isset($this->entities[$entityName]['repositoryName'])) {
58 4
            return $this->entities[$entityName]['repositoryName'];
59
        }
60
61 4
        $refClass   = new \ReflectionClass($entityName);
62 4
        $annotation = $this->reader->getClassAnnotation($refClass, 'FOS\\ElasticaBundle\\Annotation\\Search');
63 4
        if ($annotation) {
64 1
            $this->entities[$entityName]['repositoryName']
65 1
                = $annotation->repositoryClass;
66
67 1
            return $annotation->repositoryClass;
68
        }
69
70 3
        return 'FOS\ElasticaBundle\Repository';
71
    }
72
73
    /**
74
     * @param string $entityName
75
     */
76 8
    private function createRepository($entityName)
77
    {
78 8
        if (!class_exists($repositoryName = $this->getRepositoryName($entityName))) {
79 1
            throw new RuntimeException(sprintf('%s repository for %s does not exist', $repositoryName, $entityName));
80
        }
81
82 7
        return new $repositoryName($this->entities[$entityName]['finder']);
83
    }
84
}
85