<?php declare(strict_types=1);
namespace WabsMaterialstammSync\Subscriber;
use Exception;
use Psr\Log\LoggerInterface;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use WabsMaterialstammSync\Exceptions\WabsMaterialstammSyncException;
use WabsMaterialstammSync\Service\WabsMaterialstammSubsriberService;
class WabsMaterialstammSyncSubscriber implements EventSubscriberInterface
{
/** @var WabsMaterialstammSubsriberService $WabsMaterialstammSubsriberService */
private $wabsMaterialstammSubsriberService;
/** @var LoggerInterface $logger */
private $logger;
public function __construct(
WabsMaterialstammSubsriberService $wabsMaterialstammSubsriberService,
LoggerInterface $logger
) {
$this->wabsMaterialstammSubsriberService = $wabsMaterialstammSubsriberService;
$this->logger = $logger;
}
public static function getSubscribedEvents(): array
{
return [
ProductEvents::PRODUCT_WRITTEN_EVENT => "onProductWritten",
CategoryEvents::CATEGORY_WRITTEN_EVENT => "onCategoryWritten"
];
}
/**
* @param EntityWrittenEvent $event
*/
public function onProductWritten(EntityWrittenEvent $event)
{
//only execute if the context source is admin
if (!$this->isAdminContextSource($event)) {
return;
}
try {
$this->logger->debug("--- Starting onProductWritten Event - WabsMaterialstammSync\Subscriber");
$productIds = [];
foreach ($event->getWriteResults() as $writeResult) {
$productIds[] = $writeResult->getPrimaryKey();
}
$this->wabsMaterialstammSubsriberService->updateMasterdataIsSync($productIds, false);
$this->logger->debug("--- onProductWritten found " . count($productIds) . " ids");
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
}
}
/**
* @param EntityWrittenEvent $event
*/
public function onCategoryWritten(EntityWrittenEvent $event)
{
//only execute if the context source is admin
if (!$this->isAdminContextSource($event)) {
return;
}
try {
$this->logger->debug("--- Starting onCategoryWritten Event - WabsMaterialstammSync\Subscriber");
$categoryIds = [];
foreach ($event->getWriteResults() as $writeResult) {
$categoryIds[] = $writeResult->getPrimaryKey();
}
$categoryIds = $this->wabsMaterialstammSubsriberService->getCategoriesIdsByIds($categoryIds);
$productIds = $this->wabsMaterialstammSubsriberService->getProductIdsByCategoryIds($categoryIds);
if (empty($productIds)) {
throw new WabsMaterialstammSyncException("No Products found");
}
$this->wabsMaterialstammSubsriberService->updateMasterdataIsSync($productIds, false);
$this->logger->debug("--- onCategoryWritten found " . count($productIds), $productIds);
} catch (Exception $exception) {
$this->logger->error($exception->getMessage());
}
}
private function isAdminContextSource(EntityWrittenEvent $event): bool
{
return is_a($event->getContext()->getSource(), "Shopware\Core\Framework\Api\Context\AdminApiSource");
}
}