diff --git a/src/Exception/StopException.php b/src/Exception/StopException.php new file mode 100644 index 00000000..7518206b --- /dev/null +++ b/src/Exception/StopException.php @@ -0,0 +1,10 @@ +offset = $offset; $this->limit = $limit; + $this->stopOnMaxLimit = ($limit>0 && $stopOnLimit); } /** @@ -52,6 +59,10 @@ public function __invoke(array $item) { // In case we've already filtered up to limited if ($this->maxLimitHit) { + if($this->stopOnMaxLimit) { + throw new StopException(); + } + return false; } diff --git a/src/Result.php b/src/Result.php index dc9e7426..48f9b41c 100644 --- a/src/Result.php +++ b/src/Result.php @@ -48,6 +48,11 @@ class Result */ protected $totalProcessedCount = 0; + /** + * @var integer + */ + protected $skippedCount = 0; + /** * @var \SplObjectStorage */ @@ -57,18 +62,25 @@ class Result * @param string $name * @param \DateTime $startTime * @param \DateTime $endTime - * @param integer $totalCount + * @param integer $processed + * @param integer $imported + * @param integer $skipped + * @param integer $errors * @param \SplObjectStorage $exceptions */ - public function __construct($name, \DateTime $startTime, \DateTime $endTime, $totalCount, \SplObjectStorage $exceptions) + public function __construct($name, \DateTime $startTime, \DateTime $endTime, $processed, $imported, $skipped, $errors, \SplObjectStorage $exceptions) { $this->name = $name; $this->startTime = $startTime; $this->endTime = $endTime; $this->elapsed = $startTime->diff($endTime); - $this->totalProcessedCount = $totalCount; - $this->errorCount = count($exceptions); - $this->successCount = $totalCount - $this->errorCount; + + //Should expect $processed = $errors+$imported+$skipped + $this->totalProcessedCount = $processed; + $this->errorCount = $errors; + $this->successCount = $imported; + $this->skippedCount = $skipped; + $this->exceptions = $exceptions; } @@ -128,6 +140,14 @@ public function getTotalProcessedCount() return $this->totalProcessedCount; } + /** + * @return int + */ + public function getSkippedCount() + { + return $this->skippedCount; + } + /** * @return boolean */ diff --git a/src/Workflow/StepAggregator.php b/src/Workflow/StepAggregator.php index db319b83..11aeab40 100644 --- a/src/Workflow/StepAggregator.php +++ b/src/Workflow/StepAggregator.php @@ -107,7 +107,11 @@ public function addWriter(Writer $writer) */ public function process() { - $count = 0; + $processed = 0; + $skipped = 0; + $errors = 0; + $imported = 0; + $exceptions = new \SplObjectStorage(); $startTime = new \DateTime; @@ -131,9 +135,12 @@ public function process() break; } + $processed++; + try { foreach (clone $this->steps as $step) { if (false === $step->process($item)) { + $skipped++; continue 2; } } @@ -145,23 +152,29 @@ public function process() foreach ($this->writers as $writer) { $writer->writeItem($item); } + } catch(Exception\StopException $e) { + $processed--; + break; } catch(Exception $e) { if (!$this->skipItemOnFailure) { throw $e; } + $errors++; $exceptions->attach($e, $index); $this->logger->error($e->getMessage()); + + continue; } - $count++; + $imported++; } foreach ($this->writers as $writer) { $writer->finish(); } - return new Result($this->name, $startTime, new \DateTime, $count, $exceptions); + return new Result($this->name, $startTime, new \DateTime, $processed, $imported, $skipped, $errors, $exceptions); } /** diff --git a/tests/Filter/OffsetFilterTest.php b/tests/Filter/OffsetFilterTest.php index 99dda153..9c6bf6d2 100644 --- a/tests/Filter/OffsetFilterTest.php +++ b/tests/Filter/OffsetFilterTest.php @@ -48,4 +48,22 @@ public function testOffsetWithMaxCount() $resultItems = $this->applyFilter(new OffsetFilter(1, 1), $items); $this->assertEquals($resultItems, array('second')); } + + /** + * @expectedException \Ddeboer\DataImport\Exception\StopException + */ + public function testMaxCountStop() + { + $items = array('first','second','third','fourth'); + $this->applyFilter(new OffsetFilter(0, 2, true), $items); + } + + /** + * @expectedException \Ddeboer\DataImport\Exception\StopException + */ + public function testOffsetWithMaxCountStop() + { + $items = array('first','second','third','fourth'); + $this->applyFilter(new OffsetFilter(1, 1, true), $items); + } } diff --git a/tests/ResultTest.php b/tests/ResultTest.php index 73274eaa..792f6d6a 100644 --- a/tests/ResultTest.php +++ b/tests/ResultTest.php @@ -13,13 +13,13 @@ class ResultTest extends \PHPUnit_Framework_TestCase { public function testResultName() { - $result = new Result('export', new \DateTime, new \DateTime, 10, new \SplObjectStorage()); + $result = new Result('export', new \DateTime, new \DateTime, 10, 10, 0, 0, new \SplObjectStorage()); $this->assertSame('export', $result->getName()); } public function testResultCounts() { - $result = new Result('export', new \DateTime, new \DateTime, 10, new \SplObjectStorage()); + $result = new Result('export', new \DateTime, new \DateTime, 10, 10, 0, 0, new \SplObjectStorage()); $this->assertSame(10, $result->getTotalProcessedCount()); $this->assertSame(10, $result->getSuccessCount()); $this->assertSame(0, $result->getErrorCount()); @@ -27,7 +27,7 @@ public function testResultCounts() $exceptions = new \SplObjectStorage(); $exceptions->attach(new \Exception()); $exceptions->attach(new \Exception()); - $result = new Result('export', new \DateTime, new \DateTime, 10, $exceptions); + $result = new Result('export', new \DateTime, new \DateTime, 10, 8, 0, 2, $exceptions); $this->assertSame(10, $result->getTotalProcessedCount()); $this->assertSame(8, $result->getSuccessCount()); $this->assertSame(2, $result->getErrorCount()); @@ -39,7 +39,7 @@ public function testDates() $startDate = new \DateTime("22-07-2014 22:00"); $endDate = new \DateTime("22-07-2014 23:30"); - $result = new Result('export', $startDate, $endDate, 10, new \SplObjectStorage()); + $result = new Result('export', $startDate, $endDate, 10, 10, 0, 0, new \SplObjectStorage()); $this->assertSame($startDate, $result->getStartTime()); $this->assertSame($endDate, $result->getEndTime()); @@ -52,13 +52,13 @@ public function testHasErrorsReturnsTrueIfAnyExceptions() $exceptions->attach(new \Exception()); $exceptions->attach(new \Exception()); - $result = new Result('export', new \DateTime, new \DateTime, 10, $exceptions); + $result = new Result('export', new \DateTime, new \DateTime, 10, 10, 0, 2, $exceptions); $this->assertTrue($result->hasErrors()); } public function testHasErrorsReturnsFalseIfNoExceptions() { - $result = new Result('export', new \DateTime, new \DateTime, 10, new \SplObjectStorage()); + $result = new Result('export', new \DateTime, new \DateTime, 10, 10, 0, 0, new \SplObjectStorage()); $this->assertFalse($result->hasErrors()); } @@ -68,7 +68,7 @@ public function testGetExceptions() $exceptions->attach(new \Exception()); $exceptions->attach(new \Exception()); - $result = new Result('export', new \DateTime, new \DateTime, 10, $exceptions); + $result = new Result('export', new \DateTime, new \DateTime, 10, 10, 0, 2, $exceptions); $this->assertSame($exceptions, $result->getExceptions()); } }