Passed
Push — master ( 29f3de...613566 )
by Timo
04:40
created

SearchComponentManager::removeSearchComponent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Search;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2012-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 2 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use ApacheSolrForTypo3\Solr\Search\SearchComponent;
28
use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30
/**
31
 * Search components manager, registration and stuff...
32
 *
33
 * @author Ingo Renner <[email protected]>
34
 */
35
class SearchComponentManager
36
{
37
38
    /**
39
     * Search component registry.
40
     *
41
     * @var array
42
     */
43
    protected static $searchComponents = [];
44
45
    /**
46
     * Registers a search component.
47
     *
48
     * @param string $componentName Search component name
49
     * @param string $componentClassName Component class
50
     */
51 220
    public static function registerSearchComponent(
52
        $componentName,
53
        $componentClassName
54
    ) {
55 220
        self::$searchComponents[$componentName] = $componentClassName;
56 220
    }
57
58
    /**
59
     * Returns all currently registered search components.
60
     *
61
     * @return array An array of search component instances
62
     */
63 32
    public function getSearchComponents()
64
    {
65 32
        $searchComponents = [];
66
67 32
        foreach (self::$searchComponents as $componentName => $componentClass) {
68 32
            $searchComponents[$componentName] = $this->getSearchComponent($componentName);
69
        }
70
71 32
        return $searchComponents;
72
    }
73
74
    /**
75
     * Instanciates a registered search component
76
     *
77
     * @param string $componentName Search component name
78
     * @return SearchComponent Instance of the requested search component
79
     * @throws \InvalidArgumentException if $componentName is not a registered search component
80
     * @throws \RuntimeException if the class registered for $componentName is not an implementation of ApacheSolrForTypo3\Solr\Search\SearchComponent
81
     */
82 32
    public function getSearchComponent($componentName)
83
    {
84 32
        if (!array_key_exists($componentName, self::$searchComponents)) {
85
            throw new \InvalidArgumentException(
86
                'No search component registered named ' . $componentName,
87
                1343398440
88
            );
89
        }
90
91 32
        $searchComponent = GeneralUtility::makeInstance(self::$searchComponents[$componentName]);
92
93 32
        if (!($searchComponent instanceof SearchComponent)) {
94
            throw new \RuntimeException(
95
                'Class ' . self::$searchComponents[$componentName] . ' must implement interface ' . SearchComponent::class,
96
                1343398621
97
            );
98
        }
99
100 32
        return $searchComponent;
101
    }
102
103
    /**
104
     * Unregisters a search component
105
     *
106
     * @param string $componentName Search component name
107
     */
108
    public function removeSearchComponent($componentName)
109
    {
110
        if (!array_key_exists($componentName, self::$searchComponents)) {
111
            return;
112
        }
113
114
        unset(self::$searchComponents[$componentName]);
115
    }
116
}
117