<?php declare(strict_types=1);
namespace Ultra\Wartung\Subscriber;
use Doctrine\DBAL\Connection;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Routing\RouterInterface;
class Frontend implements EventSubscriberInterface {
/**
* @var RouterInterface
*/
private $router;
/**
* @var connection
*/
private $connection;
/**
* @var SystemConfigService
*/
private $systemConfigService;
public function __construct(
RouterInterface $router,
Connection $connection,
SystemConfigService $systemConfigService
)
{
$this->router = $router;
$this->connection = $connection;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array {
return[
KernelEvents::REQUEST => 'ultrawartung',
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
public function onStorefrontRender(StorefrontRenderEvent $event) {
$shopId = $event->getSalesChannelContext()->getSalesChannel()->getId();
if (!$this->systemConfigService->get('UltraWartung.config.activeKanal', $shopId)) {
return;
}
$event->getSalesChannelContext()->getSalesChannel()->assign(['UltraWartung' => $this->systemConfigService->get('UltraWartung.config', $event->getContext()->getSource()->getSalesChannelId())]);
}
public function ultrawartung(RequestEvent $event): void
{
$verkaufskanal=$event->getRequest()->attributes->get("sw-sales-channel-id");
$configit=$this->systemConfigService->get('UltraWartung.config.activeKanal', $verkaufskanal);
if($configit) {
$request = $event->getRequest();
$route = $request->attributes->get('_route');
if (!empty($route) and strpos($route, 'frontend') !== false) {
$ipadress=$request->getClientIp();
$eingang=0;
$result = $this->connection->fetchAllAssociative('SELECT ipadress, verkaufskanal, eingang FROM ultrawartung') ;
if($result) {
foreach($result as $value) {
if($value["eingang"]==1 and $value["ipadress"]==$ipadress and $value["verkaufskanal"]==$verkaufskanal) {
$eingang=1;
}
}
}
if($eingang === 0 and strpos($route, 'frontend.ultrawartung.ultrawartung') === false) {
$event->setResponse(
new RedirectResponse(
$this->router->generate('frontend.ultrawartung.ultrawartung')
)
);
}
}
}
}
}