Passed
Push — master ( 723c0e...dfb1ea )
by Timo
04:52
created

ResultParserRegistry::getParsers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2017 Timo Hund <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 2 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
29
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
30
use TYPO3\CMS\Core\SingletonInterface;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Class ResultParserRegistry
35
 *
36
 * @author Frans Saris <[email protected]>
37
 * @author Timo Hund <[email protected]>
38
 */
39
class ResultParserRegistry implements SingletonInterface
40
{
41
    /**
42
     * Array of available parser classNames
43
     *
44
     * @var array
45
     */
46
    protected $parsers = [
47
        100 => DefaultResultParser::class,
48
    ];
49
50
    /**
51
     * @var AbstractResultParser[]
52
     */
53
    protected $parserInstances;
54
55
    /**
56
     * Get registered parser classNames
57
     *
58
     * @return array
59
     */
60
    public function getParsers()
61
    {
62
        return $this->parsers;
63
    }
64
65
    /**
66
     * Can be used to register a custom parser.
67
     *
68
     * @param string $className classname of the parser that should be used
69
     * @param int $priority higher priority means more important
70
     * @throws \InvalidArgumentException
71
     */
72
    public function registerParser($className, $priority)
73
    {
74
        // check if the class is available for TYPO3 before registering the driver
75
        if (!class_exists($className)) {
76
            throw new \InvalidArgumentException('Class ' . $className . ' does not exist.', 1468863997);
77
        }
78
79
        if (!is_subclass_of($className, AbstractResultParser::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \ApacheSolrForTypo3\Solr...ractResultParser::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
80
            throw new \InvalidArgumentException('Parser ' . $className . ' needs to implement the AbstractResultParser.', 1468863998);
81
        }
82
83
        if (array_key_exists((int)$priority, $this->parsers)) {
84
            throw new \InvalidArgumentException('There is already a parser registerd with priority ' . (int)$priority . '.', 1468863999);
85
        }
86
87
        $this->parsers[(int)$priority] = $className;
88
    }
89
90
    /**
91
     * @return AbstractResultParser[]
92
     */
93
    public function getParserInstances()
94
    {
95
        if ($this->parserInstances === null) {
96
            ksort($this->parsers);
97
            $orderedParsers = array_reverse($this->parsers);
98
            foreach ($orderedParsers as $className) {
99
                $this->parserInstances[] = $this->createParserInstance($className);
100
            }
101
        }
102
        return $this->parserInstances;
103
    }
104
105
    /**
106
     * @param SearchResultSet $resultSet
107
     * @return AbstractResultParser|null
108
     */
109
    public function getParser(SearchResultSet $resultSet)
110
    {
111
        /** @var AbstractResultParser $parser */
112
        foreach ($this->getParserInstances() as $parser) {
113
            if ($parser->canParse($resultSet)) {
114
                return $parser;
115
            }
116
        }
117
        return null;
118
    }
119
120
    /**
121
     * Create an instance of a certain parser class
122
     *
123
     * @return AbstractResultParser
124
     */
125
    protected function createParserInstance($className)
126
    {
127
        return GeneralUtility::makeInstance($className);
128
    }
129
}
130