-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from umpirsky/master
Initial promotions implementation
- Loading branch information
Showing
100 changed files
with
5,174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
vendor/ | ||
bin/ | ||
|
||
composer.phar | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
Oops, something went wrong.