custom/plugins/CobShopRedirect/src/Subscriber/Subscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace CobShopRedirect\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\System\SystemConfig\SystemConfigService;
  5. use Doctrine\DBAL\Connection;
  6. use Shopware\Storefront\Event\StorefrontRenderEvent;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Shopware\Storefront\Framework\Routing\StorefrontResponse;
  9. use Symfony\Component\Routing\RouterInterface;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  12. use Symfony\Component\HttpKernel\Event\RequestEvent;
  13. class Subscriber implements EventSubscriberInterface 
  14. {
  15.    /**
  16.      * @var SystemConfigService
  17.      */
  18.     private $systemConfigService;
  19.     /**
  20.      * @var connection
  21.      */
  22.     private $connection;
  23.     public const SESSION_EXPIRE_TIME 300//This is in seconds 120 = 2 minutes
  24.     public function __construct(SystemConfigService $systemConfigServiceConnection $connection RouterInterface $router)
  25.     {
  26.         $this->systemConfigService $systemConfigService;
  27.         $this->connection $connection;
  28.         $this->router $router;
  29.         
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             StorefrontRenderEvent::class  => "onStorefrontRenderEventOne",
  35.         ];
  36.     }
  37.     public function onStorefrontRenderEventOne(StorefrontRenderEvent $event)
  38.     {
  39.         $saleschannelId $event->getSalesChannelContext()->getSalesChannel()->getId();
  40.         $request $event->getRequest();
  41.         //~ if (isset($_SESSION['COBCREATED']) && time() - $_SESSION['COBCREATED'] > self::SESSION_EXPIRE_TIME) {
  42.             //~ unset($_SESSION['canNotAccessCOBSiteMain']);
  43.             //~ unset($_SESSION['COBCREATED']);
  44.         //~ } 
  45.         $referalFrom =  array('.mitarbeiterangebote.de','.rahmenvereinbarungen.de' '.corporate-benefits.de');
  46.         $referalFromBenefits =  array('.benefits.me');
  47.             $uri $_SERVER['REQUEST_URI'];
  48.             
  49.     if($saleschannelId == '45be9f42765d42e8ae1137d1812040f4') {
  50.         if(strpos($uri,'frontend.account.order.single.page') != false){
  51.             $_SESSION['canNotAccessCOBSiteMain'] = false;
  52.             $_SESSION['redirecttomain'] = true;
  53.             $redirectUrl 'https://www.mactrade.de';
  54.             $redirectUrl .= $uri;
  55.             
  56.             //~ echo $redirectUrl; exit;
  57.             header("Location:$redirectUrl");
  58.                     exit();
  59.                     return;
  60.         }
  61.         
  62.     }
  63.     else if($saleschannelId == '4faca3b6a7f149cea7ade06ee340ce19'  ) {
  64.         if(strpos($uri,'frontend.account.order.single.page') != false){
  65.             $_SESSION['canNotAccessBenefitsSiteMain'] = false;
  66.             $_SESSION['redirecttomain'] = true;
  67.             $redirectUrl 'https://www.mactrade.de';
  68.             $redirectUrl .= $uri;
  69.             
  70.             //~ echo $redirectUrl; exit;
  71.             header("Location:$redirectUrl");
  72.                     exit();
  73.                     return;
  74.         }
  75.     }
  76.     
  77.         $url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''
  78.         if($url
  79.         {
  80.             foreach ($referalFrom as $refer_url) {
  81.                 if (strpos($url$refer_url) != false ) {
  82.                     $_SESSION['canNotAccessCOBSiteMain'] = false;
  83.                     $_SESSION['redirecttomain'] = true;
  84.                 }
  85.             }
  86.             foreach ($referalFromBenefits as $refer_url) {
  87.                 if (strpos($url$refer_url) != false ) {
  88.                     $_SESSION['canNotAccessBenefitsSiteMain'] = false;
  89.                     $_SESSION['redirecttomain'] = true;
  90.                 }
  91.             }
  92.   
  93.         }
  94.         if($saleschannelId == '45be9f42765d42e8ae1137d1812040f4' && !$url) {
  95.             if( empty($_SESSION['canNotAccessCOBSiteMain']) && empty($_SESSION['redirecttomain'])) {
  96.                 $redirectUrl 'https://www.mactrade.de/';
  97.                 if($request->attributes->get('_route') =='frontend.detail.page') {
  98.                     $rrqParam $request->attributes->get('_route_params');
  99.                     $redirectUrl .='detail/'.$rrqParam['productId'];
  100.                 }            
  101.                 $_SESSION['redirecttomain'] = false;
  102.                 header("Location:$redirectUrl");
  103.                 exit();
  104.             } 
  105.         }
  106.         else if($saleschannelId == '4faca3b6a7f149cea7ade06ee340ce19' && !$url) {
  107.             if( empty($_SESSION['canNotAccessBenefitsSiteMain']) && empty($_SESSION['redirecttomain'])) {
  108.                 $redirectUrl 'https://www.mactrade.de/';
  109.                 if($request->attributes->get('_route') =='frontend.detail.page') {
  110.                     $rrqParam $request->attributes->get('_route_params');
  111.                     $redirectUrl .='detail/'.$rrqParam['productId'];
  112.                 }            
  113.                 $_SESSION['redirecttomain'] = false;
  114.                 header("Location:$redirectUrl");
  115.                 exit();
  116.             } 
  117.         }
  118.     
  119.     }
  120.     
  121. }