<?php declare(strict_types=1);
/**
* valantic CEC Deutschland GmbH
* Copyright (c) 2022.
*
* All rights reserved.
*
* @see https://www.valantic.com/
*
* @author andrei
* Date: 12.10.2022 16:18
*/
namespace Valantic\Bundle\PentagastBundle\Subscriber;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouterInterface;
use Valantic\Bundle\PentagastBundle\Service\ShopFeaturesService;
use Valantic\Bundle\PentagastBundle\Struct\PurchasePrice;
class StorefrontSubscriber implements EventSubscriberInterface
{
public const SALES_PRICES_VISIBLE = 'salesPricesVisible';
public const STOCK_LEVELS_VISIBLE = 'stockLevelsVisible';
public const PURCHASE_PRICES_VISIBLE = 'purchasePricesVisible';
public const REGISTRATION_ENABLED = 'registrationEnabled';
public const SEARCH_BAR_ENABLED = 'searchBarEnabled';
public const LOGIN_ENABLED = 'loginEnabled';
public const CART_CHECKOUT_ENABLED = 'cartCheckoutEnabled';
public const DELIVERY_TIMES_VISIBLE = 'showDeliveryTimes';
public const STOCK_TRAFFIC_LIGHTS_VISIBLE = 'stockTrafficLightsVisible';
public const DIGTIAL_MEDIA_DOWNLOADS_VISIBLE = 'digitalMediaDownloadsVisible';
public const ACTIVATE_CLOSED_SHOP = 'val_pentagast_bundle_closed_shop';
private RouterInterface $router;
public function __construct(private ShopFeaturesService $shopFeaturesService, RouterInterface $router)
{
$this->router = $router;
}
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => ['onStorefrontRender', -250],
];
}
public function onStorefrontRender(StorefrontRenderEvent $event): void
{
$salesChannelContext = $event->getSalesChannelContext();
$customFields = $salesChannelContext->getSalesChannel()->getCustomFields();
$closedShopEnabled = $customFields[self::ACTIVATE_CLOSED_SHOP] ?? false;
$isLoggedIn = $salesChannelContext->getCustomer() !== null;
$allowedRoutes = [
'frontend.account.login.page',
'frontend.account.login',
'frontend.cms.page',
#'frontend.navigation.page',
'frontend.landing.page',
'frontend.content.page',
];
$request = $event->getRequest();
$currentRoute = $request->attributes->get('_route');
// Nur Redirect ausführen, wenn es keine Ajax-Anfrage ist
if ($closedShopEnabled && !$isLoggedIn && (!in_array($currentRoute, $allowedRoutes, true) || strpos($cmsPage->getTranslated()['name'], 'Datenschutz'))) {
if ($request->isXmlHttpRequest()) {
$event->setParameter('error', 'Login erforderlich');
} else {
$loginUrl = $this->router->generate('frontend.account.login.page');
header('Location: ' . $loginUrl);
exit;
}
}
$event->setParameter(self::SALES_PRICES_VISIBLE, $this->shopFeaturesService->salesPricesVisible($event->getSalesChannelContext()));
$event->setParameter(self::STOCK_LEVELS_VISIBLE, $this->shopFeaturesService->stockLevelsVisible($event->getSalesChannelContext()));
$event->setParameter(self::PURCHASE_PRICES_VISIBLE, $this->shopFeaturesService->purchasePricesVisible($event->getSalesChannelContext()));
$event->setParameter(self::DIGTIAL_MEDIA_DOWNLOADS_VISIBLE, $this->shopFeaturesService->digitalMediaDownloadsVisible($event->getSalesChannelContext()));
$event->setParameter(self::REGISTRATION_ENABLED, $this->shopFeaturesService->registrationEnabled($event->getSalesChannelContext()));
$event->setParameter(self::SEARCH_BAR_ENABLED, $this->shopFeaturesService->searchBarEnabled($event->getSalesChannelContext()));
$event->setParameter(self::LOGIN_ENABLED, $this->shopFeaturesService->loginEnabled($event->getSalesChannelContext()));
$event->setParameter(self::CART_CHECKOUT_ENABLED, $this->shopFeaturesService->cartCheckoutEnabled($event->getSalesChannelContext()));
$event->setParameter(self::DELIVERY_TIMES_VISIBLE, $this->shopFeaturesService->deliveryTimesVisible($event->getSalesChannelContext()));
$event->setParameter(self::STOCK_TRAFFIC_LIGHTS_VISIBLE, $this->shopFeaturesService->stockTrafficLightsVisible($event->getSalesChannelContext()));
$this->updateCart($event->getParameters());
}
protected function updateCart(array $parameters): void
{
if ($page = $parameters['page'] ?? null) {
if (method_exists($page, 'getCart')) {
/** @var Cart $cart */
$cart = $page->getCart();
foreach ($cart->getLineItems()->getElements() as $lineItem) {
$purchasePriceValue = 0;
$targetProductLine = $lineItem;
if ($lineItem->getType() === 'customized-products') {
$targetProductLine = $lineItem
->getChildren()
->filter(fn (LineItem $lineItem) => $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE)
->first();
}
if ($targetProductLine) {
$payload = $targetProductLine->getPayload();
$purchasePrices = json_decode($payload['purchasePrices'] ?? '', true);
if ($purchasePrices) {
$purchasePriceValue = $purchasePrices['calculatedPrice'] ?? 0;
}
}
$purchasePrice = new PurchasePrice();
$purchasePrice->setValue($purchasePriceValue);
/** @psalm-suppress DeprecatedMethod */
$lineItem->addExtension('purchasePrice', $purchasePrice);
}
}
}
}
}