custom/plugins/DatoElectronicInvoicingSw6/src/Storefront/Controller/AddressController.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SmartDato\DatoElectronicInvoicing\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException;
  4. use Shopware\Core\Checkout\Customer\CustomerEntity;
  5. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  6. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  7. use Shopware\Core\Framework\Validation\DataBag\DataBag;
  8. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Controller\AddressController as AddressControllerInner;
  11. use Shopware\Storefront\Framework\Routing\StorefrontResponse;
  12. use SmartDato\DatoElectronicInvoicing\Service\PluginConfig\PluginConfigService;
  13. use SmartDato\DatoElectronicInvoicing\Validation\Constraint\FiscalCodeValidator;
  14. use SmartDato\DatoElectronicInvoicing\Validation\Constraint\MainValidator;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  19. use Shopware\Core\Framework\Routing\Annotation\Since;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. /**
  22.  * @RouteScope(scopes={"storefront"})
  23.  */
  24. class AddressController extends StorefrontBaseController
  25. {
  26.     /** @var AddressControllerInner */
  27.     private $addressControllerInner;
  28.     /** @var PluginConfigService */
  29.     private $pluginConfigService;
  30.     /**
  31.      * AddressController constructor.
  32.      *
  33.      * @param AddressControllerInner $addressControllerInner
  34.      * @param PluginConfigService $pluginConfigService
  35.      */
  36.     public function __construct(
  37.         $addressControllerInner,
  38.         PluginConfigService $pluginConfigService
  39.     )
  40.     {
  41.         $this->addressControllerInner $addressControllerInner;
  42.         $this->pluginConfigService $pluginConfigService;
  43.     }
  44.     /**
  45.      * @Since("6.0.0.0")
  46.      * @LoginRequired()
  47.      * @Route("/account/address", name="frontend.account.address.page", options={"seo"="false"}, methods={"GET"})
  48.      *
  49.      * @throws CustomerNotLoggedInException
  50.      */
  51.     public function accountAddressOverview(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  52.     {
  53.         return $this->addressControllerInner->accountAddressOverview($request$context$customer);
  54.     }
  55.     /**
  56.      * @Since("6.0.0.0")
  57.      * @LoginRequired()
  58.      * @Route("/account/address/create", name="frontend.account.address.create.page", options={"seo"="false"}, methods={"GET"})
  59.      *
  60.      * @throws CustomerNotLoggedInException
  61.      */
  62.     public function accountCreateAddress(Request $requestRequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  63.     {
  64.         return $this->addressControllerInner->accountCreateAddress($request$data$context$customer);
  65.     }
  66.     /**
  67.      * @Since("6.0.0.0")
  68.      * @LoginRequired()
  69.      * @Route("/account/address/{addressId}", name="frontend.account.address.edit.page", options={"seo"="false"}, methods={"GET"})
  70.      *
  71.      * @throws CustomerNotLoggedInException
  72.      */
  73.     public function accountEditAddress(Request $requestSalesChannelContext $contextCustomerEntity $customer): Response
  74.     {
  75.         return $this->addressControllerInner->accountEditAddress($request$context$customer);
  76.     }
  77.     /**
  78.      * @Since("6.0.0.0")
  79.      * @LoginRequired()
  80.      * @Route("/account/address/default-{type}/{addressId}", name="frontend.account.address.set-default-address", methods={"POST"})
  81.      *
  82.      * @throws CustomerNotLoggedInException
  83.      * @throws InvalidUuidException
  84.      */
  85.     public function switchDefaultAddress(string $typestring $addressIdSalesChannelContext $contextCustomerEntity $customer): RedirectResponse
  86.     {
  87.         return $this->addressControllerInner->switchDefaultAddress($type$addressId$context$customer);
  88.     }
  89.     /**
  90.      * @Since("6.0.0.0")
  91.      * @LoginRequired()
  92.      * @Route("/account/address/delete/{addressId}", name="frontend.account.address.delete", options={"seo"="false"}, methods={"POST"})
  93.      *
  94.      * @throws CustomerNotLoggedInException
  95.      */
  96.     public function deleteAddress(string $addressIdSalesChannelContext $contextCustomerEntity $customer): Response
  97.     {
  98.         return $this->addressControllerInner->deleteAddress($addressId$context$customer);
  99.     }
  100.     /**
  101.      * @Since("6.0.0.0")
  102.      * @LoginRequired()
  103.      * @Route("/account/address/create", name="frontend.account.address.create", options={"seo"="false"}, methods={"POST"})
  104.      * @Route("/account/address/{addressId}", name="frontend.account.address.edit.save", options={"seo"="false"}, methods={"POST"})
  105.      *
  106.      * @throws CustomerNotLoggedInException
  107.      */
  108.     public function saveAddress(RequestDataBag $dataSalesChannelContext $contextCustomerEntity $customer): Response
  109.     {
  110.         $this->handleSaveAddress($data$context);
  111.         return $this->addressControllerInner->saveAddress($data$context$customer);
  112.     }
  113.     /**
  114.      * @Since("6.0.0.0")
  115.      * @LoginRequired(allowGuest=true)
  116.      * @Route("/widgets/account/address-book", name="frontend.account.addressbook", options={"seo"=true}, methods={"POST"}, defaults={"XmlHttpRequest"=true})
  117.      */
  118.     public function addressBook(Request $requestRequestDataBag $dataBagSalesChannelContext $contextCustomerEntity $customer): Response
  119.     {
  120.         $this->handleAddressBook($dataBag$context);
  121.         /** @var StorefrontResponse $response */
  122.         $response $this->addressControllerInner->addressBook($request$dataBag$context$customer);
  123.         $responseData $response->getData();
  124.         if (empty($responseData['messages'])) {
  125.             return $response;
  126.         }
  127.         if (empty($responseData['messages']['type'])) {
  128.             return $response;
  129.         }
  130.         if (empty($responseData['messages']['text'])) {
  131.             return $response;
  132.         }
  133.         $this->addFlash($responseData['messages']['type'], $responseData['messages']['text']);
  134.         return $response;
  135.     }
  136.     /**
  137.      * Handle save address
  138.      *
  139.      * @param RequestDataBag $data
  140.      * @param SalesChannelContext $context
  141.      */
  142.     private function handleSaveAddress(RequestDataBag $dataSalesChannelContext $context): void
  143.     {
  144.         if (($address $data->get('address')) instanceof DataBag) {
  145.             $this->addFiscalCodeAndInvoicingFieldsValidationDefinitions($address$context);
  146.         }
  147.     }
  148.     /**
  149.      * Handle address book
  150.      *
  151.      * @param RequestDataBag $dataBag
  152.      * @param SalesChannelContext $context
  153.      */
  154.     private function handleAddressBook(RequestDataBag $dataBagSalesChannelContext $context): void
  155.     {
  156.         $changeableAddresses $dataBag->get('changeableAddresses');
  157.         if ($changeableAddresses instanceof DataBag && $changeableAddresses->get('changeBilling')) {
  158.             $address $dataBag->get('address');
  159.             if ($address instanceof DataBag) {
  160.                 $this->addFiscalCodeAndInvoicingFieldsValidationDefinitions($address$context);
  161.             }
  162.         }
  163.     }
  164.     /**
  165.      * Add fiscal code and invoicing fields validation definitions to validate address before save
  166.      *
  167.      * @param DataBag $address
  168.      * @param SalesChannelContext $context
  169.      */
  170.     private function addFiscalCodeAndInvoicingFieldsValidationDefinitions(DataBag $addressSalesChannelContext $context): void
  171.     {
  172.         if ($context->getCustomer() === null) {
  173.             return;
  174.         }
  175.         if (!$this->isEditedAddressIsBilling($address$context->getCustomer()->getDefaultBillingAddressId())) {
  176.             return;
  177.         }
  178.         $validationConfig $this->pluginConfigService->getValidationConfig($context->getSalesChannel()->getId());
  179.         if (!$validationConfig->active) {
  180.             return;
  181.         }
  182.         MainValidator::createConstraintsAndAddToContext(
  183.             $context->getContext(),
  184.             FiscalCodeValidator::getCorrectCustomerType((string)$address->getAlpha('accountType')),
  185.             (string)$address->getAlnum('countryId'),
  186.             $validationConfig
  187.         );
  188.     }
  189.     /**
  190.      * Check that the edited address is the billing address
  191.      * If address id not found mean that address is created and not edited
  192.      *
  193.      * @param DataBag $address
  194.      * @param string $defaultBillingAddressId
  195.      *
  196.      * @return bool
  197.      */
  198.     private function isEditedAddressIsBilling(DataBag $addressstring $defaultBillingAddressId): bool
  199.     {
  200.         return ($addressId $address->get('id')) && $addressId === $defaultBillingAddressId;
  201.     }
  202. }