vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/PricingManager/Rule.php line 360

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Action\CartActionInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Action\ProductActionInterface;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Condition\BracketInterface;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\PricingManager\Rule\Dao;
  20. use Pimcore\Cache\RuntimeCache;
  21. use Pimcore\Logger;
  22. use Pimcore\Model\AbstractModel;
  23. use Pimcore\Model\Exception\NotFoundException;
  24. /**
  25.  * @method Dao getDao()
  26.  */
  27. class Rule extends AbstractModel implements RuleInterface
  28. {
  29.     /**
  30.      * @param int $id
  31.      *
  32.      * @return RuleInterface|null
  33.      */
  34.     public static function getById($id)
  35.     {
  36.         $cacheKey Dao::TABLE_NAME '_' $id;
  37.         try {
  38.             $rule RuntimeCache::get($cacheKey);
  39.         } catch (\Exception $e) {
  40.             try {
  41.                 $ruleClass get_called_class();
  42.                 /** @var Rule $rule */
  43.                 $rule = new $ruleClass();
  44.                 $rule->getDao()->getById($id);
  45.                 RuntimeCache::set($cacheKey$rule);
  46.             } catch (NotFoundException $ex) {
  47.                 Logger::debug($ex->getMessage());
  48.                 return null;
  49.             }
  50.         }
  51.         return $rule;
  52.     }
  53.     /**
  54.      * @var int|null
  55.      */
  56.     protected $id;
  57.     /**
  58.      * @var string
  59.      */
  60.     protected $name;
  61.     /**
  62.      * @var string[]
  63.      */
  64.     protected $label = [];
  65.     /**
  66.      * @var string[]
  67.      */
  68.     protected $description = [];
  69.     /**
  70.      * @var ConditionInterface|null
  71.      */
  72.     protected ?ConditionInterface $condition null;
  73.     /**
  74.      * @var ActionInterface[]
  75.      */
  76.     protected $action = [];
  77.     /**
  78.      * @var string
  79.      */
  80.     protected $behavior;
  81.     /**
  82.      * @var bool
  83.      */
  84.     protected $active;
  85.     /**
  86.      * @var int
  87.      */
  88.     protected $prio;
  89.     /**
  90.      * load model with serializes data from db
  91.      *
  92.      * @param string $key
  93.      * @param mixed $value
  94.      *
  95.      * @return $this
  96.      *
  97.      * @internal
  98.      */
  99.     public function setValue($key$value)
  100.     {
  101.         $method 'set' $key;
  102.         if (method_exists($this$method)) {
  103.             switch ($method) {
  104.                 // localized fields
  105.                 case 'setlabel':
  106.                 case 'setdescription':
  107.                     $value unserialize($value);
  108.                     if ($value === false) {
  109.                         return $this;
  110.                     } else {
  111.                         $this->$key $value;
  112.                     }
  113.                     return $this;
  114.                     // objects
  115.                 case 'setactions':
  116.                 case 'setcondition':
  117.                     $value unserialize($value);
  118.                     if ($value === false) {
  119.                         return $this;
  120.                     }
  121.             }
  122.             $this->$method($value);
  123.         }
  124.         return $this;
  125.     }
  126.     /**
  127.      * @param int|null $id
  128.      *
  129.      * @return $this
  130.      */
  131.     public function setId($id)
  132.     {
  133.         $this->id $id;
  134.         return $this;
  135.     }
  136.     /**
  137.      * @return int|null
  138.      */
  139.     public function getId()
  140.     {
  141.         return $this->id;
  142.     }
  143.     /**
  144.      * @param string $label
  145.      * @param string|null $locale
  146.      *
  147.      * @return $this
  148.      */
  149.     public function setLabel($label$locale null)
  150.     {
  151.         $this->label[$this->getLanguage($locale)] = $label;
  152.         return $this;
  153.     }
  154.     /**
  155.      * @param string $locale
  156.      *
  157.      * @return string|null
  158.      */
  159.     public function getLabel($locale null)
  160.     {
  161.         return $this->label[$this->getLanguage($locale)] ?? null;
  162.     }
  163.     /**
  164.      * @return string
  165.      */
  166.     public function getName()
  167.     {
  168.         return $this->name;
  169.     }
  170.     /**
  171.      * @param string $name
  172.      * @param string|null $locale
  173.      *
  174.      * @return $this
  175.      */
  176.     public function setName($name$locale null)
  177.     {
  178.         $this->name $name;
  179.         return $this;
  180.     }
  181.     /**
  182.      * @param string $description
  183.      * @param string $locale
  184.      *
  185.      * @return $this
  186.      */
  187.     public function setDescription($description$locale null)
  188.     {
  189.         $this->description[$this->getLanguage($locale)] = $description;
  190.         return $this;
  191.     }
  192.     /**
  193.      * @param string $locale
  194.      *
  195.      * @return string|null
  196.      */
  197.     public function getDescription($locale null)
  198.     {
  199.         return $this->description[$this->getLanguage($locale)] ?? null;
  200.     }
  201.     /**
  202.      * @param string $behavior
  203.      *
  204.      * @return $this
  205.      */
  206.     public function setBehavior($behavior)
  207.     {
  208.         $this->behavior $behavior;
  209.         return $this;
  210.     }
  211.     /**
  212.      * @return string
  213.      */
  214.     public function getBehavior()
  215.     {
  216.         return $this->behavior;
  217.     }
  218.     /**
  219.      * @param bool $active
  220.      *
  221.      * @return $this
  222.      */
  223.     public function setActive($active)
  224.     {
  225.         $this->active $active;
  226.         return $this;
  227.     }
  228.     /**
  229.      * @return bool
  230.      */
  231.     public function getActive()
  232.     {
  233.         return $this->active;
  234.     }
  235.     /**
  236.      * @param ConditionInterface $condition
  237.      *
  238.      * @return $this
  239.      */
  240.     public function setCondition(ConditionInterface $condition)
  241.     {
  242.         $this->condition $condition;
  243.         return $this;
  244.     }
  245.     /**
  246.      * @return ConditionInterface|null
  247.      */
  248.     public function getCondition(): ?ConditionInterface
  249.     {
  250.         return $this->condition;
  251.     }
  252.     /**
  253.      * @param ActionInterface[] $action
  254.      *
  255.      * @return $this
  256.      */
  257.     public function setActions(array $action)
  258.     {
  259.         $this->action $action;
  260.         return $this;
  261.     }
  262.     /**
  263.      * @return ActionInterface[]
  264.      */
  265.     public function getActions()
  266.     {
  267.         return $this->action;
  268.     }
  269.     /**
  270.      * @param int $prio
  271.      *
  272.      * @return $this
  273.      */
  274.     public function setPrio($prio)
  275.     {
  276.         $this->prio = (int)$prio;
  277.         return $this;
  278.     }
  279.     /**
  280.      * @return int
  281.      */
  282.     public function getPrio()
  283.     {
  284.         return $this->prio;
  285.     }
  286.     /**
  287.      * @return $this
  288.      */
  289.     public function save()
  290.     {
  291.         $this->getDao()->save();
  292.         return $this;
  293.     }
  294.     /**
  295.      * delete item
  296.      */
  297.     public function delete()
  298.     {
  299.         $this->getDao()->delete();
  300.     }
  301.     /**
  302.      * test all conditions if this rule is valid
  303.      *
  304.      * @param EnvironmentInterface $environment
  305.      *
  306.      * @return bool
  307.      */
  308.     public function check(EnvironmentInterface $environment)
  309.     {
  310.         $condition $this->getCondition();
  311.         if ($condition) {
  312.             return $condition->check($environment);
  313.         }
  314.         return true;
  315.     }
  316.     /**
  317.      * checks if rule has at least one action that changes product price (and not cart price)
  318.      *
  319.      * @return bool
  320.      */
  321.     public function hasProductActions()
  322.     {
  323.         foreach ($this->getActions() as $action) {
  324.             if ($action instanceof ProductActionInterface) {
  325.                 return true;
  326.             }
  327.         }
  328.         return false;
  329.     }
  330.     /**
  331.      * checks if rule has at least one action that changes cart price
  332.      *
  333.      * @return bool
  334.      */
  335.     public function hasCartActions()
  336.     {
  337.         foreach ($this->getActions() as $action) {
  338.             if ($action instanceof CartActionInterface) {
  339.                 return true;
  340.             }
  341.         }
  342.         return false;
  343.     }
  344.     /**
  345.      * @param EnvironmentInterface $environment
  346.      *
  347.      * @return $this
  348.      */
  349.     public function executeOnProduct(EnvironmentInterface $environment)
  350.     {
  351.         foreach ($this->getActions() as $action) {
  352.             if ($action instanceof ProductActionInterface) {
  353.                 $action->executeOnProduct($environment);
  354.             }
  355.         }
  356.         return $this;
  357.     }
  358.     /**
  359.      * @param EnvironmentInterface $environment
  360.      *
  361.      * @return $this
  362.      */
  363.     public function executeOnCart(EnvironmentInterface $environment)
  364.     {
  365.         foreach ($this->getActions() as $action) {
  366.             if ($action instanceof CartActionInterface) {
  367.                 $action->executeOnCart($environment);
  368.             }
  369.         }
  370.         return $this;
  371.     }
  372.     /**
  373.      * gets current language
  374.      *
  375.      * @param string|null $language
  376.      *
  377.      * @return string
  378.      */
  379.     protected function getLanguage($language null)
  380.     {
  381.         if ($language) {
  382.             return (string) $language;
  383.         }
  384.         return Factory::getInstance()->getEnvironment()->getSystemLocale();
  385.     }
  386.     /**
  387.      * @param string $typeClass
  388.      *
  389.      * @return ConditionInterface[]
  390.      */
  391.     public function getConditionsByType(string $typeClass): array
  392.     {
  393.         $conditions = [];
  394.         $rootCondition $this->getCondition();
  395.         if ($rootCondition instanceof BracketInterface) {
  396.             $conditions $rootCondition->getConditionsByType($typeClass);
  397.         } elseif ($rootCondition instanceof $typeClass) {
  398.             $conditions[] = $rootCondition;
  399.         }
  400.         return $conditions;
  401.     }
  402. }