custom/plugins/EnderecoShopware6ClientStore/src/Service/AddDataToPage.php line 55

Open in your IDE?
  1. <?php
  2. namespace Endereco\Shopware6ClientStore\Service;
  3. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. class AddDataToPage implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var SystemConfigService
  14.      */
  15.     private $systemConfigService;
  16.     /**
  17.      * @var EntityRepository
  18.      */
  19.     private $countryRepository;
  20.     /**
  21.      * @var EntityRepository
  22.      */
  23.     private $stateRepository;
  24.     /**
  25.      * @var EntityRepository
  26.      */
  27.     private $pluginRepository;
  28.     public function __construct(
  29.         SystemConfigService $systemConfigService,
  30.         EntityRepository $countryRepository,
  31.         EntityRepository $stateRepository,
  32.         EntityRepository $pluginRepository
  33.     )
  34.     {
  35.         $this->systemConfigService $systemConfigService;
  36.         $this->countryRepository $countryRepository;
  37.         $this->stateRepository $stateRepository;
  38.         $this->pluginRepository $pluginRepository;
  39.     }
  40.     /** @return array<string, string> */
  41.     public static function getSubscribedEvents(): array
  42.     {
  43.         return [
  44.             GenericPageLoadedEvent::class => 'addEnderecoConfigToPage'
  45.         ];
  46.     }
  47.     public function addEnderecoConfigToPage(GenericPageLoadedEvent $event): void
  48.     {
  49.         $context $event->getContext();
  50.         $configContainer = new \stdClass();
  51.         $criteria = new Criteria();
  52.         $criteria->addFilter(new EqualsFilter('name''EnderecoShopware6ClientStore'));
  53.         $version $this->pluginRepository->search($criteria$context)->first()->getVersion();
  54.         $configContainer->enderecoAgentInfo 'Endereco Shopware6 Client v' $version;
  55.         $configContainer->enderecoVersion $version;
  56.         $configContainer->defaultCountrySelect $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoPreselectDefaultCountry');
  57.         $configContainer->defaultCountry $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoPreselectDefaultCountryCode');
  58.         $configContainer->enderecoApiKey $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoApiKey');
  59.         $configContainer->enderecoRemoteUrl $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoRemoteUrl');
  60.         $configContainer->enderecoTriggerOnBlur $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoTriggerOnBlur');
  61.         $configContainer->enderecoTriggerOnSubmit $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoTriggerOnSubmit');
  62.         $configContainer->enderecoSmartAutocomplete $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoSmartAutocomplete');
  63.         $configContainer->enderecoContinueSubmit $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoContinueSubmit');
  64.         $configContainer->enderecoAllowCloseIcon $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoAllowCloseIcon');
  65.         $configContainer->enderecoConfirmWithCheckbox $this->systemConfigService->get('EnderecoShopware6ClientStore.config.enderecoConfirmWithCheckbox');
  66.         $countries $this->countryRepository->search(new Criteria(), $context);
  67.         $mapping = [];
  68.         $mappingReverse = [];
  69.         $codeToNameMapping = [];
  70.         foreach ($countries as $country) {
  71.             $mapping[strtoupper($country->getIso())] = $country->getId();
  72.             $mappingReverse[$country->getId()] = strtoupper($country->getIso());
  73.             $codeToNameMapping[strtoupper($country->getIso())] = $country->getName();
  74.         }
  75.         $configContainer->countryCodeToNameMapping str_replace("'""\'"json_encode($codeToNameMapping));
  76.         $configContainer->countryMapping str_replace("'""\'"json_encode($mapping));
  77.         $configContainer->countryMappingReverse str_replace("'""\'"json_encode($mappingReverse));
  78.         $states $this->stateRepository->search(new Criteria(), $context);
  79.         $statesMapping = [];
  80.         $statesMappingReverse = [];
  81.         $statesCodeToNameMapping = [];
  82.         foreach ($states as $state) {
  83.             $statesMapping[strtoupper($state->getShortCode())] = $state->getId();
  84.             $statesMappingReverse[$state->getId()] = strtoupper($state->getShortCode());
  85.             $statesCodeToNameMapping[strtoupper($state->getShortCode())] = $state->getName();
  86.         }
  87.         $configContainer->subdivisionCodeToNameMapping str_replace("'""\'"json_encode($statesCodeToNameMapping));
  88.         $configContainer->subdivisionMapping str_replace("'""\'"json_encode($statesMapping));
  89.         $configContainer->subdivisionMappingReverse str_replace("'""\'"json_encode($statesMappingReverse));
  90.         $event->getPage()->assign(['endereco_config' => $configContainer]);
  91.     }
  92. }