Passed
Push — master ( f55a40...3301f5 )
by Timo
23:56
created

Manager   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 0
Metric Value
wmc 5
dl 0
loc 60
ccs 10
cts 14
cp 0.7143
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerFrontendHelper() 0 3 1
A resolveAction() 0 14 3
A getActivatedFrontendHelpers() 0 3 1
1
<?php
2
namespace ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-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 3 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 TYPO3\CMS\Core\Utility\GeneralUtility;
28
29
/**
30
 * Index Queue Page Indexer frontend helper manager.
31
 *
32
 * Manages frontend helpers and creates instances.
33
 *
34
 * @author Ingo Renner <[email protected]>
35
 */
36
class Manager
37
{
38
39
    /**
40
     * Frontend helper descriptions.
41
     *
42
     * @var array
43
     */
44
    protected static $frontendHelperRegistry = [];
45
46
    /**
47
     * Instances of activated frontend helpers.
48
     *
49
     * @var array
50
     */
51
    protected $activatedFrontendHelpers = [];
52
53
    /**
54
     * Registers a frontend helper class for a certain action.
55
     *
56
     * @param string $action Action to register.
57
     * @param string $class Class to register for an action.
58
     */
59 1
    public static function registerFrontendHelper($action, $class)
60
    {
61 1
        self::$frontendHelperRegistry[$action] = $class;
62 1
    }
63
64
    /**
65
     * Tries to find a frontend helper for a given action. If found, creates an
66
     * instance of the helper.
67
     *
68
     * @param string $action The action to get a frontend helper for.
69
     * @return FrontendHelper Index Queue page indexer frontend helper
70
     * @throws \RuntimeException if the class registered for an action is not an implementation of ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper\FrontendHelper
71
     */
72 2
    public function resolveAction($action)
73
    {
74 2
        if (!array_key_exists($action, self::$frontendHelperRegistry)) {
75 1
            return null;
76
        }
77
78 1
        $frontendHelper = GeneralUtility::makeInstance(self::$frontendHelperRegistry[$action]);
79 1
        if (!$frontendHelper instanceof FrontendHelper) {
80 1
            $message = self::$frontendHelperRegistry[$action] . ' is not an implementation of ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper\FrontendHelper';
81 1
            throw new \RuntimeException($message, 1292497896);
82
        }
83
84
        $this->activatedFrontendHelpers[$action] = $frontendHelper;
85
        return $frontendHelper;
86
    }
87
88
    /**
89
     * Gets an array with references to activated frontend helpers.
90
     *
91
     * @return array Array of references to activated frontend helpers.
92
     */
93
    public function getActivatedFrontendHelpers()
94
    {
95
        return $this->activatedFrontendHelpers;
96
    }
97
}
98