<?php
/**
* @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
*
* @author Jakub MedyĆski <jme@crehler.com>
* @support Crehler <support@crehler.com>
* @created 22.01.2021
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Inspirations\Subscriber;
use Crehler\Inspirations\Entity\Inspiration\InspirationEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class InspirationWriteSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $productRepository;
private EntityRepositoryInterface $inspirationRepository;
public function __construct(EntityRepositoryInterface $productRepository, EntityRepositoryInterface $inspirationRepository)
{
$this->productRepository = $productRepository;
$this->inspirationRepository = $inspirationRepository;
}
public static function getSubscribedEvents()
{
return [
'inspiration.written' => 'onInspirationWritten'
];
}
public function onInspirationWritten(EntityWrittenEvent $event)
{
$ids = $event->getIds();
$context = $event->getContext();
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('id', $ids));
$result = $this->inspirationRepository->search($criteria, $context);
if ($result->getTotal() >= 1)
{
/** @var InspirationEntity $inspiration */
$inspiration = $result->first();
$this->productRepository->update([[
'id' => $inspiration->getMainProductId(),
'active' => $inspiration->isActive()
]], $context);
}
}
}