|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * ScandiPWA - Progressive Web App for Magento |
| 4 | + * |
| 5 | + * Copyright © Scandiweb, Inc. All rights reserved. |
| 6 | + * See LICENSE for license details. |
| 7 | + * |
| 8 | + * @license OSL-3.0 (Open Software License ("OSL") v. 3.0) |
| 9 | + * @package scandipwa/module-customer-graph-ql |
| 10 | + * @link https://github.com/scandipwa/module-customer-graph-ql |
| 11 | + */ |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ScandiPWA\CustomerGraphQl\Model\Customer\Address; |
| 15 | + |
| 16 | +use Magento\CustomerGraphQl\Model\Customer\Address\ExtractCustomerAddressData; |
| 17 | +use Magento\CustomerGraphQl\Model\Customer\Address\ValidateAddress as SourceValidateAddress; |
| 18 | +use Magento\Customer\Api\Data\AddressInterface; |
| 19 | +use Magento\Customer\Api\Data\AddressInterfaceFactory; |
| 20 | +use Magento\Customer\Api\Data\RegionInterfaceFactory; |
| 21 | +use Magento\Directory\Helper\Data as DirectoryData; |
| 22 | +use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory; |
| 23 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 24 | + |
| 25 | +class ValidateAddress extends SourceValidateAddress |
| 26 | +{ |
| 27 | + /** |
| 28 | + * @var AddressInterfaceFactory |
| 29 | + */ |
| 30 | + private $addressFactory; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var RegionInterfaceFactory |
| 34 | + */ |
| 35 | + private $regionFactory; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var DirectoryData |
| 39 | + */ |
| 40 | + protected $directoryData; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var RegionCollectionFactory |
| 44 | + */ |
| 45 | + protected $regionCollectionFactory; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var ExtractCustomerAddressData |
| 49 | + */ |
| 50 | + protected $extractCustomerAddressData; |
| 51 | + |
| 52 | + /** |
| 53 | + * ValidateCustomerData constructor. |
| 54 | + * |
| 55 | + * @param AddressInterfaceFactory $addressFactory |
| 56 | + * @param RegionInterfaceFactory $regionFactory |
| 57 | + * @param DirectoryData $directoryData |
| 58 | + * @param RegionCollectionFactory $regionCollectionFactory |
| 59 | + * @param ExtractCustomerAddressData $extractCustomerAddressData |
| 60 | + */ |
| 61 | + public function __construct( |
| 62 | + AddressInterfaceFactory $addressFactory, |
| 63 | + RegionInterfaceFactory $regionFactory, |
| 64 | + DirectoryData $directoryData, |
| 65 | + RegionCollectionFactory $regionCollectionFactory, |
| 66 | + ExtractCustomerAddressData $extractCustomerAddressData |
| 67 | + ) { |
| 68 | + parent::__construct( |
| 69 | + $addressFactory, |
| 70 | + $regionFactory, |
| 71 | + $directoryData, |
| 72 | + $regionCollectionFactory, |
| 73 | + $extractCustomerAddressData |
| 74 | + ); |
| 75 | + |
| 76 | + $this->extractCustomerAddressData = $extractCustomerAddressData; |
| 77 | + $this->regionCollectionFactory = $regionCollectionFactory; |
| 78 | + $this->directoryData = $directoryData; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Validate customer address data |
| 83 | + * |
| 84 | + * @param AddressInterface $address |
| 85 | + * @throws GraphQlInputException |
| 86 | + */ |
| 87 | + public function execute(AddressInterface $address): void |
| 88 | + { |
| 89 | + $addressData = $this->extractCustomerAddressData->execute($address); |
| 90 | + |
| 91 | + if (!isset($addressData['country_code'])) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + $isRegionRequired = $this->directoryData->isRegionRequired($addressData['country_code']); |
| 96 | + |
| 97 | + if ($isRegionRequired && is_null($addressData['region']['region_id'])) { |
| 98 | + throw new GraphQlInputException(__('A region_id is required for the specified country code')); |
| 99 | + } |
| 100 | + |
| 101 | + $regionCollection = $this->regionCollectionFactory |
| 102 | + ->create() |
| 103 | + ->addCountryFilter($addressData['country_code']); |
| 104 | + |
| 105 | + if (!$isRegionRequired && |
| 106 | + !empty($addressData['region']['region_id']) && |
| 107 | + empty($regionCollection->getItemById($addressData['region']['region_id']))) { |
| 108 | + throw new GraphQlInputException( |
| 109 | + __('The region_id does not match the selected country or region') |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + if (!isset($addressData['region']['region_code'])) { |
| 114 | + $regionCollection->addRegionCodeFilter($addressData['region']['region_code']); |
| 115 | + } |
| 116 | + |
| 117 | + // In case if region required, but no options then we getting id 0 which is correct |
| 118 | + if ($addressData['region']['region_id'] === 0) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + if (empty($regionCollection->getItemById($addressData['region']['region_id']))) { |
| 123 | + throw new GraphQlInputException( |
| 124 | + __('The specified region is not a part of the selected country or region') |
| 125 | + ); |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments