public function testEvent() { $item = new ParameterBag(); $modifier = new CallbackTransformer(function () { }); $exception = new ModificationException(); $event = new FailedItemModificationEvent($item, $modifier, $exception); $this->assertSame($item, $event->getItem()); $this->assertSame($modifier, $event->getModifier()); $this->assertSame($exception, $event->getException()); $this->assertFalse($event->getContinue()); $event->setContinue(true); $this->assertTrue($event->getContinue()); }
/** * @param FailedItemModificationEvent $event */ public function onItemModificationFailure(FailedItemModificationEvent $event) { $this->logger->warning($event->getException()->getMessage()); }
/** * @param ParameterBag $item * * @throws FilterException * @throws ModificationException * @throws ValidationException * * @return ParameterBag */ protected function modify(ParameterBag &$item) { foreach ($this->modifiers as $position => $modifier) { try { if ($modifier instanceof FilterInterface) { $modifier->filter($item); } if ($modifier instanceof MapperInterface) { $item = $modifier->map($item); } if ($modifier instanceof TransformerInterface) { $modifier->transform($item); } if ($modifier instanceof ValidatorInterface) { $modifier->validate($item); } } catch (FilterException $e) { // filter exceptions don't get to continue throw $e; } catch (ValidationException $e) { // validation exceptions don't get to continue throw $e; } catch (ModificationException $e) { // notify listeners of this failure, give them the option to stop propagation $event = new FailedItemModificationEvent($item, $modifier, $e); $event->setContinue($this->continues[$position]); $this->eventDispatcher->dispatch(FeedEvents::ITEM_MODIFICATION_FAILED, $event); if (!$event->getContinue()) { throw $e; } } } return $item; }
/** * @param FailedItemModificationEvent $event */ public function onItemModificationFailure(FailedItemModificationEvent $event) { $this->output->writeln(sprintf('<error>%s</error>', $event->getException()->getMessage())); }
/** * @inheritdoc */ public function parse(ScrapedItemBag $item) { $crawler = $this->getDomCrawler($item->getOriginalData(), $item->getOriginalUrl()); foreach ($this->modifiers as $position => $modifier) { // set crawler if needed if ($modifier instanceof CrawlerAwareInterface) { $modifier->setCrawler($crawler); } try { if ($modifier instanceof FilterInterface) { $modifier->filter($item); } if ($modifier instanceof MapperInterface) { $item = $modifier->map($item); } if ($modifier instanceof TransformerInterface) { $modifier->transform($item); } if ($modifier instanceof ValidatorInterface) { $modifier->validate($item); } } catch (FilterException $e) { // filter exceptions don't get to continue throw $e; } catch (ValidationException $e) { // validation exceptions don't get to continue throw $e; } catch (ModificationException $e) { // notify listeners of this failure, give them the option to stop propagation $event = new FailedItemModificationEvent($item, $modifier, $e); $event->setContinue($this->continues[$position]); $this->eventDispatcher->dispatch(FeedEvents::ITEM_MODIFICATION_FAILED, $event); if (!$event->getContinue()) { throw $e; } } } }