1 | <?php |
||
26 | class Importer implements EventSubscriberInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var Import |
||
30 | */ |
||
31 | protected $import; |
||
32 | |||
33 | /** |
||
34 | * @var HandlerInterface |
||
35 | */ |
||
36 | protected $handler; |
||
37 | |||
38 | /** |
||
39 | * @var EventDispatcherInterface |
||
40 | */ |
||
41 | protected $eventDispatcher; |
||
42 | |||
43 | /** |
||
44 | * @var ImportResult |
||
45 | */ |
||
46 | protected $result; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | protected $batchSize = 20; |
||
52 | |||
53 | /** |
||
54 | * @param Import $import |
||
55 | * @param HandlerInterface $handler |
||
56 | * @param EventDispatcherInterface $dispatcher |
||
57 | * @param int $batchSize |
||
58 | */ |
||
59 | 6 | public function __construct(Import $import, HandlerInterface $handler, EventDispatcherInterface $dispatcher, $batchSize = 20) |
|
69 | |||
70 | /** |
||
71 | * @return Import |
||
72 | */ |
||
73 | 4 | public function getImport() |
|
77 | |||
78 | /** |
||
79 | * @return ImportResult |
||
80 | */ |
||
81 | 4 | public function getResult() |
|
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | */ |
||
89 | 4 | public static function getSubscribedEvents() |
|
90 | { |
||
91 | return [ |
||
92 | 4 | FeedEvents::ITEM_FILTERED => [['onItemFiltered']], |
|
93 | FeedEvents::ITEM_INVALID => [['onItemInvalid']], |
||
94 | FeedEvents::ITEM_FAILED => [['onItemFailed']], |
||
95 | ]; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return EventDispatcherInterface |
||
100 | */ |
||
101 | public function getEventDispatcher() |
||
105 | |||
106 | /** |
||
107 | * @param int $size |
||
108 | * |
||
109 | * @throws \InvalidArgumentException When size is not a positive number |
||
110 | */ |
||
111 | 6 | public function setBatchSize($size) |
|
121 | |||
122 | /** |
||
123 | * Dispatched when item is filtered from the feed. |
||
124 | * |
||
125 | * @param ItemNotModifiedEvent $event |
||
126 | */ |
||
127 | 4 | public function onItemFiltered(ItemNotModifiedEvent $event) |
|
131 | |||
132 | /** |
||
133 | * Dispatched when item failed during modification. |
||
134 | * |
||
135 | * @param ItemNotModifiedEvent $event |
||
136 | */ |
||
137 | public function onItemFailed(ItemNotModifiedEvent $event) |
||
141 | |||
142 | /** |
||
143 | * Dispatched when item is processed but found invalid. |
||
144 | * |
||
145 | * @param InvalidItemEvent $event |
||
146 | */ |
||
147 | public function onItemInvalid(InvalidItemEvent $event) |
||
151 | |||
152 | /** |
||
153 | * @param string $name |
||
154 | * @param Event $event |
||
155 | */ |
||
156 | 4 | public function dispatchEvent($name, Event $event) |
|
160 | |||
161 | /** |
||
162 | * Runs the import. |
||
163 | * |
||
164 | * @param Feed $feed |
||
165 | */ |
||
166 | 4 | public function run(Feed $feed) |
|
167 | { |
||
168 | 4 | while ($item = $this->getNextItem($feed)) { |
|
169 | // dispatch event for next item |
||
170 | 4 | $event = new ItemEvent($this, $item); |
|
171 | 4 | $this->eventDispatcher->dispatch(ImportEvents::ITEM_START, $event); |
|
172 | // import the item |
||
173 | try { |
||
174 | 4 | $source = $this->handleItem($item); |
|
175 | |||
176 | 4 | $this->successItem($item, $source); |
|
177 | 4 | } catch (FailedItemException $e) { |
|
178 | 4 | $this->failItem($item, $e->getMessage()); |
|
179 | } |
||
180 | |||
181 | // item done |
||
182 | 4 | $this->eventDispatcher->dispatch(ImportEvents::ITEM_FINISH, $event); |
|
183 | |||
184 | // clear entitymanager after batch |
||
185 | 4 | if (($this->result->getProcessed() % $this->batchSize) === 0) { |
|
186 | $this->flush(); |
||
187 | $this->clear(); |
||
188 | } |
||
189 | } |
||
190 | |||
191 | // flush remaining changes |
||
192 | 4 | $this->flush(); |
|
193 | 4 | $this->clear(); |
|
194 | 4 | } |
|
195 | |||
196 | /** |
||
197 | * Returns the next item in the feed, or null if no more items are left. |
||
198 | * Use this when iterating over the feed. |
||
199 | * |
||
200 | * @param Feed $feed |
||
201 | * |
||
202 | * @return FeedItemBag|null |
||
203 | */ |
||
204 | 4 | protected function getNextItem(Feed $feed) |
|
214 | |||
215 | /** |
||
216 | * @param FeedItemBag $item |
||
217 | * |
||
218 | * @return SourceInterface |
||
219 | */ |
||
220 | 4 | protected function handleItem(FeedItemBag $item) |
|
221 | { |
||
222 | 4 | $source = $this->handler->handle($item); |
|
223 | |||
224 | 4 | $this->eventDispatcher->dispatch( |
|
225 | 4 | ImportEvents::ITEM_HANDLED, |
|
226 | 4 | new HandledItemEvent( |
|
227 | 4 | $this, |
|
228 | 4 | $item, |
|
229 | 4 | $source |
|
230 | ) |
||
231 | ); |
||
232 | |||
233 | 4 | return $source; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Dispatches an event indicating a successfully handled item. |
||
238 | * |
||
239 | * @param FeedItemBag $item |
||
240 | * @param SourceInterface $result |
||
241 | */ |
||
242 | 4 | protected function successItem(FeedItemBag $item, SourceInterface $result) |
|
249 | |||
250 | /** |
||
251 | * Dispatches an event indicating a skipped item. |
||
252 | * |
||
253 | * @param FeedItemBag $item |
||
254 | * @param string $reason |
||
255 | */ |
||
256 | 4 | protected function skipItem(FeedItemBag $item, $reason = '') |
|
263 | |||
264 | /** |
||
265 | * Dispatches an event indicating a failed item. |
||
266 | * |
||
267 | * @param FeedItemBag $item |
||
268 | * @param string $reason |
||
269 | */ |
||
270 | 4 | protected function failItem(FeedItemBag $item, $reason) |
|
277 | |||
278 | /** |
||
279 | * Dispatches exception event. |
||
280 | * |
||
281 | * @param \Exception $exception |
||
282 | */ |
||
283 | protected function handleException(\Exception $exception) |
||
287 | |||
288 | /** |
||
289 | * Flushes outstanding changes. |
||
290 | */ |
||
291 | 4 | protected function flush() |
|
295 | |||
296 | 4 | protected function clear() |
|
300 | } |
||
301 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.