Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof of concept reporter interface #241

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
96 changes: 96 additions & 0 deletions src/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/**
* Created by PhpStorm.
* User: gnat
* Date: 29/07/15
* Time: 10:08 AM
*/

namespace Ddeboer\DataImport;


class Report
{
/**
* @var integer $row
*/
private $row;

/**
* @var array $messages
*/
private $messages;

/**
* @param $row
*/
public function __construct($row)
{
$this->row = $row;
$this->messages = array();
}

/**
* @return integer
*/
public function getRow()
{
return $this->row;
}

/**
* @param integer $row
* @return Report
*/
public function setRow($row)
{
$this->row = $row;
return $this;
}

/**
* @return mixed
*/
public function getMessages($severity = null)
{
if($severity === null) {
return $this->messages;
}

$messages = array();
foreach($this->messages as $message) {
if($message->getSeverity() == $severity) {
$messages[] = $message;
}
}

return $messages;
}

/**
* @param mixed $messages
* @return Report
*/
public function setMessages(array $messages)
{
$this->messages = $messages;
return $this;
}

/**
* @param ReportMessage $message
*/
public function addMessage(ReportMessage $message)
{
$this->messages[] = $message;
}

/**
* @return boolean
*/
public function hasMessages()
{
return (!empty($this->messages));
}
}

86 changes: 86 additions & 0 deletions src/ReportMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Created by PhpStorm.
* User: gnat
* Date: 29/07/15
* Time: 12:01 PM
*/

namespace Ddeboer\DataImport;


class ReportMessage
{
private $column;
private $message;
private $severity;

public function __construct($message, $severity, $column = null)
{
$this->message = $message;
$this->severity = $severity;
$this->column = $column;
}

/**
* @return mixed
*/
public function getColumn()
{
return $this->column;
}

/**
* @param mixed $column
* @return ReportMessage
*/
public function setColumn($column)
{
$this->column = $column;
return $this;
}

/**
* @return mixed
*/
public function getMessage()
{
return $this->message;
}

/**
* @return mixed
*/
public function getSeverity()
{
return $this->severity;
}

/**
* @param integer $severity
* @return ReportMessage
*/
public function setSeverity($severity)
{
$this->severity = $severity;
return $this;
}

/**
* @param mixed $message
* @return ReportMessage
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}

/**
* @return mixed
*/
public function isException()
{
return ($this->message instanceof Exception);
}
}
21 changes: 21 additions & 0 deletions src/ReporterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Created by PhpStorm.
* User: gnat
* Date: 29/07/15
* Time: 12:09 PM
*/

namespace Ddeboer\DataImport;


interface ReporterInterface
{
const INFO = 0;
const WARNING = 1;
const ERROR = 2;

public function hasMessage();
public function getMessage();
public function getSeverity();
}
17 changes: 16 additions & 1 deletion src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,20 @@ class Result
*/
protected $exceptions;

/**
* @var \SplObjectStorage
*/
protected $reports;

/**
* @param string $name
* @param \DateTime $startTime
* @param \DateTime $endTime
* @param integer $totalCount
* @param \SplObjectStorage $exceptions
* @param \SplObjectStorage $reports
*/
public function __construct($name, \DateTime $startTime, \DateTime $endTime, $totalCount, \SplObjectStorage $exceptions)
public function __construct($name, \DateTime $startTime, \DateTime $endTime, $totalCount, \SplObjectStorage $exceptions, \SplObjectStorage $reports = null)
{
$this->name = $name;
$this->startTime = $startTime;
Expand All @@ -70,6 +76,7 @@ public function __construct($name, \DateTime $startTime, \DateTime $endTime, $to
$this->errorCount = count($exceptions);
$this->successCount = $totalCount - $this->errorCount;
$this->exceptions = $exceptions;
$this->reports = $reports;
}

/**
Expand Down Expand Up @@ -143,4 +150,12 @@ public function getExceptions()
{
return $this->exceptions;
}

/**
* @return \SplObjectStorage
*/
public function getReports()
{
return $this->reports;
}
}
2 changes: 1 addition & 1 deletion src/Step.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface Step
*
* @return boolean False return value means the item should be skipped
*/
public function process(&$item);
public function process(&$item, Report $report = null);
}
9 changes: 8 additions & 1 deletion src/Step/ConverterStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Ddeboer\DataImport\Step;

use Ddeboer\DataImport\Report;
use Ddeboer\DataImport\Step;
use Ddeboer\DataImport\Exception\UnexpectedTypeException;
use Ddeboer\DataImport\ReporterInterface;
use Ddeboer\DataImport\ReportMessage;

/**
* @author Markus Bachmann <[email protected]>
Expand Down Expand Up @@ -40,10 +43,14 @@ public function add(callable $converter)
/**
* {@inheritdoc}
*/
public function process(&$item)
public function process(&$item, Report $report = null)
{
foreach ($this->converters as $converter) {
$item = call_user_func($converter, $item);

if($report !== null && $converter instanceof ReporterInterface && $converter->hasMessage()) {
$report->addMessage(new ReportMessage($converter->getMessage(),$converter->getSeverity()));
}
}

return true;
Expand Down
10 changes: 9 additions & 1 deletion src/Step/FilterStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Ddeboer\DataImport\Step;

use Ddeboer\DataImport\Report;
use Ddeboer\DataImport\Step;
use Ddeboer\DataImport\ReporterInterface;
use Ddeboer\DataImport\ReportMessage;

/**
* @author Markus Bachmann <[email protected]>
Expand Down Expand Up @@ -35,14 +38,19 @@ public function add(callable $filter, $priority = null)
/**
* {@inheritdoc}
*/
public function process(&$item)
public function process(&$item, Report $report = null)
{
foreach (clone $this->filters as $filter) {
if (false === call_user_func($filter, $item)) {
if($report !== null && $filter instanceof ReporterInterface && $filter->hasMessage()) {
$report->addMessage(new ReportMessage($filter->getMessage(),$filter->getSeverity()));
}

return false;
}
}

return true;
}
}

3 changes: 2 additions & 1 deletion src/Step/MappingStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ddeboer\DataImport\Step;

use Ddeboer\DataImport\Exception\MappingException;
use Ddeboer\DataImport\Report;
use Ddeboer\DataImport\Step;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function map($from, $to)
*
* @throws MappingException
*/
public function process(&$item)
public function process(&$item, Report $report = null)
{
try {
foreach ($this->mappings as $from => $to) {
Expand Down
3 changes: 2 additions & 1 deletion src/Step/ValidatorStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ddeboer\DataImport\Step;

use Ddeboer\DataImport\Report;
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
Expand Down Expand Up @@ -81,7 +82,7 @@ public function getViolations()
/**
* {@inheritdoc}
*/
public function process(&$item)
public function process(&$item, Report $report = null)
{
$constraints = new Constraints\Collection($this->constraints);
$list = $this->validator->validateValue($item, $constraints);
Expand Down
8 changes: 7 additions & 1 deletion src/Step/ValueConverterStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Ddeboer\DataImport\Step;

use Ddeboer\DataImport\Report;
use Ddeboer\DataImport\ReporterInterface;
use Ddeboer\DataImport\ReportMessage;
use Ddeboer\DataImport\Step;
use Symfony\Component\PropertyAccess\PropertyAccessor;

Expand Down Expand Up @@ -31,7 +34,7 @@ public function add($property, callable $converter)
/**
* {@inheritdoc}
*/
public function process(&$item)
public function process(&$item, Report $report = null)
{
$accessor = new PropertyAccessor();

Expand All @@ -40,6 +43,9 @@ public function process(&$item)
$orgValue = $accessor->getValue($item, $property);
$value = call_user_func($converter, $orgValue);
$accessor->setValue($item,$property,$value);
if($report !== null && $converter instanceof ReporterInterface && $converter->hasMessage()) {
$report->addMessage(new ReportMessage($converter->getMessage(),$converter->getSeverity(),$property));
}
}
}

Expand Down
Loading