From dd2eba60f611d55e3e6d5a039c84505b2bf2828d Mon Sep 17 00:00:00 2001 From: Aivaras Gotovskis Date: Mon, 19 Jan 2015 15:44:18 +0200 Subject: [PATCH 1/3] Fixed Tautrimas comments and added basic pipeline documentation. --- .../AbstractConsumeEventListener.php | 4 +- Pipeline/Event/ItemPipelineEvent.php | 12 +++--- Pipeline/ItemSkipException.php | 7 +++- Pipeline/Pipeline.php | 2 +- Resources/doc/pipeline.rst | 40 ++++++++++++++++++ Tests/Unit/Pipeline/PipelineTest.php | 41 ++++++++++--------- Tests/Unit/Pipeline/PipelineTestConsumer.php | 4 +- 7 files changed, 79 insertions(+), 31 deletions(-) create mode 100644 Resources/doc/pipeline.rst diff --git a/EventListener/AbstractConsumeEventListener.php b/EventListener/AbstractConsumeEventListener.php index 81e8009..c3cd6ca 100644 --- a/EventListener/AbstractConsumeEventListener.php +++ b/EventListener/AbstractConsumeEventListener.php @@ -14,7 +14,7 @@ use ONGR\ConnectionsBundle\Pipeline\Event\ItemPipelineEvent; /** - * AbstractConsumeEventListener class. + * Handles basic item skipping when there is nothing to do on skip. */ abstract class AbstractConsumeEventListener { @@ -25,7 +25,7 @@ abstract class AbstractConsumeEventListener */ public function onConsume(ItemPipelineEvent $event) { - if ($event->getItemSkipException()) { + if ($event->getSkipException()) { $this->skip($event); } else { $this->consume($event); diff --git a/Pipeline/Event/ItemPipelineEvent.php b/Pipeline/Event/ItemPipelineEvent.php index 12e86cf..0d77ef0 100644 --- a/Pipeline/Event/ItemPipelineEvent.php +++ b/Pipeline/Event/ItemPipelineEvent.php @@ -34,7 +34,7 @@ class ItemPipelineEvent extends Event /** * @var ItemSkipException */ - private $itemSkipException; + private $skipException; /** * @param mixed $item @@ -79,19 +79,19 @@ public function setOutput($output) /** * @return ItemSkipException */ - public function getItemSkipException() + public function getSkipException() { - return $this->itemSkipException; + return $this->skipException; } /** - * @param ItemSkipException $itemSkipException + * @param ItemSkipException $skipException * * @return $this */ - public function setItemSkipException(ItemSkipException $itemSkipException) + public function setSkipException(ItemSkipException $skipException) { - $this->itemSkipException = $itemSkipException; + $this->skipException = $skipException; return $this; } diff --git a/Pipeline/ItemSkipException.php b/Pipeline/ItemSkipException.php index 4246c73..e489bf6 100644 --- a/Pipeline/ItemSkipException.php +++ b/Pipeline/ItemSkipException.php @@ -12,7 +12,12 @@ namespace ONGR\ConnectionsBundle\Pipeline; /** - * Skip Item Exception. + * Exception for skipping items. + * + * This exception (or exception derived from this) + * should be thrown inside pipeline modifier + * to indicate that this item should be skipped. + * Thrown exception will be available for consumers. */ class ItemSkipException extends \Exception { diff --git a/Pipeline/Pipeline.php b/Pipeline/Pipeline.php index 3cec1b0..7dca6d2 100644 --- a/Pipeline/Pipeline.php +++ b/Pipeline/Pipeline.php @@ -75,7 +75,7 @@ public function start() $itemEvent ); } catch (ItemSkipException $itemSkipException) { - $itemEvent->setItemSkipException($itemSkipException); + $itemEvent->setSkipException($itemSkipException); } $dispatcher->dispatch( diff --git a/Resources/doc/pipeline.rst b/Resources/doc/pipeline.rst new file mode 100644 index 0000000..3535e54 --- /dev/null +++ b/Resources/doc/pipeline.rst @@ -0,0 +1,40 @@ +Pipeline +======== + +Pipeline is used to process data with 5 events: + + - source + - start + - modify + - consume + - finish + +Pipeline starts with source event which provides all data which should be processed. +Then start event is fired to indicate that items are about to come +after that pipeline iterates through all items from all sources by calling modify and consume events +with each item. After the iteration finish event is fired to notify that no more items will follow. + +Pipeline can have any number of listeners for each event but for functioning pipeline +at least one source and consume listener should be provided. + +Listeners should listen to ongr.pipeline... events. +Example: + +.. code-block:: yaml + + test.import.modifier: + class: %ongr_connections.import.modifier.class% + tags: + - { name: kernel.event_listener, event: ongr.pipeline.import.default.modify, method: onModify } +.. + +Item skipping +------------- +If item for some reason should not be consumed without stopping pipeline ItemSkipException can be used. + +When modifier throws `ItemSkipException` pipeline catches it and sets skipException in `ItemPipelineEvent`. +If `AbstractConsumeEventListener` is used `skip` method will called if skipException exception set otherwise `consume` +method is called. + + + diff --git a/Tests/Unit/Pipeline/PipelineTest.php b/Tests/Unit/Pipeline/PipelineTest.php index fa6533b..7ac6d88 100644 --- a/Tests/Unit/Pipeline/PipelineTest.php +++ b/Tests/Unit/Pipeline/PipelineTest.php @@ -17,11 +17,30 @@ use ONGR\ConnectionsBundle\Pipeline\PipelineFactory; use Symfony\Component\EventDispatcher\EventDispatcher; -/** - * PipelineTest class. - */ class PipelineTest extends \PHPUnit_Framework_TestCase { + /** + * Pipeline data provider. + * + * @return array + */ + public function pipelineData() + { + // Modifier skips items which are string 'skip', anything else gets consumed. + return [ + // Case #0: No data so then should be 0 consumed and skipped items. + [[], 0, 0], + // Case #1: All data should be consumed so 1 consume and 0 skips. + [['consume'], 1, 0], + // Case #2: All data should be skipped so 0 consumes and 1 skip. + [['skip'], 0, 1], + // Case #3: Data with consume and skips. 1 consume and 2 skips. + [['skip', 'consume', 'skip'], 1, 2], + // Case #3: Data with consumes and skip. 2 consumes and 1 skip. + [['consume', 'skip', 'consume'], 2, 1], + ]; + } + /** * Tests pipeline. * @@ -70,20 +89,4 @@ public function onModify(ItemPipelineEvent $event) throw new ItemSkipException(); } } - - /** - * Pipeline data provider. - * - * @return array - */ - public function pipelineData() - { - return [ - [[], 0, 0], - [['consume'], 1, 0], - [['skip'], 0, 1], - [['skip', 'consume', 'skip'], 1, 2], - [['consume', 'skip', 'consume'], 2, 1], - ]; - } } diff --git a/Tests/Unit/Pipeline/PipelineTestConsumer.php b/Tests/Unit/Pipeline/PipelineTestConsumer.php index f269e05..72487fe 100644 --- a/Tests/Unit/Pipeline/PipelineTestConsumer.php +++ b/Tests/Unit/Pipeline/PipelineTestConsumer.php @@ -34,7 +34,7 @@ class PipelineTestConsumer extends AbstractConsumeEventListener */ public function consume(ItemPipelineEvent $event) { - ++$this->consumeCalled; + $this->consumeCalled++; } /** @@ -42,7 +42,7 @@ public function consume(ItemPipelineEvent $event) */ public function skip(ItemPipelineEvent $event) { - ++$this->skipCalled; + $this->skipCalled++; } /** From d3ab2fecc35cc695a1ef98f7be48816094a6b8fb Mon Sep 17 00:00:00 2001 From: Aivaras Gotovskis Date: Mon, 19 Jan 2015 17:51:27 +0200 Subject: [PATCH 2/3] Improved comments. --- .../AbstractConsumeEventListener.php | 2 +- Resources/doc/pipeline.rst | 19 ++++++++++--------- Tests/Unit/Pipeline/PipelineTest.php | 10 +++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/EventListener/AbstractConsumeEventListener.php b/EventListener/AbstractConsumeEventListener.php index c3cd6ca..348f198 100644 --- a/EventListener/AbstractConsumeEventListener.php +++ b/EventListener/AbstractConsumeEventListener.php @@ -14,7 +14,7 @@ use ONGR\ConnectionsBundle\Pipeline\Event\ItemPipelineEvent; /** - * Handles basic item skipping when there is nothing to do on skip. + * Provides functionality for skipping pipeline items. */ abstract class AbstractConsumeEventListener { diff --git a/Resources/doc/pipeline.rst b/Resources/doc/pipeline.rst index 3535e54..7a1a645 100644 --- a/Resources/doc/pipeline.rst +++ b/Resources/doc/pipeline.rst @@ -10,9 +10,10 @@ Pipeline is used to process data with 5 events: - finish Pipeline starts with source event which provides all data which should be processed. -Then start event is fired to indicate that items are about to come -after that pipeline iterates through all items from all sources by calling modify and consume events -with each item. After the iteration finish event is fired to notify that no more items will follow. +Then start event is fired to indicate that items are about to come, +after that pipeline iterates through all items from all sources, calling modify and consume events +with each item inside ``ItemPipelineEvent``. After the iterations are finished, finish event is fired +to notify that no more items will follow. Pipeline can have any number of listeners for each event but for functioning pipeline at least one source and consume listener should be provided. @@ -30,11 +31,11 @@ Example: Item skipping ------------- -If item for some reason should not be consumed without stopping pipeline ItemSkipException can be used. - -When modifier throws `ItemSkipException` pipeline catches it and sets skipException in `ItemPipelineEvent`. -If `AbstractConsumeEventListener` is used `skip` method will called if skipException exception set otherwise `consume` -method is called. - +If item for some reason should be skipped without stopping pipeline, ItemSkipException can be used. +When ``ItemSkipException`` is thrown by the modifier, pipeline catches it and sets skipException +value in ``ItemPipelineEvent``. +When modifier throws ``ItemSkipException`` pipeline catches it and sets skipException in ``ItemPipelineEvent``. +If ``AbstractConsumeEventListener`` is used and ``skipException`` exception is set, ``skip`` method will be called. +Otherwise ``consume`` will be invoked. diff --git a/Tests/Unit/Pipeline/PipelineTest.php b/Tests/Unit/Pipeline/PipelineTest.php index 7ac6d88..4944186 100644 --- a/Tests/Unit/Pipeline/PipelineTest.php +++ b/Tests/Unit/Pipeline/PipelineTest.php @@ -26,17 +26,17 @@ class PipelineTest extends \PHPUnit_Framework_TestCase */ public function pipelineData() { - // Modifier skips items which are string 'skip', anything else gets consumed. + // Modifier skips items which are equal to 'skip', anything else gets consumed. return [ - // Case #0: No data so then should be 0 consumed and skipped items. + // Case #0: No data. Results should be: 0 consumed and skipped items. [[], 0, 0], - // Case #1: All data should be consumed so 1 consume and 0 skips. + // Case #1: All data should be consumed, so 1 consume and 0 skips. [['consume'], 1, 0], - // Case #2: All data should be skipped so 0 consumes and 1 skip. + // Case #2: All data should be skipped, so 0 consumes and 1 skip. [['skip'], 0, 1], // Case #3: Data with consume and skips. 1 consume and 2 skips. [['skip', 'consume', 'skip'], 1, 2], - // Case #3: Data with consumes and skip. 2 consumes and 1 skip. + // Case #4: Data with consumes and skip. 2 consumes and 1 skip. [['consume', 'skip', 'consume'], 2, 1], ]; } From e1343f1096a8c830c56bba9052ea533e789f38c5 Mon Sep 17 00:00:00 2001 From: Aivaras Gotovskis Date: Tue, 20 Jan 2015 13:50:05 +0200 Subject: [PATCH 3/3] Removed duplicated text --- Resources/doc/pipeline.rst | 3 --- 1 file changed, 3 deletions(-) diff --git a/Resources/doc/pipeline.rst b/Resources/doc/pipeline.rst index 7a1a645..9196796 100644 --- a/Resources/doc/pipeline.rst +++ b/Resources/doc/pipeline.rst @@ -33,9 +33,6 @@ Item skipping ------------- If item for some reason should be skipped without stopping pipeline, ItemSkipException can be used. -When ``ItemSkipException`` is thrown by the modifier, pipeline catches it and sets skipException -value in ``ItemPipelineEvent``. - When modifier throws ``ItemSkipException`` pipeline catches it and sets skipException in ``ItemPipelineEvent``. If ``AbstractConsumeEventListener`` is used and ``skipException`` exception is set, ``skip`` method will be called. Otherwise ``consume`` will be invoked.