custom/plugins/econsorsetup/src/Subscriber/EconsorSetupConfigSubscriber.php line 70

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace EconsorSetup\Subscriber;
  4. use Exception;
  5. use Shopware\Core\Content\Cms\SalesChannel\Struct\TextStruct;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\System\Language\LanguageEntity;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  12. use Shopware\Storefront\Page\PageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  15. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. /**
  18.  * @psalm-type TEconsorSetupConfig = array{
  19.  *    strictTransportSecurityEnabled: bool,
  20.  *    strictTransportSecurity: string,
  21.  *    xFrameOptionsEnabled: bool,
  22.  *    xFrameOptions: string,
  23.  *    xXssProtectionEnabled: bool,
  24.  *    xXssProtection: string,
  25.  *    xContentTypeOptionsEnabled: bool,
  26.  *    xContentTypeOptions: string,
  27.  *    referrerPolicyEnabled: bool,
  28.  *    referrerPolicy: string,
  29.  *    permissionsPolicyEnabled: bool,
  30.  *    permissionsPolicy: string,
  31.  *    contentSecurityPolicyEnabled: bool,
  32.  *    contentSecurityPolicy: string,
  33.  * }
  34.  */
  35. class EconsorSetupConfigSubscriber implements EventSubscriberInterface
  36. {
  37.     private const FOOTER_LINK 'Umsetzung: ' .
  38.         '<a href="https://www.econsor.de/shopware/" title="Shopware-Agentur" target="_blank">' .
  39.             'Shopware-Agentur ECONSOR' .
  40.         '</a>';
  41.     /**
  42.      * @var SystemConfigService
  43.      */
  44.     private $systemConfigService;
  45.     /**
  46.      * @var EntityRepository
  47.      */
  48.     private $languageRepo;
  49.     public function __construct(SystemConfigService $systemConfigServiceEntityRepository $languageRepo)
  50.     {
  51.         $this->systemConfigService $systemConfigService;
  52.         $this->languageRepo $languageRepo;
  53.     }
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         return [
  57.             GenericPageLoadedEvent::class => 'onPageLoaded',
  58.             KernelEvents::RESPONSE => 'handleRequest'
  59.         ];
  60.     }
  61.     public function onPageLoaded(PageLoadedEvent $event): void
  62.     {
  63.         $page $event->getPage();
  64.         $route = (string)$event->getRequest()->attributes->get('_route');
  65.         if ('frontend.home.page' === $route) {
  66.             $page->assign(['footerConfig' => self::FOOTER_LINK]);
  67.         }
  68.         $page->assign([
  69.             'headTag' => $this->systemConfigService->get('EconsorSetup.config.headTag')
  70.         ]);
  71.         $page->assign([
  72.             'bodyTag' => $this->systemConfigService->get('EconsorSetup.config.bodyTag')
  73.         ]);
  74.         $page->assign([
  75.             'cookieFirst' => $this->systemConfigService->get('EconsorSetup.config.cookieFirst')
  76.         ]);
  77.         $page->assign([
  78.             'cookiePreferencesLinkEnabled' => $this->systemConfigService->get(
  79.                 'EconsorSetup.config.cookiePreferencesLinkEnabled'
  80.             )
  81.         ]);
  82.         $criteria = (new Criteria())->addFilter(
  83.             new EqualsFilter('id'$event->getSalesChannelContext()->getSalesChannel()->getLanguageId())
  84.         )->addAssociation('locale');
  85.         try {
  86.             $result $this->languageRepo->search($criteria$event->getContext());
  87.             if ($result->getTotal()) {
  88.                 $entityCollection $result->getEntities();
  89.                 /** @var LanguageEntity $language */
  90.                 $language $entityCollection->first();
  91.                 $struct = new TextStruct();
  92.                 $locale $language->getLocale();
  93.                 if (null !== $locale) {
  94.                     $struct->setContent($locale->getCode());
  95.                     $event->getContext()->addExtension('localeLanguage'$struct);
  96.                 }
  97.             }
  98.         } catch (Exception $e) {
  99.             /** TODO: log error */
  100.         }
  101.     }
  102.     public function handleRequest(ResponseEvent $event): void
  103.     {
  104.         $this->setSecurityHeaders($event->getResponse()->headers);
  105.     }
  106.     private function setSecurityHeaders(ResponseHeaderBag $headers): void
  107.     {
  108.         $config $this->systemConfigService->get('EconsorSetup.config');
  109.         assert(is_array($config));
  110.         /** @var TEconsorSetupConfig $config */
  111.         if ($config['strictTransportSecurityEnabled']) {
  112.             $headers->set('Strict-Transport-Security'$config['strictTransportSecurity'], true);
  113.         }
  114.         if ($config['xFrameOptionsEnabled']) {
  115.             $headers->set('X-Frame-Options'$config['xFrameOptions'], true);
  116.         }
  117.         if ($config['xXssProtectionEnabled']) {
  118.             $headers->set('X-XSS-Protection'$config['xXssProtection'], true);
  119.         }
  120.         if ($config['xContentTypeOptionsEnabled']) {
  121.             $headers->set('X-Content-Type-Options'$config['xContentTypeOptions'], true);
  122.         }
  123.         if ($config['referrerPolicyEnabled']) {
  124.             $headers->set('Referrer-Policy'$config['referrerPolicy'], true);
  125.         }
  126.         if ($config['permissionsPolicyEnabled']) {
  127.             $headers->set('Permissions-Policy'$config['permissionsPolicy'], true);
  128.         }
  129.         if ($config['contentSecurityPolicyEnabled']) {
  130.             $headers->set('Content-Security-Policy'$config['contentSecurityPolicy'], true);
  131.         }
  132.     }
  133. }