|
| 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\Customer\Api\AddressRepositoryInterface; |
| 17 | +use Magento\Customer\Api\Data\AddressInterface; |
| 18 | +use Magento\CustomerGraphQl\Model\Customer\Address\GetAllowedAddressAttributes; |
| 19 | +use Magento\CustomerGraphQl\Model\Customer\Address\PopulateCustomerAddressFromInput; |
| 20 | +use Magento\CustomerGraphQl\Model\Customer\Address\UpdateCustomerAddress as SourceAddress; |
| 21 | +use Magento\Directory\Helper\Data as DirectoryData; |
| 22 | +use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory; |
| 23 | +use Magento\Framework\Api\DataObjectHelper; |
| 24 | +use Magento\Framework\Exception\LocalizedException; |
| 25 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 26 | + |
| 27 | +/** |
| 28 | + * @inheridoc |
| 29 | + */ |
| 30 | +class UpdateCustomerAddress extends SourceAddress |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var AddressRepositoryInterface |
| 34 | + */ |
| 35 | + protected $addressRepository; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var DataObjectHelper |
| 39 | + */ |
| 40 | + protected $dataObjectHelper; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var array |
| 44 | + */ |
| 45 | + protected $restrictedKeys; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var ValidateAddress |
| 49 | + */ |
| 50 | + protected $addressValidator; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var PopulateCustomerAddressFromInput |
| 54 | + */ |
| 55 | + protected $populateCustomerAddressFromInput; |
| 56 | + |
| 57 | + /** |
| 58 | + * @param GetAllowedAddressAttributes $getAllowedAddressAttributes |
| 59 | + * @param AddressRepositoryInterface $addressRepository |
| 60 | + * @param DataObjectHelper $dataObjectHelper |
| 61 | + * @param DirectoryData $directoryData |
| 62 | + * @param RegionCollectionFactory $regionCollectionFactory |
| 63 | + * @param ValidateAddress $addressValidator |
| 64 | + * @param PopulateCustomerAddressFromInput $populateCustomerAddressFromInput |
| 65 | + * @param array $restrictedKeys |
| 66 | + */ |
| 67 | + public function __construct( |
| 68 | + GetAllowedAddressAttributes $getAllowedAddressAttributes, |
| 69 | + AddressRepositoryInterface $addressRepository, |
| 70 | + DataObjectHelper $dataObjectHelper, |
| 71 | + DirectoryData $directoryData, |
| 72 | + RegionCollectionFactory $regionCollectionFactory, |
| 73 | + ValidateAddress $addressValidator, |
| 74 | + PopulateCustomerAddressFromInput $populateCustomerAddressFromInput, |
| 75 | + array $restrictedKeys = [] |
| 76 | + ) { |
| 77 | + parent::__construct( |
| 78 | + $getAllowedAddressAttributes, |
| 79 | + $addressRepository, |
| 80 | + $dataObjectHelper, |
| 81 | + $directoryData, |
| 82 | + $regionCollectionFactory, |
| 83 | + $addressValidator, |
| 84 | + $populateCustomerAddressFromInput, |
| 85 | + $restrictedKeys |
| 86 | + ); |
| 87 | + |
| 88 | + $this->addressRepository = $addressRepository; |
| 89 | + $this->dataObjectHelper = $dataObjectHelper; |
| 90 | + $this->addressValidator = $addressValidator; |
| 91 | + $this->populateCustomerAddressFromInput = $populateCustomerAddressFromInput; |
| 92 | + $this->restrictedKeys = $restrictedKeys; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @inheridoc |
| 97 | + */ |
| 98 | + public function execute(AddressInterface $address, array $data): void |
| 99 | + { |
| 100 | + if (isset($data['country_code'])) { |
| 101 | + $data['country_id'] = $data['country_code']; |
| 102 | + } |
| 103 | + $this->validateData($data); |
| 104 | + |
| 105 | + $filteredData = array_diff_key($data, array_flip($this->restrictedKeys)); |
| 106 | + $this->dataObjectHelper->populateWithArray($address, $filteredData, AddressInterface::class); |
| 107 | + |
| 108 | + if (!empty($data['region']['region_id'])) { |
| 109 | + $address->setRegionId($address->getRegion()->getRegionId()); |
| 110 | + } else { |
| 111 | + $data['region']['region_id'] = null; |
| 112 | + |
| 113 | + # If new address doesn't have selectable region, make sure that old region id is removed from DB |
| 114 | + $address->setRegionId(null); |
| 115 | + } |
| 116 | + |
| 117 | + $this->populateCustomerAddressFromInput->execute($address, $filteredData); |
| 118 | + $this->addressValidator->execute($address); |
| 119 | + |
| 120 | + try { |
| 121 | + $this->addressRepository->save($address); |
| 122 | + } catch (LocalizedException $e) { |
| 123 | + throw new GraphQlInputException(__($e->getMessage()), $e); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments