custom/plugins/WbfkITScope/src/Subscriber/WbfkProductExtensionValidator.php line 30

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbfk\ITScope\Subscriber;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  8. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\Validator\ConstraintViolation;
  11. use Symfony\Component\Validator\ConstraintViolationList;
  12. use Wbfk\ITScope\Core\Content\Product\WbfkItScopeProductExtensionDefinition;
  13. class WbfkProductExtensionValidator implements EventSubscriberInterface
  14. {
  15.     public function __construct(protected Connection $connection)
  16.     {
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             PreWriteValidationEvent::class => 'onPreValidate',
  22.         ];
  23.     }
  24.     public function onPreValidate(PreWriteValidationEvent $event): void
  25.     {
  26.         $violationList = new ConstraintViolationList();
  27.         foreach ($event->getCommands() as $command) {
  28.             if (!($command instanceof InsertCommand || $command instanceof UpdateCommand)) {
  29.                 continue;
  30.             }
  31.             if ($command->getDefinition()->getClass() !== WbfkItScopeProductExtensionDefinition::class) {
  32.                 continue;
  33.             }
  34.             $violationList->addAll($this->validateWbfkProductExtension($command));
  35.         }
  36.         if ($violationList->count() > 0) {
  37.             $event->getExceptions()->add(new WriteConstraintViolationException($violationList));
  38.         }
  39.     }
  40.     protected function validateWbfkProductExtension(InsertCommand|UpdateCommand $command): ConstraintViolationList
  41.     {
  42.         $violationList = new ConstraintViolationList();
  43.         $payload $command->getPayload();
  44.         if (empty($payload['it_scope_id'])) {
  45.             return $violationList;
  46.         }
  47.         $itScopeId json_decode($payload['it_scope_id'], true);
  48.         $stmt $this->connection->prepare('SELECT LOWER(HEX(product_id)) AS product_id FROM wbfk_it_scope_product_extension WHERE it_scope_id = ?');
  49.         $stmt->executeStatement([$itScopeId]);
  50.         if ($stmt->rowCount() > 0) {
  51.             $result $stmt->fetchOne();
  52.             $violationList->add(
  53.                 new ConstraintViolation(
  54.                     sprintf('Die ITScopeId wird bereits von Artikel %s genutzt'$result),
  55.                     'Die ITScopeId wird bereits genutzt',
  56.                     [],
  57.                     '',
  58.                     $command->getPath().'/itScopeId',
  59.                     $itScopeId
  60.                 )
  61.             );
  62.         }
  63.         return $violationList;
  64.     }
  65. }