82 lines
1.9 KiB
PHP
82 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace KupShop\OrderDiscountBundle;
|
|
|
|
use KupShop\OrderDiscountBundle\Actions\ActionInterface;
|
|
use KupShop\OrderDiscountBundle\Triggers\AbstractTrigger;
|
|
use KupShop\OrderDiscountBundle\Triggers\TriggerInterface;
|
|
|
|
class OrderDiscountLocator
|
|
{
|
|
private $triggers;
|
|
private $actions;
|
|
|
|
public function __construct(iterable $triggers, iterable $actions)
|
|
{
|
|
$triggersToSort = [];
|
|
foreach ($triggers as $trigger) {
|
|
$triggersToSort[$trigger::getType()] = [
|
|
'position' => $trigger::getPosition(),
|
|
'trigger' => $trigger,
|
|
];
|
|
}
|
|
$actionsToSort = [];
|
|
foreach ($actions as $action) {
|
|
$actionsToSort[$action::getType()] = [
|
|
'position' => $action::getPosition(),
|
|
'action' => $action,
|
|
];
|
|
}
|
|
|
|
$sortedTriggers = $this->sortArray($triggersToSort);
|
|
foreach ($sortedTriggers as $sorted) {
|
|
$trigger = $sorted['trigger'];
|
|
$this->triggers[$trigger::getType()] = $trigger;
|
|
}
|
|
|
|
$sortedActions = $this->sortArray($actionsToSort);
|
|
foreach ($sortedActions as $sorted) {
|
|
$action = $sorted['action'];
|
|
$this->actions[$action::getType()] = $action;
|
|
}
|
|
}
|
|
|
|
private function sortArray($array)
|
|
{
|
|
usort($array, function ($a, $b) {
|
|
return $a['position'] - $b['position'];
|
|
});
|
|
|
|
return $array;
|
|
}
|
|
|
|
/**
|
|
* @return AbstractTrigger[]
|
|
*/
|
|
public function getTriggers()
|
|
{
|
|
return $this->triggers;
|
|
}
|
|
|
|
/**
|
|
* @return TriggerInterface
|
|
*/
|
|
public function getTrigger($type)
|
|
{
|
|
return $this->triggers[$type];
|
|
}
|
|
|
|
public function getActions()
|
|
{
|
|
return $this->actions;
|
|
}
|
|
|
|
/**
|
|
* @return ActionInterface
|
|
*/
|
|
public function getAction($type)
|
|
{
|
|
return $this->actions[$type];
|
|
}
|
|
}
|