Skip to content

Commit

Permalink
refactoring calculators, fix tests, add new events, fix few bugs, rem…
Browse files Browse the repository at this point in the history
…ove collections
  • Loading branch information
moriony committed Nov 17, 2015
1 parent c398e97 commit 3de85d1
Show file tree
Hide file tree
Showing 26 changed files with 813 additions and 489 deletions.
8 changes: 5 additions & 3 deletions examples/calculate_asendia_pmi_shipping.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use EsteIt\ShippingCalculator\Calculator\AsendiaCalculator;
use EsteIt\ShippingCalculator\Calculator\BaseCalculator;
use EsteIt\ShippingCalculator\CalculatorHandler\AsendiaCalculatorHandler;
use EsteIt\ShippingCalculator\Model\Weight;
use EsteIt\ShippingCalculator\Model\Dimensions;
use EsteIt\ShippingCalculator\Model\Address;
Expand All @@ -10,8 +11,9 @@

$config = include __DIR__.'/../src/Resources/Asendia/PMEI/tariff_2015_06_15.php';


$calculator = AsendiaCalculator::create($config);
$calculator = new BaseCalculator([
'handler' => AsendiaCalculatorHandler::create($config)
]);

$weight = new Weight();
$weight->setValue(10);
Expand Down
7 changes: 5 additions & 2 deletions examples/calculate_dhl_shipping.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use EsteIt\ShippingCalculator\Calculator\DhlCalculator;
use EsteIt\ShippingCalculator\Calculator\BaseCalculator;
use EsteIt\ShippingCalculator\CalculatorHandler\DhlCalculatorHandler;
use EsteIt\ShippingCalculator\Model\Weight;
use EsteIt\ShippingCalculator\Model\Dimensions;
use EsteIt\ShippingCalculator\Model\Address;
Expand All @@ -10,7 +11,9 @@

$config = include __DIR__.'/../src/Resources/DHL/ExportExpressWorldWide/tariff_2015_08_25_usa.php';

$calculator = DhlCalculator::create($config);
$calculator = new BaseCalculator([
'handler' => DhlCalculatorHandler::create($config)
]);

$weight = new Weight();
$weight->setValue(10);
Expand Down
47 changes: 0 additions & 47 deletions examples/calculator_collection.php

This file was deleted.

36 changes: 17 additions & 19 deletions examples/selective_calculation_by_date.php
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
<?php

use EsteIt\ShippingCalculator\Calculator\AsendiaCalculator;
use EsteIt\ShippingCalculator\CalculatorHandler\AsendiaCalculatorHandler;;
use EsteIt\ShippingCalculator\CalculatorHandler\DhlCalculatorHandler;
use EsteIt\ShippingCalculator\Model\Weight;
use EsteIt\ShippingCalculator\Model\Dimensions;
use EsteIt\ShippingCalculator\Model\Address;
use EsteIt\ShippingCalculator\Model\Package;
use EsteIt\ShippingCalculator\Collection\CalculatorCollection;
use EsteIt\ShippingCalculator\Calculator\SelectiveCalculator;
use EsteIt\ShippingCalculator\Calculator\DhlCalculator;
use EsteIt\ShippingCalculator\Model\PackageInterface;
use EsteIt\ShippingCalculator\Calculator\CalculatorInterface;
use EsteIt\ShippingCalculator\CalculatorHandler\CalculatorHandlerInterface;

include_once __DIR__.'/../vendor/autoload.php';

$config1 = include __DIR__.'/../src/Resources/Asendia/PMI/tariff_2015_06_15.php';
$config2 = include __DIR__.'/../src/Resources/DHL/ExportExpressWorldWide/tariff_2015_08_25_usa.php';

$collection = new CalculatorCollection([
AsendiaCalculator::create($config1),
DhlCalculator::create($config2)
]);

// This \Closure will find actual calculator by date from `extra_data`
$selector = function (CalculatorCollection $calculators, PackageInterface $package) {
/** @var CalculatorInterface $currentCalculator */
$currentCalculator = null;
// This \Closure will find actual handler by date from handler's extra data
$selector = function ($handlers, PackageInterface $package) {
/** @var CalculatorInterface $currentHandler */
$currentHandler = null;
$currentDate = null;

/** @var CalculatorInterface $calculator */
foreach ($calculators as $calculator) {
$extraData = $calculator->getExtraData();
/** @var CalculatorHandlerInterface $handler */
foreach ($handlers as $handler) {
$extraData = $handler->get('extra_data');
$date = new \DateTime($extraData['date']);
if ($package->getCalculationDate() >= $date && (is_null($currentCalculator) || $date > $currentDate)) {
$currentCalculator = $calculator;
if ($package->getCalculationDate() >= $date && (is_null($currentHandler) || $date > $currentDate)) {
$currentHandler = $handler;
}
}
return $currentCalculator;
return $currentHandler;
};

$calculator = new SelectiveCalculator([
'calculators' => $collection,
'handlers' => [
AsendiaCalculatorHandler::create($config1),
DhlCalculatorHandler::create($config2)
],
'selector' => $selector
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace EsteIt\ShippingCalculator\Calculator;

use EsteIt\ShippingCalculator\CalculatorHandler\CalculatorHandlerInterface;
use EsteIt\ShippingCalculator\Event\AfterHandleEvent;
use EsteIt\ShippingCalculator\Event\BeforeHandleEvent;
use EsteIt\ShippingCalculator\Exception\InvalidArgumentException;
use EsteIt\ShippingCalculator\Model\CalculationResultInterface;
use EsteIt\ShippingCalculator\Event\AfterCalculateEvent;
Expand All @@ -11,26 +14,51 @@
use EsteIt\ShippingCalculator\Exception\BasicExceptionInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

abstract class AbstractCalculator implements CalculatorInterface
class BaseCalculator implements CalculatorInterface
{
const RESULT_INTERFACE = 'EsteIt\ShippingCalculator\Model\CalculationResultInterface';

/**
* @var EventDispatcherInterface
*/
protected $dispatcher;

/**
* @var array
* @var CalculatorHandlerInterface
*/
protected $options;
protected $handler;

/**
* @var string
*/
protected $resultClass;

const RESULT_INTERFACE = 'EsteIt\ShippingCalculator\Model\CalculationResultInterface';
const DEFAULT_RESULT_CLASS = 'EsteIt\ShippingCalculator\Model\CalculationResult';
public function __construct(array $options)
{
$resolver = new OptionsResolver();
$resolver
->setDefaults([
'dispatcher' => new EventDispatcher(),
'result_class' => 'EsteIt\ShippingCalculator\Model\CalculationResult',
])
->setRequired([
'dispatcher',
'handler',
])
->setAllowedTypes([
'dispatcher' => 'Symfony\Component\EventDispatcher\EventDispatcherInterface',
'result_class' => 'string',
'handler' => 'EsteIt\ShippingCalculator\CalculatorHandler\CalculatorHandlerInterface',
]);

$options = $resolver->resolve($options);

$this->setDispatcher($options['dispatcher']);
$this->setResultClass($options['result_class']);
$this->setHandler($options['handler']);
}

/**
* @param EventDispatcherInterface $dispatcher
Expand Down Expand Up @@ -72,32 +100,48 @@ public function setResultClass($class)

public function getResultClass()
{
if (!$this->resultClass) {
$this->setResultClass(self::DEFAULT_RESULT_CLASS);
}

return $this->resultClass;
}

/**
* @param CalculatorHandlerInterface $handler
* @return $this
*/
public function setHandler(CalculatorHandlerInterface $handler)
{
$this->handler = $handler;

return $this;
}

/**
* @return CalculatorHandlerInterface
*/
public function getHandler()
{
return $this->handler;
}

/**
* @param PackageInterface $package
* @return CalculationResultInterface
*/
final public function calculate(PackageInterface $package)
public function calculate(PackageInterface $package)
{
$this->getDispatcher()->dispatch(Events::BEFORE_CALCULATE, new BeforeCalculateEvent($this, $package));

$result = $this->createResult();
$result->setPackage($package);
$result->setCalculator($this);

try {
$this->visit($result, $package);
$this->getDispatcher()->dispatch(Events::BEFORE_HANDLE, new BeforeHandleEvent($this, $this->getHandler(), $package));
$this->getHandler()->visit($result, $package);
$this->getDispatcher()->dispatch(Events::AFTER_HANDLE, new AfterHandleEvent($this, $this->getHandler(), $result));
} catch (BasicExceptionInterface $e) {
$result->setError($e);
}

$this->getDispatcher()->dispatch(Events::AFTER_CALCULATE, new AfterCalculateEvent($result));
$this->getDispatcher()->dispatch(Events::AFTER_CALCULATE, new AfterCalculateEvent($this, $result));

return $result;
}
Expand All @@ -110,28 +154,4 @@ protected function createResult()
$resultClass = $this->getResultClass();
return new $resultClass;
}

/**
* @param CalculationResultInterface $result
* @param PackageInterface $package
* @return int|float|string
*/
abstract public function visit(CalculationResultInterface $result, PackageInterface $package);

/**
* @return mixed
*/
public function getExtraData()
{
return null;
}

/**
* @param mixed $name
* @return mixed null
*/
public function getOption($name)
{
return $this->options && array_key_exists($name, $this->options) ? $this->options[$name] : null;
}
}
12 changes: 0 additions & 12 deletions src/Calculator/CalculatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,4 @@ interface CalculatorInterface
* @return CalculationResultInterface
*/
public function calculate(PackageInterface $package);

/**
* @return mixed
*/
public function getExtraData();

/**
* @param CalculationResultInterface $result
* @param PackageInterface $package
* @return void
*/
public function visit(CalculationResultInterface $result, PackageInterface $package);
}
Loading

0 comments on commit 3de85d1

Please sign in to comment.