|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Observers\ObserverVisitor |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Observers; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Visitor implementation for a subject's observers. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Tim Wagner <[email protected]> |
|
29
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
class ObserverVisitor |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Return's a new visitor instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @return \TechDivision\Import\Cli\Observers\ObserverVisitor The visitor instance |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function get() |
|
43
|
|
|
{ |
|
44
|
|
|
return new ObserverVisitor(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Visitor implementation that initializes the observers of the passed subject. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to initialize the observers for |
|
51
|
|
|
* |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function visit(SubjectInterface $subject) |
|
55
|
|
|
{ |
|
56
|
|
|
// prepare the observers |
|
57
|
|
|
foreach ($subject->getConfiguration()->getObservers() as $observers) { |
|
58
|
|
|
$this->prepareObservers($subject, $observers); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Prepare the observers defined in the system configuration. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to prepare the observers for |
|
66
|
|
|
* @param array $observers The array with the observers |
|
67
|
|
|
* @param string $type The actual observer type |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
protected function prepareObservers(SubjectInterface $subject, array $observers, $type = null) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
// iterate over the array with observers and prepare them |
|
75
|
|
|
foreach ($observers as $key => $observer) { |
|
76
|
|
|
// we have to initialize the type only on the first level |
|
77
|
|
|
if ($type == null) { |
|
78
|
|
|
$type = $key; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// query whether or not we've an subarry or not |
|
82
|
|
|
if (is_array($observer)) { |
|
83
|
|
|
$this->prepareObservers($subject, $observer, $type); |
|
84
|
|
|
} else { |
|
85
|
|
|
$observerInstance = $this->observerFactory($subject, $observer); |
|
86
|
|
|
$subject->registerObserver($observerInstance, $type); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Initialize and return a new observer of the passed type. |
|
93
|
|
|
* |
|
94
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to create the observer for |
|
95
|
|
|
* @param string $className The type of the observer to instanciate |
|
96
|
|
|
* |
|
97
|
|
|
* @return \TechDivision\Import\Observers\ObserverInterface The observer instance |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function observerFactory(SubjectInterface $subject, $className) |
|
100
|
|
|
{ |
|
101
|
|
|
return new $className($subject); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
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.