Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AddressRepository.php
Go to the documentation of this file.
1 <?php
9 
18 
25 {
31  protected $directoryData;
32 
36  protected $addressFactory;
37 
41  protected $addressRegistry;
42 
46  protected $customerRegistry;
47 
52 
57 
62 
67 
71  private $collectionProcessor;
72 
84  public function __construct(
85  \Magento\Customer\Model\AddressFactory $addressFactory,
89  \Magento\Directory\Helper\Data $directoryData,
90  \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory $addressSearchResultsFactory,
92  \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor,
93  CollectionProcessorInterface $collectionProcessor = null
94  ) {
95  $this->addressFactory = $addressFactory;
96  $this->addressRegistry = $addressRegistry;
97  $this->customerRegistry = $customerRegistry;
98  $this->addressResourceModel = $addressResourceModel;
99  $this->directoryData = $directoryData;
100  $this->addressSearchResultsFactory = $addressSearchResultsFactory;
101  $this->addressCollectionFactory = $addressCollectionFactory;
102  $this->extensionAttributesJoinProcessor = $extensionAttributesJoinProcessor;
103  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
104  }
105 
113  public function save(\Magento\Customer\Api\Data\AddressInterface $address)
114  {
115  $addressModel = null;
116  $customerModel = $this->customerRegistry->retrieve($address->getCustomerId());
117  if ($address->getId()) {
118  $addressModel = $this->addressRegistry->retrieve($address->getId());
119  }
120 
121  if ($addressModel === null) {
123  $addressModel = $this->addressFactory->create();
124  $addressModel->updateData($address);
125  $addressModel->setCustomer($customerModel);
126  } else {
127  $addressModel->updateData($address);
128  }
129  $addressModel->setStoreId($customerModel->getStoreId());
130 
131  $errors = $addressModel->validate();
132  if ($errors !== true) {
133  $inputException = new InputException();
134  foreach ($errors as $error) {
135  $inputException->addError($error);
136  }
137  throw $inputException;
138  }
139  $addressModel->save();
140  $address->setId($addressModel->getId());
141  // Clean up the customer registry since the Address save has a
142  // side effect on customer : \Magento\Customer\Model\ResourceModel\Address::_afterSave
143  $this->addressRegistry->push($addressModel);
144  $this->updateAddressCollection($customerModel, $addressModel);
145 
146  return $addressModel->getDataModel();
147  }
148 
157  private function updateAddressCollection(CustomerModel $customer, CustomerAddressModel $address)
158  {
159  $customer->getAddressesCollection()->removeItemByKey($address->getId());
160  $customer->getAddressesCollection()->addItem($address);
161  }
162 
170  public function getById($addressId)
171  {
172  $address = $this->addressRegistry->retrieve($addressId);
173  return $address->getDataModel();
174  }
175 
184  {
186  $collection = $this->addressCollectionFactory->create();
187  $this->extensionAttributesJoinProcessor->process(
188  $collection,
189  \Magento\Customer\Api\Data\AddressInterface::class
190  );
191 
192  $this->collectionProcessor->process($searchCriteria, $collection);
193 
195  $addresses = [];
197  foreach ($collection->getItems() as $address) {
198  $addresses[] = $this->getById($address->getId());
199  }
200 
202  $searchResults = $this->addressSearchResultsFactory->create();
203  $searchResults->setItems($addresses);
204  $searchResults->setSearchCriteria($searchCriteria);
205  $searchResults->setTotalCount($collection->getSize());
206  return $searchResults;
207  }
208 
219  {
220  $fields = [];
221  $conditions = [];
222  foreach ($filterGroup->getFilters() as $filter) {
223  $condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq';
224  $fields[] = ['attribute' => $filter->getField(), $condition => $filter->getValue()];
225  $conditions[] = [$condition => $filter->getValue()];
226  }
227  if ($fields) {
228  $collection->addFieldToFilter($fields, $conditions);
229  }
230  }
231 
240  {
241  $addressId = $address->getId();
242  $address = $this->addressRegistry->retrieve($addressId);
243  $customerModel = $this->customerRegistry->retrieve($address->getCustomerId());
244  $customerModel->getAddressesCollection()->clear();
245  $this->addressResourceModel->delete($address);
246  $this->addressRegistry->remove($addressId);
247  return true;
248  }
249 
258  public function deleteById($addressId)
259  {
260  $address = $this->addressRegistry->retrieve($addressId);
261  $customerModel = $this->customerRegistry->retrieve($address->getCustomerId());
262  $customerModel->getAddressesCollection()->removeItemByKey($addressId);
263  $this->addressResourceModel->delete($address);
264  $this->addressRegistry->remove($addressId);
265  return true;
266  }
267 
274  private function getCollectionProcessor()
275  {
276  if (!$this->collectionProcessor) {
277  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
278  'Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor'
279  );
280  }
281  return $this->collectionProcessor;
282  }
283 }
$customer
Definition: customers.php:11
$addresses
Definition: address_list.php:7
$fields
Definition: details.phtml:14
$searchCriteria
$address
Definition: customer.php:38
__construct(\Magento\Customer\Model\AddressFactory $addressFactory, \Magento\Customer\Model\AddressRegistry $addressRegistry, \Magento\Customer\Model\CustomerRegistry $customerRegistry, \Magento\Customer\Model\ResourceModel\Address $addressResourceModel, \Magento\Directory\Helper\Data $directoryData, \Magento\Customer\Api\Data\AddressSearchResultsInterfaceFactory $addressSearchResultsFactory, \Magento\Customer\Model\ResourceModel\Address\CollectionFactory $addressCollectionFactory, \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $extensionAttributesJoinProcessor, CollectionProcessorInterface $collectionProcessor=null)
save(\Magento\Customer\Api\Data\AddressInterface $address)
addFilterGroupToCollection(FilterGroup $filterGroup, Collection $collection)
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$errors
Definition: overview.phtml:9