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

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. abstract class AbstractLanguageValidator implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var Connection
  26.      */
  27.     protected $connection;
  28.     public function __construct(Connection $connection)
  29.     {
  30.         $this->connection $connection;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             PostWriteValidationEvent::class => 'postValidate',
  36.         ];
  37.     }
  38.     public function postValidate(PostWriteValidationEvent $event): void
  39.     {
  40.         $violationList = new ConstraintViolationList();
  41.         foreach ($event->getCommands() as $command) {
  42.             if (!($command instanceof InsertCommand || $command instanceof UpdateCommand)
  43.                 || $command->getDefinition()->getClass() !== $this->getSupportedCommandDefinitionClass()
  44.             ) {
  45.                 continue;
  46.             }
  47.             $this->validate($command$violationList);
  48.         }
  49.         if ($violationList->count() > 0) {
  50.             $event->getExceptions()->add(new WriteConstraintViolationException($violationList));
  51.         }
  52.     }
  53.     abstract protected function getSupportedCommandDefinitionClass(): string;
  54.     protected function validate(WriteCommand $commandConstraintViolationList $violationList): void
  55.     {
  56.         $payload $command->getPayload();
  57.         if (!isset($payload['language_id'])) {
  58.             return;
  59.         }
  60.         $languageId $payload['language_id'];
  61.         if (!$this->isLanguageManagedByLanguagePack($languageId) || $this->isSalesChannelLanguageAvailable($languageId)) {
  62.             return;
  63.         }
  64.         $violationList->add(
  65.             new ConstraintViolation(
  66.                 \sprintf('The language with the id "%s" is disabled for all Sales Channels.'Uuid::fromBytesToHex($languageId)),
  67.                 'The language with the id "{{ languageId }}" is disabled for all Sales Channels.',
  68.                 [$languageId],
  69.                 null,
  70.                 $command->getPath(),
  71.                 $languageId
  72.             )
  73.         );
  74.     }
  75.     protected function isSalesChannelLanguageAvailable(string $languageId): bool
  76.     {
  77.         $statement $this->connection->createQueryBuilder()
  78.             ->select('sales_channel_active')
  79.             ->from(PackLanguageDefinition::ENTITY_NAME)
  80.             ->where('language_id = :languageId')
  81.             ->setParameter('languageId'$languageId)
  82.             ->setMaxResults(1)
  83.             ->execute();
  84.         if (!$statement instanceof ResultStatement) {
  85.             return false;
  86.         }
  87.         return (bool) $statement->fetch(FetchMode::COLUMN);
  88.     }
  89.     protected function isLanguageManagedByLanguagePack(string $languageId): bool
  90.     {
  91.         $statement $this->connection->createQueryBuilder()
  92.             ->select('swag_language_pack_language_id')
  93.             ->from(LanguageDefinition::ENTITY_NAME)
  94.             ->where('id = :languageId')
  95.             ->setParameter('languageId'$languageId)
  96.             ->setMaxResults(1)
  97.             ->execute();
  98.         if (!$statement instanceof ResultStatement) {
  99.             return false;
  100.         }
  101.         return (bool) $statement->fetch(FetchMode::COLUMN);
  102.     }
  103. }