Skip to content

Commit

Permalink
Merge pull request #1 from umpirsky/master
Browse files Browse the repository at this point in the history
Initial promotions implementation
  • Loading branch information
Paweł Jędrzejewski committed Feb 22, 2013
2 parents 0a560a6 + e9ef929 commit 7a804d5
Show file tree
Hide file tree
Showing 100 changed files with 5,174 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
vendor/
bin/

composer.phar
composer.lock
43 changes: 43 additions & 0 deletions Action/FixedDiscountPromotionAction.php
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Action;

use Sylius\Bundle\ResourceBundle\Model\RepositoryInterface;
use Sylius\Bundle\SalesBundle\Model\OrderInterface;

/**
* Creates adjustment and adds it to given order.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class FixedDiscountPromotionAction implements PromotionActionInterface
{
protected $repository;

public function __construct(RepositoryInterface $repository)
{
$this->repository = $repository;
}

public function execute(OrderInterface $order, array $configuration)
{
$adjustment = $this->repository->createNew();
$adjustment->setAmount($configuration['amount']);

$order->addAdjustment($adjustment);
}

public function getConfigurationFormType()
{
return 'sylius_promotion_action_fixed_discount_configuration';
}
}
25 changes: 25 additions & 0 deletions Action/PromotionActionInterface.php
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Action;

use Sylius\Bundle\SalesBundle\Model\OrderInterface;

/**
* Executes promotion action on given order.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
interface PromotionActionInterface
{
public function execute(OrderInterface $order, array $configuration);
public function getConfigurationFormType();
}
41 changes: 41 additions & 0 deletions Action/PromotionApplicator.php
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Action;

use Sylius\Bundle\PromotionsBundle\Action\Registry\PromotionActionRegistryInterface;
use Sylius\Bundle\SalesBundle\Model\OrderInterface;
use Sylius\Bundle\PromotionsBundle\Model\PromotionInterface;

/**
* Applies all registered promotion actions to given order.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class PromotionApplicator implements PromotionApplicatorInterface
{
protected $registry;

public function __construct(PromotionActionRegistryInterface $registry)
{
$this->registry = $registry;
}

public function apply(OrderInterface $order, PromotionInterface $promotion)
{
foreach ($promotion->getActions() as $action) {
$this->registry
->getAction($action->getType())
->execute($order, $action->getConfiguration())
;
}
}
}
25 changes: 25 additions & 0 deletions Action/PromotionApplicatorInterface.php
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Action;

use Sylius\Bundle\SalesBundle\Model\OrderInterface;
use Sylius\Bundle\PromotionsBundle\Model\PromotionInterface;

/**
* Applies promotion to given order.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
interface PromotionApplicatorInterface
{
public function apply(OrderInterface $order, PromotionInterface $promotion);
}
26 changes: 26 additions & 0 deletions Action/Registry/ExistingPromotionActionException.php
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Action\Registry;

/**
* This exception should be thrown by promotion action registry
* when action of given type already exists.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class ExistingPromotionActionException extends \InvalidArgumentException
{
public function __construct($type)
{
parent::__construct(sprintf('Promotion action of type "%s" already exist.', $type));
}
}
26 changes: 26 additions & 0 deletions Action/Registry/NonExistingPromotionActionException.php
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Action\Registry;

/**
* This exception should be thrown by promotion action registry
* when action of given type does not exist.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class NonExistingPromotionActionException extends \InvalidArgumentException
{
public function __construct($type)
{
parent::__construct(sprintf('Promotion action of type "%s" does not exist.', $type));
}
}
74 changes: 74 additions & 0 deletions Action/Registry/PromotionActionRegistry.php
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Action\Registry;

use Sylius\Bundle\PromotionsBundle\Action\PromotionActionInterface;

/**
* Promotion action registry.
*
* This service keeps all promotion actions registered inside
* container. Allows to retrieve them by type.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class PromotionActionRegistry implements PromotionActionRegistryInterface
{
/**
* Promotion actions.
*
* @var PromotionActionInterface[]
*/
protected $actions;

public function __construct()
{
$this->actions = array();
}

public function getActions()
{
return $this->actions;
}

public function registerAction($name, PromotionActionInterface $action)
{
if ($this->hasAction($name)) {
throw new ExistingPromotionActionException($name);
}

$this->actions[$name] = $action;
}

public function unregisterAction($name)
{
if (!$this->hasAction($name)) {
throw new NonExistingPromotionActionException($name);
}

unset($this->actions[$name]);
}

public function hasAction($name)
{
return isset($this->actions[$name]);
}

public function getAction($name)
{
if (!$this->hasAction($name)) {
throw new NonExistingPromotionActionException($name);
}

return $this->actions[$name];
}
}
28 changes: 28 additions & 0 deletions Action/Registry/PromotionActionRegistryInterface.php
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Action\Registry;

use Sylius\Bundle\PromotionsBundle\Action\PromotionActionInterface;

/**
* Promotion action registry interface.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
interface PromotionActionRegistryInterface
{
public function getActions();
public function registerAction($name, PromotionActionInterface $action);
public function unregisterAction($name);
public function hasAction($name);
public function getAction($name);
}
35 changes: 35 additions & 0 deletions Checker/ItemCountRuleChecker.php
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Checker;

use Sylius\Bundle\SalesBundle\Model\OrderInterface;

/**
* Checks if order item count exeeds (or at least equal) to the configured count.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class ItemCountRuleChecker implements RuleCheckerInterface
{
public function isEligible(OrderInterface $order, array $configuration)
{
if ($configuration['equal']) {
return $order->countItems() >= $configuration['count']; }

return $order->countItems() > $configuration['count'];
}

public function getConfigurationFormType()
{
return 'sylius_promotion_rule_item_count_configuration';
}
}
36 changes: 36 additions & 0 deletions Checker/OrderTotalRuleChecker.php
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sylius\Bundle\PromotionsBundle\Checker;

use Sylius\Bundle\SalesBundle\Model\OrderInterface;

/**
* Checks if order’s total exeeds (or at least equal) to the configured amount.
*
* @author Saša Stamenković <umpirsky@gmail.com>
*/
class OrderTotalRuleChecker implements RuleCheckerInterface
{
public function isEligible(OrderInterface $order, array $configuration)
{
if ($configuration['equal']) {
return $order->getTotal() >= $configuration['amount'];
}

return $order->getTotal() > $configuration['amount'];
}

public function getConfigurationFormType()
{
return 'sylius_promotion_rule_order_total_configuration';
}
}

0 comments on commit 7a804d5

Please sign in to comment.