1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Base\Container\ApiLoader\SignalSlot; |
10
|
|
|
|
11
|
|
|
class SignalDispatcherFactory |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Relative namespace for internal signals. |
15
|
|
|
*/ |
16
|
|
|
const RELATIVE_SIGNAL_NAMESPACE = 'eZ\\Publish\\Core\\SignalSlot\\Signal'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $signalDispatcherClass; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $signalSlotMap = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $searchEngineAlias; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* SignalDispatcherFactory constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $signalDispatcherClass |
37
|
|
|
* @param $searchEngineAlias |
38
|
|
|
*/ |
39
|
|
|
public function __construct( |
40
|
|
|
$signalDispatcherClass, |
41
|
|
|
$searchEngineAlias |
42
|
|
|
) { |
43
|
|
|
$this->signalDispatcherClass = $signalDispatcherClass; |
44
|
|
|
$this->searchEngineAlias = $searchEngineAlias; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get current search engine alias. |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getSearchEngineAlias() |
53
|
|
|
{ |
54
|
|
|
return $this->searchEngineAlias; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Bulk add all signal slots if needed for a search engine. |
59
|
|
|
* |
60
|
|
|
* @param string $searchEngineAlias |
61
|
|
|
* @param array $searchEngineSignalSlots [signal => array(slot1, slot2, ...)] |
62
|
|
|
*/ |
63
|
|
|
public function addSlotsForSearchEngine($searchEngineAlias, array $searchEngineSignalSlots) |
64
|
|
|
{ |
65
|
|
|
if ($this->getSearchEngineAlias() !== $searchEngineAlias) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach ($searchEngineSignalSlots as $signalIdentifier => $slots) { |
70
|
|
View Code Duplication |
if ($signalIdentifier[0] === '\\') { |
|
|
|
|
71
|
|
|
$signalIdentifier = substr($signalIdentifier, 1); |
72
|
|
|
} elseif ($signalIdentifier !== '*') { |
73
|
|
|
$signalIdentifier = static::RELATIVE_SIGNAL_NAMESPACE . "\\$signalIdentifier"; |
74
|
|
|
} |
75
|
|
|
$this->signalSlotMap[$signalIdentifier] = $slots; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Build SignalDispatcher for SignalSlots. |
81
|
|
|
* |
82
|
|
|
* @return \eZ\Publish\Core\SignalSlot\SignalDispatcher |
83
|
|
|
*/ |
84
|
|
|
public function buildSignalDispatcher() |
85
|
|
|
{ |
86
|
|
|
return new $this->signalDispatcherClass($this->signalSlotMap); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.