custom/plugins/SwagPublisher/src/Common/UpdateChangeDetector.php line 37

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 SwagPublisher\Common;
  8. use Doctrine\DBAL\Connection;
  9. use Doctrine\DBAL\Platforms\MariaDb1027Platform;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Field\JsonField;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\UpdateCommand;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class UpdateChangeDetector implements EventSubscriberInterface
  15. {
  16.     private Connection $connection;
  17.     /**
  18.      * @var string[]
  19.      */
  20.     private array $activeEntities;
  21.     public function __construct(Connection $connection, array $activeEntities = [])
  22.     {
  23.         $this->connection $connection;
  24.         $this->activeEntities $activeEntities;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [PreWriteValidationEvent::class => 'detectChanges'];
  29.     }
  30.     public function detectChanges(PreWriteValidationEvent $event): void
  31.     {
  32.         $commands $event->getCommands();
  33.         $extension UpdateChangeContextExtension::extract($event->getContext());
  34.         foreach ($commands as $command) {
  35.             if (!$command instanceof UpdateCommand) {
  36.                 continue;
  37.             }
  38.             if (!\in_array($command->getDefinition()->getClass(), $this->activeEntitiestrue)) {
  39.                 continue;
  40.             }
  41.             $extension->addResult(
  42.                 $command,
  43.                 $this->containsChanges($command)
  44.             );
  45.         }
  46.     }
  47.     private function containsChanges(UpdateCommand $command): bool
  48.     {
  49.         $payload $command->getPayload();
  50.         unset($payload['updated_at']);
  51.         $conditions = [];
  52.         $values = [];
  53.         $data \array_merge($command->getPrimaryKey(), $payload);
  54.         $fields $command->getDefinition()->getFields();
  55.         $isMariaDb $this->connection->getDatabasePlatform() instanceof MariaDb1027Platform;
  56.         foreach ($data as $key => $value) {
  57.             $field $fields->getByStorageName($key);
  58.             if ($field === null) {
  59.                 continue;
  60.             }
  61.             if (!$isMariaDb && $field instanceof JsonField) {
  62.                 $conditions[] = "`$key` = CAST(:$key AS JSON)";
  63.             } else {
  64.                 $conditions[] = "`$key` = :$key";
  65.             }
  66.             $values[$key] = $value;
  67.         }
  68.         $query \sprintf(
  69.             'SELECT 1 AS id FROM `%s` WHERE %s LIMIT 1',
  70.             $command->getDefinition()->getEntityName(),
  71.             \implode(' AND '$conditions)
  72.         );
  73.         return $this->connection->fetchColumn($query$values) !== '1';
  74.     }
  75. }