Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Exception/StopException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Ddeboer\DataImport\Exception;

use Ddeboer\DataImport\Exception;

class StopException extends \RuntimeException implements Exception
{

}
13 changes: 12 additions & 1 deletion src/Filter/OffsetFilter.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace Ddeboer\DataImport\Filter;
use Ddeboer\DataImport\Exception\StopException;

/**
* This filter can be used to filter out some items from the beginning and/or
Expand Down Expand Up @@ -35,14 +36,20 @@ class OffsetFilter
*/
protected $maxLimitHit = false;

/**
* @var boolean
*/
protected $stopOnMaxLimit = false;

/**
* @param integer $offset 0-based index of the item to start read from
* @param integer|null $limit Maximum count of items to read. null = no limit
*/
public function __construct($offset = 0, $limit = null)
public function __construct($offset = 0, $limit = null, $stopOnLimit = false)
{
$this->offset = $offset;
$this->limit = $limit;
$this->stopOnMaxLimit = ($limit>0 && $stopOnLimit);
}

/**
Expand All @@ -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;
}

Expand Down
30 changes: 25 additions & 5 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class Result
*/
protected $totalProcessedCount = 0;

/**
* @var integer
*/
protected $skippedCount = 0;

/**
* @var \SplObjectStorage
*/
Expand All @@ -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;
}

Expand Down Expand Up @@ -128,6 +140,14 @@ public function getTotalProcessedCount()
return $this->totalProcessedCount;
}

/**
* @return int
*/
public function getSkippedCount()
{
return $this->skippedCount;
}

/**
* @return boolean
*/
Expand Down
19 changes: 16 additions & 3 deletions src/Workflow/StepAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -131,9 +135,12 @@ public function process()
break;
}

$processed++;

try {
foreach (clone $this->steps as $step) {
if (false === $step->process($item)) {
$skipped++;
continue 2;
}
}
Expand All @@ -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);
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/Filter/OffsetFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 7 additions & 7 deletions tests/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ 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());

$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());
Expand All @@ -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());
Expand All @@ -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());
}

Expand All @@ -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());
}
}