vendor/shopware/platform/src/Core/Checkout/Promotion/Validator/PromotionValidator.php line 68

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Promotion\Validator;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Promotion\Aggregate\PromotionDiscount\PromotionDiscountDefinition;
  5. use Shopware\Core\Checkout\Promotion\Aggregate\PromotionDiscount\PromotionDiscountEntity;
  6. use Shopware\Core\Checkout\Promotion\PromotionDefinition;
  7. use Shopware\Core\Framework\Api\Exception\ResourceNotFoundException;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  12. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Validator\ConstraintViolation;
  15. use Symfony\Component\Validator\ConstraintViolationInterface;
  16. use Symfony\Component\Validator\ConstraintViolationList;
  17. class PromotionValidator implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * this is the min value for all types
  21.      * (absolute, percentage, ...)
  22.      */
  23.     private const DISCOUNT_MIN_VALUE 0.00;
  24.     /**
  25.      * this is used for the maximum allowed
  26.      * percentage discount.
  27.      */
  28.     private const DISCOUNT_PERCENTAGE_MAX_VALUE 100.0;
  29.     /**
  30.      * @var Connection
  31.      */
  32.     private $connection;
  33.     /**
  34.      * @var array
  35.      */
  36.     private $databasePromotions;
  37.     /**
  38.      * @var array
  39.      */
  40.     private $databaseDiscounts;
  41.     public function __construct(Connection $connection)
  42.     {
  43.         $this->connection $connection;
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             PreWriteValidationEvent::class => 'preValidate',
  49.         ];
  50.     }
  51.     /**
  52.      * This function validates our incoming delta-values for promotions
  53.      * and its aggregation. It does only check for business relevant rules and logic.
  54.      * All primitive "required" constraints are done inside the definition of the entity.
  55.      *
  56.      * @throws WriteConstraintViolationException
  57.      */
  58.     public function preValidate(PreWriteValidationEvent $event): void
  59.     {
  60.         $this->collect($event->getCommands());
  61.         $violationList = new ConstraintViolationList();
  62.         $writeCommands $event->getCommands();
  63.         foreach ($writeCommands as $index => $command) {
  64.             if (!$command instanceof InsertCommand && !$command instanceof UpdateCommand) {
  65.                 continue;
  66.             }
  67.             switch (\get_class($command->getDefinition())) {
  68.                 case PromotionDefinition::class:
  69.                     /** @var string $promotionId */
  70.                     $promotionId $command->getPrimaryKey()['id'];
  71.                     try {
  72.                         /** @var array $promotion */
  73.                         $promotion $this->getPromotionById($promotionId);
  74.                     } catch (ResourceNotFoundException $ex) {
  75.                         $promotion = [];
  76.                     }
  77.                     $this->validatePromotion(
  78.                         $promotion,
  79.                         $command->getPayload(),
  80.                         $violationList,
  81.                         $index
  82.                     );
  83.                     break;
  84.                 case PromotionDiscountDefinition::class:
  85.                     /** @var string $discountId */
  86.                     $discountId $command->getPrimaryKey()['id'];
  87.                     try {
  88.                         /** @var array $discount */
  89.                         $discount $this->getDiscountById($discountId);
  90.                     } catch (ResourceNotFoundException $ex) {
  91.                         $discount = [];
  92.                     }
  93.                     $this->validateDiscount(
  94.                         $discount,
  95.                         $command->getPayload(),
  96.                         $violationList,
  97.                         $index
  98.                     );
  99.                     break;
  100.             }
  101.         }
  102.         if ($violationList->count() > 0) {
  103.             $event->getExceptions()->add(new WriteConstraintViolationException($violationList));
  104.         }
  105.     }
  106.     /**
  107.      * This function collects all database data that might be
  108.      * required for any of the received entities and values.
  109.      *
  110.      * @throws ResourceNotFoundException
  111.      * @throws \Doctrine\DBAL\DBALException
  112.      */
  113.     private function collect(array $writeCommands): void
  114.     {
  115.         $promotionIds = [];
  116.         $discountIds = [];
  117.         /** @var WriteCommand $command */
  118.         foreach ($writeCommands as $command) {
  119.             if (!$command instanceof InsertCommand && !$command instanceof UpdateCommand) {
  120.                 continue;
  121.             }
  122.             switch (\get_class($command->getDefinition())) {
  123.                 case PromotionDefinition::class:
  124.                     $promotionIds[] = $command->getPrimaryKey()['id'];
  125.                     break;
  126.                 case PromotionDiscountDefinition::class:
  127.                     $discountIds[] = $command->getPrimaryKey()['id'];
  128.                     break;
  129.             }
  130.         }
  131.         // why do we have inline sql queries in here?
  132.         // because we want to avoid any other private functions that accidentally access
  133.         // the database. all private getters should only access the local in-memory list
  134.         // to avoid additional database queries.
  135.         $promotionQuery $this->connection->executeQuery(
  136.             'SELECT * FROM `promotion` WHERE `id` IN (:ids)',
  137.             ['ids' => $promotionIds],
  138.             ['ids' => Connection::PARAM_STR_ARRAY]
  139.         );
  140.         $this->databasePromotions $promotionQuery->fetchAll() ?? [];
  141.         $discountQuery $this->connection->executeQuery(
  142.             'SELECT * FROM `promotion_discount` WHERE `id` IN (:ids)',
  143.             ['ids' => $discountIds],
  144.             ['ids' => Connection::PARAM_STR_ARRAY]
  145.         );
  146.         $this->databaseDiscounts $discountQuery->fetchAll() ?? [];
  147.     }
  148.     /**
  149.      * Validates the provided Promotion data and adds
  150.      * violations to the provided list of violations, if found.
  151.      *
  152.      * @param array                   $promotion     the current promotion from the database as array type
  153.      * @param array                   $payload       the incoming delta-data
  154.      * @param ConstraintViolationList $violationList the list of violations that needs to be filled
  155.      * @param int                     $index         the index of this promotion in the command queue
  156.      *
  157.      * @throws \Exception
  158.      */
  159.     private function validatePromotion(array $promotion, array $payloadConstraintViolationList $violationListint $index): void
  160.     {
  161.         /** @var string|null $validFrom */
  162.         $validFrom $this->getValue($payload'valid_from'$promotion);
  163.         /** @var string|null $validUntil */
  164.         $validUntil $this->getValue($payload'valid_until'$promotion);
  165.         /** @var bool $useCodes */
  166.         $useCodes $this->getValue($payload'use_codes'$promotion);
  167.         /** @var bool $useCodesIndividual */
  168.         $useCodesIndividual $this->getValue($payload'use_individual_codes'$promotion);
  169.         /** @var string|null $pattern */
  170.         $pattern $this->getValue($payload'individual_code_pattern'$promotion);
  171.         /** @var string|null $promotionId */
  172.         $promotionId $this->getValue($payload'id'$promotion);
  173.         /** @var string|null $code */
  174.         $code $this->getValue($payload'code'$promotion);
  175.         if ($code === null) {
  176.             $code '';
  177.         }
  178.         if ($pattern === null) {
  179.             $pattern '';
  180.         }
  181.         $trimmedCode trim($code);
  182.         // if we have both a date from and until, make sure that
  183.         // the dateUntil is always in the future.
  184.         if ($validFrom !== null && $validUntil !== null) {
  185.             // now convert into real date times
  186.             // and start comparing them
  187.             $dateFrom = new \DateTime($validFrom);
  188.             $dateUntil = new \DateTime($validUntil);
  189.             if ($dateUntil $dateFrom) {
  190.                 $violationList->add($this->buildViolation(
  191.                     'Expiration Date of Promotion must be after Start of Promotion',
  192.                     $payload['valid_until'],
  193.                     'validUntil',
  194.                     'PROMOTION_VALID_UNTIL_VIOLATION',
  195.                     $index
  196.                 ));
  197.             }
  198.         }
  199.         // check if we use global codes
  200.         if ($useCodes && !$useCodesIndividual) {
  201.             // make sure the code is not empty
  202.             if ($trimmedCode === '') {
  203.                 $violationList->add($this->buildViolation(
  204.                     'Please provide a valid code',
  205.                     $code,
  206.                     'code',
  207.                     'PROMOTION_EMPTY_CODE_VIOLATION',
  208.                     $index
  209.                 ));
  210.             }
  211.             // if our code length is greater than the trimmed one,
  212.             // this means we have leading or trailing whitespaces
  213.             if (mb_strlen($code) > mb_strlen($trimmedCode)) {
  214.                 $violationList->add($this->buildViolation(
  215.                     'Code may not have any leading or ending whitespaces',
  216.                     $code,
  217.                     'code',
  218.                     'PROMOTION_CODE_WHITESPACE_VIOLATION',
  219.                     $index
  220.                 ));
  221.             }
  222.         }
  223.         if ($pattern !== '' && $this->isCodePatternAlreadyUsed($pattern$promotionId)) {
  224.             $violationList->add($this->buildViolation(
  225.                 'Code Pattern already exists in other promotion. Please provide a different pattern.',
  226.                 $pattern,
  227.                 'individualCodePattern',
  228.                 'PROMOTION_DUPLICATE_PATTERN_VIOLATION',
  229.                 $index
  230.             ));
  231.         }
  232.         // lookup global code if it does already exist in database
  233.         if ($trimmedCode !== '' && $this->isCodeAlreadyUsed($trimmedCode$promotionId)) {
  234.             $violationList->add($this->buildViolation(
  235.                 'Code already exists in other promotion. Please provide a different code.',
  236.                 $trimmedCode,
  237.                 'code',
  238.                 'PROMOTION_DUPLICATED_CODE_VIOLATION',
  239.                 $index
  240.             ));
  241.         }
  242.     }
  243.     /**
  244.      * Validates the provided PromotionDiscount data and adds
  245.      * violations to the provided list of violations, if found.
  246.      *
  247.      * @param array                   $discount      the discount as array from the database
  248.      * @param array                   $payload       the incoming delta-data
  249.      * @param ConstraintViolationList $violationList the list of violations that needs to be filled
  250.      */
  251.     private function validateDiscount(array $discount, array $payloadConstraintViolationList $violationListint $index): void
  252.     {
  253.         /** @var string $type */
  254.         $type $this->getValue($payload'type'$discount);
  255.         /** @var float|null $value */
  256.         $value $this->getValue($payload'value'$discount);
  257.         if ($value === null) {
  258.             return;
  259.         }
  260.         if ($value self::DISCOUNT_MIN_VALUE) {
  261.             $violationList->add($this->buildViolation(
  262.                 'Value must not be less than ' self::DISCOUNT_MIN_VALUE,
  263.                 $value,
  264.                 'value',
  265.                 'PROMOTION_DISCOUNT_MIN_VALUE_VIOLATION',
  266.                 $index
  267.             ));
  268.         }
  269.         switch ($type) {
  270.             case PromotionDiscountEntity::TYPE_PERCENTAGE:
  271.                 if ($value self::DISCOUNT_PERCENTAGE_MAX_VALUE) {
  272.                     $violationList->add($this->buildViolation(
  273.                         'Absolute value must not greater than ' self::DISCOUNT_PERCENTAGE_MAX_VALUE,
  274.                         $value,
  275.                         'value',
  276.                         'PROMOTION_DISCOUNT_MAX_VALUE_VIOLATION',
  277.                         $index
  278.                     ));
  279.                 }
  280.                 break;
  281.         }
  282.     }
  283.     /**
  284.      * Gets a value from an array. It also does clean checks if
  285.      * the key is set, and also provides the option for default values.
  286.      *
  287.      * @param array  $data  the data array
  288.      * @param string $key   the requested key in the array
  289.      * @param array  $dbRow the db row of from the database
  290.      *
  291.      * @return mixed the object found in the key, or the default value
  292.      */
  293.     private function getValue(array $datastring $key, array $dbRow)
  294.     {
  295.         // try in our actual data set
  296.         if (isset($data[$key])) {
  297.             return $data[$key];
  298.         }
  299.         // try in our db row fallback
  300.         if (isset($dbRow[$key])) {
  301.             return $dbRow[$key];
  302.         }
  303.         // use default
  304.         return null;
  305.     }
  306.     /**
  307.      * @throws ResourceNotFoundException
  308.      *
  309.      * @return array|mixed
  310.      */
  311.     private function getPromotionById(string $id)
  312.     {
  313.         /** @var array $promotion */
  314.         foreach ($this->databasePromotions as $promotion) {
  315.             if ($promotion['id'] === $id) {
  316.                 return $promotion;
  317.             }
  318.         }
  319.         throw new ResourceNotFoundException('promotion', [$id]);
  320.     }
  321.     /**
  322.      * @throws ResourceNotFoundException
  323.      *
  324.      * @return array|mixed
  325.      */
  326.     private function getDiscountById(string $id)
  327.     {
  328.         /** @var array $discount */
  329.         foreach ($this->databaseDiscounts as $discount) {
  330.             if ($discount['id'] === $id) {
  331.                 return $discount;
  332.             }
  333.         }
  334.         throw new ResourceNotFoundException('promotion_discount', [$id]);
  335.     }
  336.     /**
  337.      * This helper function builds an easy violation
  338.      * object for our validator.
  339.      *
  340.      * @param string $message      the error message
  341.      * @param mixed  $invalidValue the actual invalid value
  342.      * @param string $propertyPath the property path from the root value to the invalid value without initial slash
  343.      * @param string $code         the error code of the violation
  344.      * @param int    $index        the position of this entity in the command queue
  345.      *
  346.      * @return ConstraintViolationInterface the built constraint violation
  347.      */
  348.     private function buildViolation(string $message$invalidValuestring $propertyPathstring $codeint $index): ConstraintViolationInterface
  349.     {
  350.         $formattedPath "/{$index}/{$propertyPath}";
  351.         return new ConstraintViolation(
  352.             $message,
  353.             '',
  354.             [
  355.                 'value' => $invalidValue,
  356.             ],
  357.             $invalidValue,
  358.             $formattedPath,
  359.             $invalidValue,
  360.             null,
  361.             $code
  362.         );
  363.     }
  364.     /**
  365.      * True, if the provided pattern is already used in another promotion.
  366.      */
  367.     private function isCodePatternAlreadyUsed(string $pattern, ?string $promotionId): bool
  368.     {
  369.         $qb $this->connection->createQueryBuilder();
  370.         $query $qb
  371.             ->select('id')
  372.             ->from('promotion')
  373.             ->where($qb->expr()->eq('individual_code_pattern'':pattern'))
  374.             ->setParameter(':pattern'$pattern);
  375.         $promotions $query->execute()->fetchAll();
  376.         /** @var array $p */
  377.         foreach ($promotions as $p) {
  378.             // if we have a promotion id to verify
  379.             // and a promotion with another id exists, then return that is used
  380.             if ($promotionId !== null && $p['id'] !== $promotionId) {
  381.                 return true;
  382.             }
  383.         }
  384.         return false;
  385.     }
  386.     /**
  387.      * True, if the provided code is already used as global
  388.      * or individual code in another promotion.
  389.      */
  390.     private function isCodeAlreadyUsed(string $code, ?string $promotionId): bool
  391.     {
  392.         $qb $this->connection->createQueryBuilder();
  393.         // check if individual code.
  394.         // if we dont have a promotion Id only
  395.         // check if its existing somewhere,
  396.         // if we have an Id, verify if it's existing in another promotion
  397.         $query $qb
  398.             ->select('id')
  399.             ->from('promotion_individual_code')
  400.             ->where($qb->expr()->eq('code'':code'))
  401.             ->setParameter(':code'$code);
  402.         if ($promotionId !== null) {
  403.             $query->andWhere($qb->expr()->neq('promotion_id'':promotion_id'))
  404.                 ->setParameter(':promotion_id'$promotionId);
  405.         }
  406.         $existingIndividual = \count($query->execute()->fetchAll()) > 0;
  407.         if ($existingIndividual) {
  408.             return true;
  409.         }
  410.         $qb $this->connection->createQueryBuilder();
  411.         // check if it is a global promotion code.
  412.         // again with either an existing promotion Id
  413.         // or without one.
  414.         $query
  415.             $qb->select('id')
  416.             ->from('promotion')
  417.             ->where($qb->expr()->eq('code'':code'))
  418.             ->setParameter(':code'$code);
  419.         if ($promotionId !== null) {
  420.             $query->andWhere($qb->expr()->neq('id'':id'))
  421.                 ->setParameter(':id'$promotionId);
  422.         }
  423.         return \count($query->execute()->fetchAll()) > 0;
  424.     }
  425. }