Completed
Push — task/master/1311-combine-facet... ( c66844...a56b0a )
by Timo
21:49
created

FacetParserRegistry::createParserInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Hierarchy\HierarchyFacetParser;
18
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\QueryGroup\QueryGroupFacetParser;
19
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RangeBased\DateRange\DateRangeFacetParser;
20
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\RangeBased\NumericRange\NumericRangeFacetParser;
21
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options\OptionsFacetParser;
22
use ApacheSolrForTypo3\Solr\System\Object\AbstractClassRegistry;
23
24
/**
25
 * Class FacetParserRegistry
26
 *
27
 * @author Frans Saris <[email protected]>
28
 * @author Timo Hund <[email protected]>
29
 * @package ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets
30
 */
31
class FacetParserRegistry extends AbstractClassRegistry
32
{
33
    /**
34
     * Array of available parser classNames
35
     *
36
     * @var array
37
     */
38
    protected $classMap = [
39
        'options' => OptionsFacetParser::class,
40
        'hierarchy' => HierarchyFacetParser::class,
41
        'queryGroup' => QueryGroupFacetParser::class,
42
        'dateRange' => DateRangeFacetParser::class,
43
        'numericRange' => NumericRangeFacetParser::class,
44
    ];
45
46
    /**
47
     * Default parser className
48
     *
49
     * @var string
50
     */
51
    protected $defaultClass = OptionsFacetParser::class;
52
53
    /**
54
     * Get defaultParser
55
     *
56
     * @return string
57
     */
58
    public function getDefaultParser()
59
    {
60
        return $this->defaultClass;
61
    }
62
63
    /**
64
     * Set defaultParser
65
     *
66
     * @param string $defaultParserClassName
67
     */
68 1
    public function setDefaultParser($defaultParserClassName)
69
    {
70 1
        $this->defaultClass = $defaultParserClassName;
71 1
    }
72
73
    /**
74
     * Get registered parser classNames
75
     *
76
     * @return array
77
     */
78
    public function getParsers()
79
    {
80
        return $this->classMap;
81
    }
82
83
    /**
84
     * @param string $className
85
     * @param string $type
86
     * @throws \InvalidArgumentException
87
     */
88 3
    public function registerParser($className, $type)
89
    {
90 3
        return $this->register($className, $type, FacetParserInterface::class);
91
    }
92
93
    /**
94
     * Get parser
95
     *
96
     * @param string $type
97
     * @return FacetParserInterface
98
     * @throws \Exception
99
     */
100 43
    public function getParser($type)
101
    {
102 43
        $instance = $this->getInstance($type);
103 43
        if(!$instance instanceof FacetParserInterface) {
104
            throw new \Exception('Invalid class registered for ' . htmlspecialchars($type));
105
        }
106 43
        return $instance;
107
    }
108
}
109