|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zikula\SearchModule\Collector; |
|
13
|
|
|
|
|
14
|
|
|
use Zikula\SearchModule\SearchableInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class SearchableModuleCollector |
|
18
|
|
|
*/ |
|
19
|
|
|
class SearchableModuleCollector |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var SearchableInterface[] e.g. [<moduleName> => <ServiceObject>] |
|
23
|
|
|
*/ |
|
24
|
|
|
private $searchableModules = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* SearchableModuleCollector constructor. |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Add a service to the collection. |
|
35
|
|
|
* @param string $moduleName |
|
36
|
|
|
* @param SearchableInterface $service |
|
37
|
|
|
*/ |
|
38
|
|
View Code Duplication |
public function add($moduleName, SearchableInterface $service) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
if (isset($this->searchableModules[$moduleName])) { |
|
41
|
|
|
throw new \InvalidArgumentException('Attempting to register a searchable module with a duplicate module name. (' . $moduleName . ')'); |
|
42
|
|
|
} |
|
43
|
|
|
$this->searchableModules[$moduleName] = $service; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get a SearchableInterface from the collection by moduleName. |
|
48
|
|
|
* @param $moduleName |
|
49
|
|
|
* @return SearchableInterface|null |
|
50
|
|
|
*/ |
|
51
|
|
|
public function get($moduleName) |
|
52
|
|
|
{ |
|
53
|
|
|
return isset($this->searchableModules[$moduleName]) ? $this->searchableModules[$moduleName] : null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get all the searchableModules in the collection. |
|
58
|
|
|
* @return SearchableInterface[] |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getAll() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->searchableModules; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @return array of service aliases |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getKeys() |
|
69
|
|
|
{ |
|
70
|
|
|
return array_keys($this->searchableModules); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.