custom/plugins/PickwareDhl/vendor/pickware/document-bundle/src/DocumentEntityWriteValidator.php line 38

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Pickware GmbH. All rights reserved.
  4.  * This file is part of software that is released under a proprietary license.
  5.  * You must not copy, modify, distribute, make publicly available, or execute
  6.  * its contents or parts thereof without express permission by the copyright
  7.  * holder, unless otherwise permitted by law.
  8.  */
  9. declare(strict_types=1);
  10. namespace Pickware\DocumentBundle;
  11. use Pickware\DocumentBundle\Model\DocumentDefinition;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  15. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Validator\ConstraintViolation;
  18. use Symfony\Component\Validator\ConstraintViolationList;
  19. /**
  20.  * This whole class is there just to ensure that a documents deep link code is exactly 32 characters long.
  21.  */
  22. class DocumentEntityWriteValidator implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @return array
  26.      */
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [
  30.             PreWriteValidationEvent::class => 'onPreWriteValidation',
  31.         ];
  32.     }
  33.     public function onPreWriteValidation(PreWriteValidationEvent $event): void
  34.     {
  35.         $documentCommands array_values(array_filter($event->getCommands(), fn (WriteCommand $command) => $command->getDefinition()->getClass() === DocumentDefinition::class && !($command instanceof DeleteCommand)));
  36.         $violations $this->getDeepLinkCodeViolations($documentCommands);
  37.         if ($violations->count() > 0) {
  38.             $event->getExceptions()->add(new WriteConstraintViolationException($violations));
  39.         }
  40.     }
  41.     /**
  42.      * Checks every value of the deepLinkCode properties of the $documentCommands. If they do not have the expected
  43.      * length, an appropriate violation is returned.
  44.      *
  45.      * @param WriteCommand[] $documentCommands
  46.      */
  47.     private function getDeepLinkCodeViolations(array $documentCommands): ConstraintViolationList
  48.     {
  49.         $violationMessageTemplate 'The length of the property "deepLinkCode" must be exactly {{ length }} characters.';
  50.         $parameters = ['{{ length }}' => DocumentDefinition::DEEP_LINK_CODE_LENGTH];
  51.         $violationMessage strtr($violationMessageTemplate$parameters);
  52.         $violations = new ConstraintViolationList();
  53.         foreach ($documentCommands as $documentCommand) {
  54.             $payload $documentCommand->getPayload();
  55.             if ($documentCommand->getEntityExistence()->exists() && !isset($payload['deep_link_code'])) {
  56.                 // Update without a change of the deepLinkCode
  57.                 continue;
  58.             }
  59.             if (mb_strlen($payload['deep_link_code']) !== DocumentDefinition::DEEP_LINK_CODE_LENGTH) {
  60.                 $violations[] = new ConstraintViolation(
  61.                     $violationMessage,
  62.                     $violationMessageTemplate,
  63.                     $parameters,
  64.                     null// ???
  65.                     $documentCommand->getPath() . '/deepLinkCode',
  66.                     $payload['deep_link_code'],
  67.                 );
  68.             }
  69.         }
  70.         return $violations;
  71.     }
  72. }