<?php declare(strict_types=1);
namespace CobShopRedirect\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Doctrine\DBAL\Connection;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Shopware\Storefront\Framework\Routing\StorefrontResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class Subscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var connection
*/
private $connection;
public const SESSION_EXPIRE_TIME = 300; //This is in seconds 120 = 2 minutes
public function __construct(SystemConfigService $systemConfigService, Connection $connection , RouterInterface $router)
{
$this->systemConfigService = $systemConfigService;
$this->connection = $connection;
$this->router = $router;
}
public static function getSubscribedEvents(): array
{
return [
StorefrontRenderEvent::class => "onStorefrontRenderEventOne",
];
}
public function onStorefrontRenderEventOne(StorefrontRenderEvent $event)
{
$saleschannelId = $event->getSalesChannelContext()->getSalesChannel()->getId();
$request = $event->getRequest();
//~ if (isset($_SESSION['COBCREATED']) && time() - $_SESSION['COBCREATED'] > self::SESSION_EXPIRE_TIME) {
//~ unset($_SESSION['canNotAccessCOBSiteMain']);
//~ unset($_SESSION['COBCREATED']);
//~ }
$referalFrom = array('.mitarbeiterangebote.de','.rahmenvereinbarungen.de' , '.corporate-benefits.de');
$referalFromBenefits = array('.benefits.me');
$uri = $_SERVER['REQUEST_URI'];
if($saleschannelId == '45be9f42765d42e8ae1137d1812040f4') {
if(strpos($uri,'frontend.account.order.single.page') != false){
$_SESSION['canNotAccessCOBSiteMain'] = false;
$_SESSION['redirecttomain'] = true;
$redirectUrl = 'https://www.mactrade.de';
$redirectUrl .= $uri;
//~ echo $redirectUrl; exit;
header("Location:$redirectUrl");
exit();
return;
}
}
else if($saleschannelId == '4faca3b6a7f149cea7ade06ee340ce19' ) {
if(strpos($uri,'frontend.account.order.single.page') != false){
$_SESSION['canNotAccessBenefitsSiteMain'] = false;
$_SESSION['redirecttomain'] = true;
$redirectUrl = 'https://www.mactrade.de';
$redirectUrl .= $uri;
//~ echo $redirectUrl; exit;
header("Location:$redirectUrl");
exit();
return;
}
}
$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
if($url)
{
foreach ($referalFrom as $refer_url) {
if (strpos($url, $refer_url) != false ) {
$_SESSION['canNotAccessCOBSiteMain'] = false;
$_SESSION['redirecttomain'] = true;
}
}
foreach ($referalFromBenefits as $refer_url) {
if (strpos($url, $refer_url) != false ) {
$_SESSION['canNotAccessBenefitsSiteMain'] = false;
$_SESSION['redirecttomain'] = true;
}
}
}
if($saleschannelId == '45be9f42765d42e8ae1137d1812040f4' && !$url) {
if( empty($_SESSION['canNotAccessCOBSiteMain']) && empty($_SESSION['redirecttomain'])) {
$redirectUrl = 'https://www.mactrade.de/';
if($request->attributes->get('_route') =='frontend.detail.page') {
$rrqParam = $request->attributes->get('_route_params');
$redirectUrl .='detail/'.$rrqParam['productId'];
}
$_SESSION['redirecttomain'] = false;
header("Location:$redirectUrl");
exit();
}
}
else if($saleschannelId == '4faca3b6a7f149cea7ade06ee340ce19' && !$url) {
if( empty($_SESSION['canNotAccessBenefitsSiteMain']) && empty($_SESSION['redirecttomain'])) {
$redirectUrl = 'https://www.mactrade.de/';
if($request->attributes->get('_route') =='frontend.detail.page') {
$rrqParam = $request->attributes->get('_route_params');
$redirectUrl .='detail/'.$rrqParam['productId'];
}
$_SESSION['redirecttomain'] = false;
header("Location:$redirectUrl");
exit();
}
}
}
}