custom/plugins/PickwareGls/vendor/pickware/document-bundle/src/DocumentEntityWriteValidator.php line 36

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\DalBundle\EntityPreWriteValidationEvent;
  12. use Pickware\DalBundle\EntityPreWriteValidationEventDispatcher;
  13. use Pickware\DocumentBundle\Model\DocumentDefinition;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  16. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\Validator\ConstraintViolation;
  19. use Symfony\Component\Validator\ConstraintViolationList;
  20. /**
  21.  * This whole class is there just to ensure that a documents deep link code is exactly 32 characters long.
  22.  */
  23. class DocumentEntityWriteValidator implements EventSubscriberInterface
  24. {
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             EntityPreWriteValidationEventDispatcher::getEventName(DocumentDefinition::ENTITY_NAME) => 'onPreWriteValidation',
  29.         ];
  30.     }
  31.     public function onPreWriteValidation($event): void
  32.     {
  33.         if (!($event instanceof EntityPreWriteValidationEvent)) {
  34.             // The subscriber is probably instantiated in its old version (with the Shopware PreWriteValidationEvent) in
  35.             // the container and will be updated on the next container rebuild (next request). Early return.
  36.             return;
  37.         }
  38.         $documentCommands array_values(array_filter(
  39.             $event->getCommands(),
  40.             fn (WriteCommand $command) => !($command instanceof DeleteCommand),
  41.         ));
  42.         $violations $this->getDeepLinkCodeViolations($documentCommands);
  43.         if ($violations->count() > 0) {
  44.             $event->addViolation(new WriteConstraintViolationException($violations));
  45.         }
  46.     }
  47.     /**
  48.      * Checks every value of the deepLinkCode properties of the $documentCommands. If they do not have the expected
  49.      * length, an appropriate violation is returned.
  50.      *
  51.      * @param WriteCommand[] $documentCommands
  52.      */
  53.     private function getDeepLinkCodeViolations(array $documentCommands): ConstraintViolationList
  54.     {
  55.         $violationMessageTemplate 'The length of the property "deepLinkCode" must be exactly {{ length }} characters.';
  56.         $parameters = ['{{ length }}' => DocumentDefinition::DEEP_LINK_CODE_LENGTH];
  57.         $violationMessage strtr($violationMessageTemplate$parameters);
  58.         $violations = new ConstraintViolationList();
  59.         foreach ($documentCommands as $documentCommand) {
  60.             $payload $documentCommand->getPayload();
  61.             if ($documentCommand->getEntityExistence()->exists() && !isset($payload['deep_link_code'])) {
  62.                 // Update without a change of the deepLinkCode
  63.                 continue;
  64.             }
  65.             if (mb_strlen($payload['deep_link_code']) !== DocumentDefinition::DEEP_LINK_CODE_LENGTH) {
  66.                 $violations[] = new ConstraintViolation(
  67.                     $violationMessage,
  68.                     $violationMessageTemplate,
  69.                     $parameters,
  70.                     null// ???
  71.                     $documentCommand->getPath() . '/deepLinkCode',
  72.                     $payload['deep_link_code'],
  73.                 );
  74.             }
  75.         }
  76.         return $violations;
  77.     }
  78. }