FinderManager::getClassFinder()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Mdiyakov\DoctrineSolrBundle\Manager;
4
5
use Mdiyakov\DoctrineSolrBundle\Config\Config;
6
use Mdiyakov\DoctrineSolrBundle\Exception\FinderException;
7
use Mdiyakov\DoctrineSolrBundle\Finder\ClassFinder;
8
use Mdiyakov\DoctrineSolrBundle\Finder\SchemaFinder;
9
use Mdiyakov\DoctrineSolrBundle\Query\SelectQueryBuilder;
10
11
class FinderManager
12
{
13
    /**
14
     * @var SchemaFinder[]
15
     */
16
    private $schemaFinders = [];
17
18
    /**
19
     * @var ClassFinder[]
20
     */
21
    private $classFinders = [];
22
23
    /**
24
     * @param Config $config
25
     * @param SelectQueryBuilder $queryBuilder
26
     */
27
    public function __construct(Config $config, SelectQueryBuilder $queryBuilder)
28
    {
29
        $entityConfigs = $config->getIndexedEntities();
30
        $defaultFinderClass = 'Mdiyakov\DoctrineSolrBundle\Finder\ClassFinder';
31
        foreach ($entityConfigs as $entityConfig) {
32
            $entityClass = $entityConfig['class'];
33
            if (!array_key_exists($entityClass, $this->classFinders)) {
34
35
                $finderClass = (!empty($entityConfig['finder_class'])) ? $entityConfig['finder_class'] : $defaultFinderClass;
36
37
                if ($finderClass != $defaultFinderClass && !is_subclass_of($finderClass, $defaultFinderClass)) {
38
                    throw new FinderException('Finder class of entity must be extended from ClassFinder');
39
                }
40
41
                $this->classFinders[$entityClass] = new $finderClass(
42
                    $entityClass,
43
                    $queryBuilder->buildClassSelectQuery($entityClass)
44
                );
45
            }
46
47
            $entitySchemaName = $entityConfig['schema'];
48
            if (!array_key_exists($entitySchemaName, $this->schemaFinders)) {
49
                $this->schemaFinders[$entitySchemaName] = new SchemaFinder(
50
                    $queryBuilder,
51
                    $config->getSchemaByEntityClass($entityClass),
52
                    $config
53
                );
54
            }
55
        }
56
57
    }
58
59
    /**
60
     * @param string $schemaName
61
     * @return SchemaFinder
62
     * @throws \InvalidArgumentException
63
     */
64
    public function getSchemaFinder($schemaName)
65
    {
66
        if (!array_key_exists($schemaName, $this->schemaFinders)) {
67
            throw new \InvalidArgumentException(
68
                sprintf('Schema finder %s is not found', $schemaName)
69
            );
70
        }
71
72
        return $this->schemaFinders[$schemaName];
73
    }
74
75
    /**
76
     * @param string $class
77
     * @return ClassFinder
78
     * @throws \InvalidArgumentException
79
     */
80
    public function getClassFinder($class)
81
    {
82
        if (!array_key_exists($class, $this->classFinders)) {
83
            throw new \InvalidArgumentException(
84
                sprintf('Class finder %s is not found', $class)
85
            );
86
        }
87
88
        return $this->classFinders[$class];
89
    }
90
}