src/Ecommerce/PriceSystem/PriceSystemRule/Processor.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Ecommerce\PriceSystem\PriceSystemRule;
  3. use App\Ecommerce\PriceSystem\PriceSystemRuleContract;
  4. use App\Model\Customer;
  5. use Factory\SupportBundle\Contract\Model\Ecommerce\AddressContract;
  6. use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
  7. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  8. class Processor
  9. {
  10.     const ACTION_CLASS_DEFAULT 'actionClass';
  11.     const ACTION_CLASS_EMPTY 'emptyActionClass';
  12.     /**
  13.      * @var EnvironmentInterface
  14.      */
  15.     protected $environment;
  16.     /**
  17.      * @var array
  18.      */
  19.     protected $options = [];
  20.     /**
  21.      * @var array
  22.      */
  23.     protected $customrGroupIds = [];
  24.     protected $customer;
  25.     public function __construct(EnvironmentInterface $environment, array $options = [])
  26.     {
  27.         $this->environment $environment;
  28.         $this->options $this->processOptions($options);
  29.         $this->customrGroupIds = [];
  30.     }
  31.     protected function processOptions(array $options)
  32.     {
  33.         return array_merge(
  34.             [
  35.                 'actionClass' => Action::class,
  36.                 'emptyActionClass' => EmptyAction::class
  37.             ],
  38.             $options
  39.         );
  40.     }
  41.     protected function createActionClass(string $type$dataPriceSystemRuleContract $priceSystemRule)
  42.     {
  43.         return new $this->options[$type]($data$priceSystemRule$this);
  44.     }
  45.     public function getMatchingAction(PriceSystemRuleContract $priceSystemRule): ActionContract
  46.     {
  47.         if (!$priceSystemRule->getActions()) {
  48.             return $this->createActionClass(self::ACTION_CLASS_EMPTYnull$priceSystemRule);
  49.         }
  50.         foreach ($priceSystemRule->getActions() as $action) {
  51.             $actionClass $this->createActionClass(self::ACTION_CLASS_DEFAULT$action$priceSystemRule);
  52.             if (!$this->match($actionClass)) {
  53.                 continue;
  54.             }
  55.             return $actionClass;
  56.         }
  57.         return $this->createActionClass(self::ACTION_CLASS_EMPTYnull$priceSystemRule);
  58.     }
  59.     protected function match(ActionContract $action)
  60.     {
  61.         $user \Pimcore\Tool\Admin::getCurrentUser();
  62.         if (!$this->getCustomer() && !$user) {
  63.             return false;
  64.         }
  65.         if ($action->getNotIn()) {
  66.             if (!in_array($this->getCountryCode(), $action->getCountries())) {
  67.                 return $action;
  68.             }
  69.         }
  70.         if (in_array($this->getCountryCode(), $action->getCountries())) {
  71.             return $action;
  72.         }
  73.         return false;
  74.     }
  75.     protected function getCustomer(): ?Customer
  76.     {
  77.         if (!$this->customer) {
  78.             $this->customer Customer::getById($this->environment->getCurrentUserId());
  79.         }
  80.         return $this->customer;
  81.     }
  82.     protected function getCountryCode()
  83.     {
  84.         $env Factory::getInstance()->getEnvironment();
  85.         if (!$env->getBillingAddress()) {
  86.             return null;
  87.         }
  88.         if ($env->getBillingAddress() instanceof AddressContract) {
  89.             return $env->getBillingAddress()->getCountryCode();
  90.         }
  91.         // We will replace all addresses with `AddressContract`
  92.         return $env->getBillingAddress()->getCountry();
  93.     }
  94. }