Permalink
Browse files

start refactoring of pricing set processed values and properties

  • Loading branch information...
1 parent e9fdf7d commit 5b326b07bb5c04da8487ad9ad259143f4d24199f @iampersistent iampersistent committed Mar 11, 2013
View
24 lib/Vespolina/Entity/Pricing/Element/TotalDoughValueElement.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Vespolina\Entity\Pricing\Element;
+
+use Vespolina\Entity\Pricing\PricingElement;
+
+class TotalDoughValueElement extends PricingElement
+{
+ protected $position = 100000;
+
+ protected function doProcess($context, $processed)
+ {
+ $totalValue = $processed['netValue']->reduce($processed['discounts']);
+ if (isset($processed['surcharge'])) {
+ $totalValue = $totalValue->plus($processed['surcharge']);
+ }
+ if (isset($processed['taxes'])) {
+ $totalValue = $totalValue->plus($processed['taxes']);
+ }
+ $processed['totalValue'] = $totalValue;
+
+ return $processed;
+ }
+}
View
115 lib/Vespolina/Entity/Pricing/PricingSet.php
@@ -14,6 +14,12 @@
class PricingSet implements PricingSetInterface
{
+ protected $id;
+ protected $context;
+ protected $processedProperties;
+ protected $processedValues;
+ protected $processingState = self::PROCESSING_UNPROCESSED;
+ protected $returns;
protected $pricingElements;
const PROCESSING_UNPROCESSED = 0;
@@ -27,7 +33,7 @@ public function __construct(array $customReturns = array(), array $globalPricing
$this->returns = array_merge($defaultReturns, $customReturns);
foreach ($this->returns as $return) {
- $this->processed[$return] = null;
+ $this->processedValues[$return] = null;
}
$this->addPricingElements($globalPricingElements);
@@ -69,14 +75,87 @@ public function get($name)
throw new \Exception('Accessing unprocessed pricing element ' . $name);
}
- if (isset($this->processed[$name])) {
- return $this->processed[$name];
+ if (isset($this->processedValues[$name])) {
+ return $this->processedValues[$name];
+ }
+
+ if (isset($this->processedProperties[$name])) {
+ return $this->processedProperties[$name];
}
}
public function set($name, $value)
{
- $this->pricingElements[$name] = $value;
+ if (is_object($value)) {
+ $this->processedValues[$name] = $value;
+ }
+ $this->processedProperties[$name] = $value;
+
+ return $this;
+ }
+
+ public function has($name)
+ {
+ return (bool) $this->get($name);
+ }
+
+ public function process($context = null)
+ {
+ // create empty array with keys from $this->processed
+ $processed = array(
+ 'properties' => array(),
+ 'values' => array(),
+ );
+
+ if (count($this->getPricingElements()) !== 0) {
+ /** @var \Vespolina\Entity\Pricing\PricingElementInterface $element */
+ foreach ($this->getPricingElements() as $element) {
+ $processed = array_merge($this->processed, $element->process($context, $processed));
+ }
+ } else {
+ $processed = $this->getProcessed();
+ }
+
+
+ $newSet = new self();
+ $newSet->setProcessedProperties($processed['properties']);
+ $newSet->setProcessedValues($processed['values']);
+ $newSet->setProcessingState(self::PROCESSING_FINISHED);
+ $this->processingState = self::PROCESSING_FINISHED;
+
+ return $newSet;
+ }
+
+ public function plus($addSet)
+ {
+ $newSet = new self();
+ foreach ($this->getProcessed() as $key => $value) {
+ if (!is_scalar($value)) {
+ continue;
+ }
+ $newSet->set($key, $value);
+ }
+
+ if ($addSet instanceof PricingSetInterface) {
+ foreach ($addSet->getProcessed() as $key => $value) {
+ if (!is_scalar($value)) {
+ continue;
+ }
+ $total = $this->get($key) + $value;
+ $newSet->set($key, $total);
+ }
+ }
+
+ $newSet->setProcessingState(self::PROCESSING_FINISHED);
+
+ return $newSet;
+ }
+
+ public function setContext($context)
+ {
+ $this->context = $context;
+
+ return $this;
}
public function getContext()
@@ -128,16 +207,38 @@ public function all()
return $returnElements;
}
- public function setProcessed($processed)
+ public function setProcessedProperties($properties)
{
- $this->processed = $processed;
+ $this->processedProperties = $properties;
return $this;
}
+ public function getProcessedProperties()
+ {
+ return $this->processedProperties;
+ }
+
+ public function setProcessedValues($values)
+ {
+ $this->processedValues = $values;
+
+ return $this;
+ }
+
+ public function getProcessedValues()
+ {
+ return $this->processedValues;
+ }
+
public function getProcessed()
{
- return $this->processed;
+ $processed = array(
+ 'properties' => $this->processedProperties,
+ 'values' => $this->processedValues,
+ );
+
+ return $processed;
}
public function setProcessingState($processingState)
View
0 ...s/Entity/Element/RecurringElementTest.php → .../Pricing/Element/RecurringElementTest.php
File renamed without changes.
View
66 tests/Entity/Pricing/PricingSetTest.php
@@ -27,6 +27,70 @@ public function testAddElements()
$this->assertCount(4, $pricingSet->getPricingElements());
}
+ public function testGet()
+ {
+ $pricingSet = new PricingSet();
+ $object = new stdClass();
+ $pricingSet->setProcessedProperties(array('thisPropertyExists' => 10));
+ $pricingSet->setProcessedValues(array('thisValueExists' => $object));
+ $pricingSet->setProcessingState(PricingSet::PROCESSING_FINISHED);
+ $this->assertNull($pricingSet->get('noWayInHellThisExists'));
+
+ $this->assertEquals(10, $pricingSet->get('thisPropertyExists'));
+ $this->assertEquals($object, $pricingSet->get('thisValueExists'));
+ }
+
+ public function testPlus()
+ {
+ $pricingSet1 = new PricingSet();
+ $pricingSet1->set('discounts', 5);
+ $pricingSet1->set('netValue', 5);
+ $pricingSet1->set('surcharge', 5);
+ $pricingSet1->set('taxes', 5);
+ $pricingSet1->set('totalValue', 5);
+ $pricingSet1->setProcessingState(PricingSet::PROCESSING_FINISHED);
+
+ $sumPricingSet = $pricingSet1->plus(null);
+ $this->assertInstanceOf('Vespolina\Entity\Pricing\PricingSetInterface', $sumPricingSet, 'a pricing set should be returned when nothing is added');
+ $this->assertNotSame($pricingSet1, $sumPricingSet, 'the new set should be a new object');
+ $this->assertEquals('5', $sumPricingSet->get('discounts'), 'the final value should be 10');
+ $this->assertEquals('5', $sumPricingSet->get('netValue'), 'the final value should be 10');
+ $this->assertEquals('5', $sumPricingSet->get('surcharge'), 'the final value should be 10');
+ $this->assertEquals('5', $sumPricingSet->get('taxes'), 'the final value should be 10');
+ $this->assertEquals('5', $sumPricingSet->get('totalValue'), 'the final value should be 10');
+
+ $pricingSet2 = new PricingSet();
+ $pricingSet2->set('discounts', 5);
+ $pricingSet2->set('netValue', 5);
+ $pricingSet2->set('surcharge', 5);
+ $pricingSet2->set('taxes', 5);
+ $pricingSet2->set('totalValue', 5);
+ $pricingSet2->setProcessingState(PricingSet::PROCESSING_FINISHED);
+
+ $sumPricingSet = $pricingSet1->plus($pricingSet2);
+ $this->assertInstanceOf('Vespolina\Entity\Pricing\PricingSetInterface', $sumPricingSet, 'a pricing set should be returned');
+ $this->assertNotSame($pricingSet1, $sumPricingSet, 'the new set should be a new object');
+ $this->assertNotSame($pricingSet2, $sumPricingSet, 'the new set should be a new object');
+ $this->assertEquals('10', $sumPricingSet->get('discounts'), 'the final value should be 10');
+ $this->assertEquals('10', $sumPricingSet->get('netValue'), 'the final value should be 10');
+ $this->assertEquals('10', $sumPricingSet->get('surcharge'), 'the final value should be 10');
+ $this->assertEquals('10', $sumPricingSet->get('taxes'), 'the final value should be 10');
+ $this->assertEquals('10', $sumPricingSet->get('totalValue'), 'the final value should be 10');
+
+ $pricingSet1 = new PricingSet();
+ $pricingSet1->set('scalar', 5);
+ $pricingSet1->set('object', new PricingSet());
+ $pricingSet1->setProcessingState(PricingSet::PROCESSING_FINISHED);
+ $sumPricingSet = $pricingSet1->plus(null);
+ $this->assertInstanceOf('Vespolina\Entity\Pricing\PricingSetInterface', $sumPricingSet, 'a pricing set should be returned when nothing is added');
+ $this->assertNotSame($pricingSet1, $sumPricingSet, 'the new set should be a new object');
+ $this->assertEquals('5', $sumPricingSet->get('scalar'), 'a scalar value is set');
+ $this->assertNull($sumPricingSet->get('object'), 'a non scalar is skipped');
+
+ $this->markTestIncomplete('implement combining mismatched processed elements');
+ $this->markTestIncomplete('implement inclusions/exclusions from adding process by passing array (either white or black list?)');
+ }
+
public function testGetPricingElements()
{
$pricingSet = new PricingSet();
@@ -67,7 +131,7 @@ public function testProcess()
->will($this->returnValue(array('discount' => '.99')));
$this->markTestIncomplete(
- 'Pricing processingneeds better a better test'
+ 'Pricing processing needs better a better test'
);
$pricingSet->process();

0 comments on commit 5b326b0

Please sign in to comment.