|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Callbacks\CallbackVisitor |
|
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\Callbacks; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Visitor implementation for a subject's callbacks. |
|
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 CallbackVisitor |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Return's a new visitor instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @return \TechDivision\Import\Cli\Callbacks\CallbackVisitor The visitor instance |
|
41
|
|
|
*/ |
|
42
|
|
|
public static function get() |
|
43
|
|
|
{ |
|
44
|
|
|
return new CallbackVisitor(); |
|
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 callbacks |
|
57
|
|
|
foreach ($subject->getCallbackMappings() as $type => $callbacks) { |
|
58
|
|
|
$this->prepareCallbacks($subject, $callbacks, $type); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Prepare the callbacks defined in the system configuration. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject to prepare the callbacks for |
|
66
|
|
|
* @param array $callbacks The array with the callbacks |
|
67
|
|
|
* @param string $type The actual callback type |
|
68
|
|
|
* |
|
69
|
|
|
* @return void |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
public function prepareCallbacks(SubjectInterface $subject, array $callbacks, $type = null) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
|
|
74
|
|
|
// iterate over the array with callbacks and prepare them |
|
75
|
|
|
foreach ($callbacks as $key => $callback) { |
|
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($callback)) { |
|
83
|
|
|
$this->prepareCallbacks($subject, $callback, $type); |
|
84
|
|
|
} else { |
|
85
|
|
|
$callbackInstance = $this->callbackFactory($subject, $callback); |
|
86
|
|
|
$subject->registerCallback($callbackInstance, $type); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Initialize and return a new callback 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 callback to instanciate |
|
96
|
|
|
* |
|
97
|
|
|
* @return \TechDivision\Import\Callbacks\CallbackInterface The callback instance |
|
98
|
|
|
*/ |
|
99
|
|
|
public function callbackFactory(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.