<?php
declare(strict_types=1);
namespace Mad\MyWerb\Subscriber;
use League\Flysystem\FilesystemInterface;
use Mad\MyWerb\Message\MyWerbOrderPlaced;
use Mad\MyWerb\Service\MyWerbChecker;
use Mad\MyWerb\Service\MyWerbFileLocator;
use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
use Shopware\Core\Checkout\Cart\Event\CheckoutOrderPlacedEvent;
use Shopware\Core\Content\MailTemplate\MailTemplateEntity;
use Shopware\Core\Content\Product\SalesChannel\Listing\ProductListingLoader;
use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Metric\CountResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Core\System\SalesChannel\Context\AbstractSalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPage;
use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoader;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Twig\Environment;
use UsingRefs\Product;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class ProductSubscriber implements EventSubscriberInterface
{
public function __construct(
private MyWerbChecker $myWerbChecker,
private RequestStack $requestStack,
private FilesystemInterface $owncloud,
private FilesystemInterface $filesystemTemp,
private EntityRepositoryInterface $mailTemplateRepository,
private EntityRepositoryInterface $salesChannelRepository,
private AbstractSalesChannelContextFactory $salesChannelContextFactory,
private Environment $twig,
private SystemConfigService $systemConfigService,
private MessageBusInterface $messageBus,
private MyWerbFileLocator $myWerbFileLocator
) {
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
ProductPageLoadedEvent::class => 'addMyWerbCheck',
ProductEvents::PRODUCT_LOADED_EVENT => "onProductLoaded",
BeforeLineItemAddedEvent::class => 'onLineItemAdded',
//CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPage',
#CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPage',
#CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinish',
CheckoutOrderPlacedEvent::class => [
['handleMyWerbOrder', PHP_INT_MIN + 100]
],
];
}
public function handleMyWerbOrder(CheckoutOrderPlacedEvent $event): void
{
$order = $event->getOrder();
$salesChannelId = $event->getSalesChannelId();
$context = $event->getContext();
$message = MyWerbOrderPlaced::create($order, $salesChannelId, $context);
foreach ($order->getLineItems() as $orderLineItem) {
$payload = $orderLineItem->getPayload();
if (\is_array($payload) && isset($payload['mywerb'])) {
$this->messageBus->dispatch($message);
return;
}
}
}
public function onLineItemAdded(BeforeLineItemAddedEvent $event)
{
$mywerbId = 0;
$request = $this->requestStack->getCurrentRequest()->get('lineItems');
if ($request) {
foreach ($request as $key => $item) {
if (isset($item['mywerb'])) {
$mywerbId = str_replace(' ', '_', $item['mywerb']);
}
}
if ($mywerbId) {
$mylineitems = $event->getCart()->getLineItems();
$lineitems = $event->getLineItem();
$lineitems->setPayloadValue("mywerb", $mywerbId);
}
}
}
public function onProductLoaded(EntityLoadedEvent $event)
{
$entities = $event->getEntities();
/** @var SalesChannelProductEntity $entity */
foreach ($entities as $entity) {
$number = $entity->getProductNumber();
$name = $entity->getName();
if ($this->myWerbChecker->isMyWerb($entity)) {
$data = new ArrayStruct(['myWerb' => true]);
$entity->addExtension('myWerb', $data);
}
}
}
public function addMyWerbCheck(ProductPageLoadedEvent $event)
{
$number = $event->getPage()->getProduct()->getProductNumber();
$name = $event->getPage()->getProduct()->getName();
if ($this->myWerbChecker->isMyWerb($event->getPage()->getProduct())) {
$filePath = $this->myWerbFileLocator->getProductDownloadPath($number, 'pdf', $event->getSalesChannelContext()->getSalesChannelId());
if ($this->owncloud->has($filePath)) {
$download = new ArrayEntity(['Download' => '1']);
$event->getPage()->addExtension('Download', $download);
}
$data = new ArrayEntity(['myWerb' => '1']);
$event->getPage()->addExtension('myWerb', $data);
}
}
}