custom/plugins/WbfkITScope/src/Subscriber/SupplierDeliveryTimeConfigurationValidator.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\ITScope\Subscriber;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  7. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Validator\ConstraintViolation;
  10. use Symfony\Component\Validator\ConstraintViolationList;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Wbfk\ITScope\Core\Content\WbfkSupplier\WbfkSupplierDefinition;
  13. class SupplierDeliveryTimeConfigurationValidator implements EventSubscriberInterface
  14. {
  15.     public function __construct(private readonly TranslatorInterface $translator)
  16.     {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             PreWriteValidationEvent::class => 'preValidate',
  22.         ];
  23.     }
  24.     public function preValidate(PreWriteValidationEvent $event)
  25.     {
  26.         return;
  27.         $violationList = new ConstraintViolationList();
  28.         foreach ($event->getCommands() as $command) {
  29.             if (!($command instanceof InsertCommand || $command instanceof UpdateCommand)) {
  30.                 continue;
  31.             }
  32.             if ($command->getDefinition()->getClass() !== WbfkSupplierDefinition::class) {
  33.                 continue;
  34.             }
  35.             $violationList->addAll($this->validateDeliveryTimeConfigurations($command));
  36.         }
  37.         if ($violationList->count() > 0) {
  38.             $event->getExceptions()->add(new WriteConstraintViolationException($violationList));
  39.         }
  40.     }
  41.     private function validateDeliveryTimeConfigurations(InsertCommand|UpdateCommand $command): ConstraintViolationList
  42.     {
  43.         $payload $command->getPayload();
  44.         $deliveryTimes json_decode($payload['delivery_times'], true);
  45.         $violationList = new ConstraintViolationList();
  46.         $this->validateDefaultConfigurationIsSet($deliveryTimes$violationList$command);
  47.         $this->validateConfigForEachCountry($deliveryTimes$violationList$command);
  48.         return $violationList;
  49.     }
  50.     private function validateDefaultConfigurationIsSet(
  51.         array $deliveryTimes,
  52.         ConstraintViolationList $violationList,
  53.         InsertCommand|UpdateCommand $command
  54.     ) {
  55.         foreach ($deliveryTimes as $deliveryTime) {
  56.             if ($deliveryTime['isDefault']) {
  57.                 // a default config is found, no further check required.
  58.                 return;
  59.             }
  60.         }
  61.         $message $this->translator->trans('deliveryTimes.error.requiresDefaultDeliveryTime');
  62.         $uuid $this->getUuid($command);
  63.         $violationList->add(
  64.             new ConstraintViolation(
  65.                 $message,
  66.                 $message,
  67.                 [],
  68.                 '',
  69.                 $command->getPath().'/delivery_times',
  70.                 null,
  71.                 null,
  72.                 $uuid,
  73.             )
  74.         );
  75.     }
  76.     public function getUuid(UpdateCommand|InsertCommand $command): string
  77.     {
  78.         $uuid bin2hex($command->getPrimaryKey()['id']);
  79.         return substr($uuid08).'-'.substr($uuid84).'-'.substr($uuid124).'-'.substr(
  80.                 $uuid,
  81.                 16,
  82.                 4
  83.             ).'-'.substr($uuid20);
  84.     }
  85.     private function validateConfigForEachCountry(
  86.         mixed $deliveryTimes,
  87.         ConstraintViolationList $violationList,
  88.         UpdateCommand|InsertCommand $command
  89.     ) {
  90.         foreach (
  91.             $deliveryTimes as ['isLinked' => $isLinked,
  92.             'countryId' => $countryId,
  93.             'isDefault' => $isDefault,
  94.             'parcelDelivery' => $parcelDelivery,
  95.             'freightDelivery' => $freightDelivery]
  96.         ) {
  97.             if ($isLinked) {
  98.                 continue;
  99.             }
  100.             $this->validateDeliveryTime($countryId'parcelDelivery'$parcelDelivery$violationList$command);
  101.             $this->validateDeliveryTime($countryId'freightDelivery'$freightDelivery$violationList$command);
  102.         }
  103.     }
  104.     private function validateDeliveryTime(
  105.         string $countryId,
  106.         string $deliveryType,
  107.         array $deliveryConfig,
  108.         ConstraintViolationList $violationList,
  109.         InsertCommand|UpdateCommand $command
  110.     ) {
  111.         [
  112.             'sameDayShipping' => $sameDayShipping,
  113.             'deliveryTimeInDays' => $deliveryTimeInDays,
  114.             'shippingTimeInDays' => $shippingTimeInDays,
  115.             'sameDayShippingTillTime' => $sameDayShippingTillTime,
  116.         ] = $deliveryConfig;
  117.         if ($sameDayShipping && !$this->validTime($sameDayShippingTillTime)) {
  118.             $message $this->translator->trans('deliveryTimes.error.requiresValidSameDayShippingTillTime');
  119.             $violationList->add(
  120.                 $this->generateDeliveryTimeViolation(
  121.                     $countryId,
  122.                     $deliveryType,
  123.                     $message,
  124.                     $command
  125.                 )
  126.             );
  127.         }
  128.         if (!$sameDayShipping && !is_int($shippingTimeInDays) || $shippingTimeInDays 1) {
  129.             $message $this->translator->trans('deliveryTimes.error.requiresValidShippingTimeInDays');
  130.             $violationList->add(
  131.                 $this->generateDeliveryTimeViolation(
  132.                     $countryId,
  133.                     $deliveryType,
  134.                     $message,
  135.                     $command
  136.                 )
  137.             );
  138.         }
  139.         if (!is_int($deliveryTimeInDays) || $deliveryTimeInDays 1) {
  140.             $message $this->translator->trans('deliveryTimes.error.requiresValidDeliveryTimeInDays');
  141.             $violationList->add(
  142.                 $this->generateDeliveryTimeViolation(
  143.                     $countryId,
  144.                     $deliveryType,
  145.                     $message,
  146.                     $command
  147.                 )
  148.             );
  149.         }
  150.     }
  151.     private function validTime(?string $time): bool
  152.     {
  153.         return (bool)preg_match('#^([01]?[0-9]|2[0-3]):[0-5][0-9]$#', (string)$time);
  154.     }
  155.     private function generateDeliveryTimeViolation(
  156.         string $countryId,
  157.         string $deliveryType,
  158.         string $message,
  159.         UpdateCommand|InsertCommand $command
  160.     ): ConstraintViolation {
  161.         return new ConstraintViolation(
  162.             $message,
  163.             $message,
  164.             [],
  165.             '',
  166.             $command->getPath()."/delivery_times/$countryId/$deliveryType",
  167.             null,
  168.             null,
  169.             $this->getUuid($command)
  170.         );
  171.     }
  172. }