1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Category\Observers\AbstractCategoryImportObserver |
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 2019 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-category |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Category\Observers; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Observers\AbstractObserver; |
24
|
|
|
use TechDivision\Import\Subjects\SubjectInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Abstract category observer that handles the process to import category bunches. |
28
|
|
|
* |
29
|
|
|
* @author Tim Wagner <[email protected]> |
30
|
|
|
* @copyright 2019 TechDivision GmbH <[email protected]> |
31
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
32
|
|
|
* @link https://github.com/techdivision/import-category |
33
|
|
|
* @link http://www.techdivision.com |
34
|
|
|
*/ |
35
|
|
|
abstract class AbstractCategoryImportObserver extends AbstractObserver implements CategoryImportObserverInterface |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Will be invoked by the action on the events the listener has been registered for. |
40
|
|
|
* |
41
|
|
|
* @param \TechDivision\Import\Subjects\SubjectInterface $subject The subject instance |
42
|
|
|
* |
43
|
|
|
* @return array The modified row |
44
|
|
|
* @see \TechDivision\Import\Observers\ObserverInterface::handle() |
45
|
|
|
*/ |
46
|
|
|
public function handle(SubjectInterface $subject) |
47
|
|
|
{ |
48
|
|
|
|
49
|
|
|
// initialize the row |
50
|
|
|
$this->setSubject($subject); |
51
|
|
|
$this->setRow($subject->getRow()); |
52
|
|
|
|
53
|
|
|
// process the functionality and return the row |
54
|
|
|
$this->process(); |
55
|
|
|
|
56
|
|
|
// return the processed row |
57
|
|
|
return $this->getRow(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Process the observer's business logic. |
62
|
|
|
* |
63
|
|
|
* @return void |
64
|
|
|
*/ |
65
|
|
|
abstract protected function process(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Queries whether or not the path has already been processed. |
69
|
|
|
* |
70
|
|
|
* @param string $path The path to check |
71
|
|
|
* |
72
|
|
|
* @return boolean TRUE if the path has been processed, else FALSE |
73
|
|
|
*/ |
74
|
|
|
protected function hasBeenProcessed($path) |
75
|
|
|
{ |
76
|
|
|
return $this->getSubject()->hasBeenProcessed($path); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return's TRUE if the passed path is the actual one. |
81
|
|
|
* |
82
|
|
|
* @param string $path The path to check |
83
|
|
|
* |
84
|
|
|
* @return boolean TRUE if the passed path is the actual one |
85
|
|
|
*/ |
86
|
|
|
protected function isLastPath($path) |
87
|
|
|
{ |
88
|
|
|
return $this->getSubject()->getLastPath() === $path; |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Return's the entity ID for the passed path. |
93
|
|
|
* |
94
|
|
|
* @param string $path The path to return the entity ID for |
95
|
|
|
* |
96
|
|
|
* @return integer The mapped entity ID |
97
|
|
|
* @throws \Exception Is thrown, if the path can not be mapped |
98
|
|
|
*/ |
99
|
|
|
protected function mapPath($path) |
100
|
|
|
{ |
101
|
|
|
return $this->mapPathEntityId($path); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return's the entity ID for the passed path. |
106
|
|
|
* |
107
|
|
|
* @param string $path The path to return the entity ID for |
108
|
|
|
* |
109
|
|
|
* @return integer The mapped entity ID |
110
|
|
|
* @throws \Exception Is thrown, if the path can not be mapped |
111
|
|
|
*/ |
112
|
|
|
protected function mapPathEntityId($path) |
113
|
|
|
{ |
114
|
|
|
return $this->getSubject()->mapPathEntityId($path); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Add the passed path => entity ID mapping. |
119
|
|
|
* |
120
|
|
|
* @param string $path The path |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
protected function addPathEntityIdMapping($path) |
125
|
|
|
{ |
126
|
|
|
$this->getSubject()->addPathEntityIdMapping($path); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Removes the mapping, e. g. when a category has been deleted. |
131
|
|
|
* |
132
|
|
|
* @param string $path The path to delete the mapping for |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
|
|
protected function removePathEntityIdMapping($path) |
137
|
|
|
{ |
138
|
|
|
$this->getSubject()->removePathEntityIdMapping($path); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Find the path from mapping when only entity ID exists |
143
|
|
|
* |
144
|
|
|
* @param string $id The entoty_id to find in mapping |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
protected function findPathFromEntityIdMapping($id) |
149
|
|
|
{ |
150
|
|
|
return $this->getSubject()->findPathfromEntityIdMapping($id); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Add the passed path => store view code mapping. |
155
|
|
|
* |
156
|
|
|
* @param string $path The path |
157
|
|
|
* @param string $storeViewCode The store view code |
158
|
|
|
* |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
protected function addPathStoreViewCodeMapping($path, $storeViewCode) |
162
|
|
|
{ |
163
|
|
|
$this->getSubject()->addPathStoreViewCodeMapping($path, $storeViewCode); |
|
|
|
|
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: