custom/plugins/SwagLanguagePack/src/Core/Framework/DataAbstractionLayer/Write/Validation/AbstractLanguageValidator.php line 47

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\LanguagePack\Core\Framework\DataAbstractionLayer\Write\Validation;
  8. use Doctrine\DBAL\Connection;
  9. use Doctrine\DBAL\Driver\ResultStatement;
  10. use Doctrine\DBAL\FetchMode;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\InsertCommand;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\WriteCommand;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PostWriteValidationEvent;
  15. use Shopware\Core\Framework\Uuid\Uuid;
  16. use Shopware\Core\Framework\Validation\WriteConstraintViolationException;
  17. use Shopware\Core\System\Language\LanguageDefinition;
  18. use Swag\LanguagePack\PackLanguage\PackLanguageDefinition;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\Validator\ConstraintViolation;
  21. use Symfony\Component\Validator\ConstraintViolationList;
  22. /**
  23.  * @internal
  24.  */
  25. abstract class AbstractLanguageValidator implements EventSubscriberInterface
  26. {
  27.     /**
  28.      * @var Connection
  29.      */
  30.     protected $connection;
  31.     public function __construct(Connection $connection)
  32.     {
  33.         $this->connection $connection;
  34.     }
  35.     public static function getSubscribedEvents(): array
  36.     {
  37.         return [
  38.             PostWriteValidationEvent::class => 'postValidate',
  39.         ];
  40.     }
  41.     public function postValidate(PostWriteValidationEvent $event): void
  42.     {
  43.         $violationList = new ConstraintViolationList();
  44.         foreach ($event->getCommands() as $command) {
  45.             if (!($command instanceof InsertCommand || $command instanceof UpdateCommand)
  46.                 || $command->getDefinition()->getClass() !== $this->getSupportedCommandDefinitionClass()
  47.             ) {
  48.                 continue;
  49.             }
  50.             $this->validate($command$violationList);
  51.         }
  52.         if ($violationList->count() > 0) {
  53.             $event->getExceptions()->add(new WriteConstraintViolationException($violationList));
  54.         }
  55.     }
  56.     abstract protected function getSupportedCommandDefinitionClass(): string;
  57.     protected function validate(WriteCommand $commandConstraintViolationList $violationList): void
  58.     {
  59.         $payload $command->getPayload();
  60.         if (!isset($payload['language_id'])) {
  61.             return;
  62.         }
  63.         $languageId $payload['language_id'];
  64.         if (!$this->isLanguageManagedByLanguagePack($languageId) || $this->isSalesChannelLanguageAvailable($languageId)) {
  65.             return;
  66.         }
  67.         $violationList->add(
  68.             new ConstraintViolation(
  69.                 \sprintf('The language with the id "%s" is disabled for all Sales Channels.'Uuid::fromBytesToHex($languageId)),
  70.                 'The language with the id "{{ languageId }}" is disabled for all Sales Channels.',
  71.                 [$languageId],
  72.                 null,
  73.                 $command->getPath(),
  74.                 $languageId
  75.             )
  76.         );
  77.     }
  78.     protected function isSalesChannelLanguageAvailable(string $languageId): bool
  79.     {
  80.         $statement $this->connection->createQueryBuilder()
  81.             ->select('sales_channel_active')
  82.             ->from(PackLanguageDefinition::ENTITY_NAME)
  83.             ->where('language_id = :languageId')
  84.             ->setParameter('languageId'$languageId)
  85.             ->setMaxResults(1)
  86.             ->execute();
  87.         if (!$statement instanceof ResultStatement) {
  88.             return false;
  89.         }
  90.         return (bool) $statement->fetch(FetchMode::COLUMN);
  91.     }
  92.     protected function isLanguageManagedByLanguagePack(string $languageId): bool
  93.     {
  94.         $statement $this->connection->createQueryBuilder()
  95.             ->select('swag_language_pack_language_id')
  96.             ->from(LanguageDefinition::ENTITY_NAME)
  97.             ->where('id = :languageId')
  98.             ->setParameter('languageId'$languageId)
  99.             ->setMaxResults(1)
  100.             ->execute();
  101.         if (!$statement instanceof ResultStatement) {
  102.             return false;
  103.         }
  104.         return (bool) $statement->fetch(FetchMode::COLUMN);
  105.     }
  106. }