Passed
Push — master ( a95893...a4d2df )
by Timo
57s
created

FacetParserRegistry::getDefaultParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Core\SingletonInterface;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
20
21
/**
22
 * Class FacetParserRegistry
23
 *
24
 * @author Frans Saris <[email protected]>
25
 * @author Timo Hund <[email protected]>
26
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets
27
 */
28
class FacetParserRegistry implements SingletonInterface
29
{
30
    /**
31
     * Array of available parser classNames
32
     *
33
     * @var array
34
     */
35
    protected $parsers = [
36
        'options' => OptionBased\Options\OptionsFacetParser::class,
37
        'hierarchy' => OptionBased\Hierarchy\HierarchyFacetParser::class,
38
        'queryGroup' => OptionBased\QueryGroup\QueryGroupFacetParser::class,
39
        'dateRange' => RangeBased\DateRange\DateRangeFacetParser::class,
40
        'numericRange' => RangeBased\NumericRange\NumericRangeFacetParser::class,
41
    ];
42
    /**
43
     * Default parser className
44
     *
45
     * @var string
46
     */
47
    protected $defaultParser = OptionBased\Options\OptionsFacetParser::class;
48
49
    /**
50
     * @var ObjectManagerInterface
51
     */
52
    protected $objectManager;
53
54
    /**
55
     * @param ObjectManagerInterface $objectManager
56
     */
57 42
    public function injectObjectManager(ObjectManagerInterface $objectManager)
58
    {
59 42
        $this->objectManager = $objectManager;
60 42
    }
61
62
    /**
63
     * Get defaultParser
64
     *
65
     * @return string
66
     */
67
    public function getDefaultParser()
68
    {
69
        return $this->defaultParser;
70
    }
71
72
    /**
73
     * Set defaultParser
74
     *
75
     * @param string $defaultParserClassName
76
     */
77 1
    public function setDefaultParser($defaultParserClassName)
78
    {
79 1
        $this->defaultParser = $defaultParserClassName;
80 1
    }
81
82
    /**
83
     * Get registered parser classNames
84
     *
85
     * @return array
86
     */
87
    public function getParsers()
88
    {
89
        return $this->parsers;
90
    }
91
92
    /**
93
     * @param string $className
94
     * @param string $type
95
     * @throws \InvalidArgumentException
96
     */
97 3
    public function registerParser($className, $type)
98
    {
99
100
        // check if the class is available for TYPO3 before registering the driver
101 3
        if (!class_exists($className)) {
102 1
            throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1462883324);
103
        }
104
105 2
        if (!in_array(FacetParserInterface::class, class_implements($className), true)) {
106 1
            throw new \InvalidArgumentException('Parser ' . $className . ' needs to implement the FacetParserInterface.', 1462883325);
107
        }
108
109 1
        $this->parsers[$type] = $className;
110 1
    }
111
112
    /**
113
     * Get parser
114
     *
115
     * @param string $type
116
     * @return FacetParserInterface
117
     */
118 43
    public function getParser($type)
119
    {
120 43
        $className = $this->defaultParser;
121
122 43
        if (isset($this->parsers[$type])) {
123 26
            $className = $this->parsers[$type];
124
        }
125
126 43
        return $this->createParserInstance($className);
127
    }
128
129
    /**
130
     * Create an instance of a certain parser class
131
     *
132
     * @param string $className
133
     * @return FacetParserInterface
134
     */
135 40
    protected function createParserInstance($className)
136
    {
137
        /** @var $instance FacetParserInterface */
138 40
        $instance = $this->objectManager->get($className);
139 40
        return $instance;
140
    }
141
}
142