custom/plugins/SwagPublisher/src/VersionControlSystem/Internal/PreviewMediaSync.php line 36

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\VersionControlSystem\Internal;
  8. use Shopware\Core\Content\Cms\CmsPageEvents;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AndFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class PreviewMediaSync implements EventSubscriberInterface
  17. {
  18.     private VersionControlCmsGateway $cmsGateway;
  19.     public function __construct(
  20.         VersionControlCmsGateway $cmsGateway
  21.     ) {
  22.         $this->cmsGateway $cmsGateway;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             CmsPageEvents::PAGE_WRITTEN_EVENT => 'updatePreviewMediaId',
  28.         ];
  29.     }
  30.     public function updatePreviewMediaId(EntityWrittenEvent $event): void
  31.     {
  32.         $payloads = [];
  33.         $writeResults $event->getWriteResults();
  34.         foreach ($writeResults as $writeResult) {
  35.             if ($writeResult->hasPayload('previewMediaId')) {
  36.                 $key $writeResult->getPrimaryKey();
  37.                 if (\is_array($key)) {
  38.                     continue;
  39.                 }
  40.                 $payloads[$key] = $writeResult->getPayload();
  41.             }
  42.         }
  43.         if (!$payloads) {
  44.             return;
  45.         }
  46.         $context $event->getContext();
  47.         $drafts $this->cmsGateway
  48.             ->searchDrafts(self::createCriteriaFromPayloads($payloads), $context);
  49.         if (!$drafts->count()) {
  50.             return;
  51.         }
  52.         $draftData = [];
  53.         foreach ($drafts as $draft) {
  54.             $draftData[] = [
  55.                 'id' => $draft->getId(),
  56.                 'previewMediaId' => $payloads[$draft->get('pageId')]['previewMediaId'],
  57.             ];
  58.         }
  59.         $context->scope(Context::SYSTEM_SCOPE, function ($systemContext) use ($draftData): void {
  60.             $this->cmsGateway->updateDrafts($draftData$systemContext);
  61.         });
  62.     }
  63.     private static function createCriteriaFromPayloads(array $payloads): Criteria
  64.     {
  65.         $filters = [];
  66.         foreach ($payloads as $payload) {
  67.             $filters[] = new AndFilter([
  68.                 new EqualsFilter('pageId'$payload['id']),
  69.                 new EqualsFilter('draftVersion'$payload['versionId']),
  70.             ]);
  71.         }
  72.         $criteria = new Criteria();
  73.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR$filters));
  74.         return $criteria;
  75.     }
  76. }