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
8 
11 use Magento\Sales\Api\Data\OrderAddressSearchResultInterfaceFactory as SearchResultFactory;
16 
23 {
27  protected $metadata;
28 
32  protected $searchResultFactory = null;
33 
37  protected $registry = [];
38 
42  private $collectionProcessor;
43 
50  public function __construct(
52  SearchResultFactory $searchResultFactory,
53  CollectionProcessorInterface $collectionProcessor = null
54  ) {
55  $this->metadata = $metadata;
56  $this->searchResultFactory = $searchResultFactory;
57  $this->collectionProcessor = $collectionProcessor ?: $this->getCollectionProcessor();
58  }
59 
68  public function get($id)
69  {
70  if (!$id) {
71  throw new InputException(__('An ID is needed. Set the ID and try again.'));
72  }
73 
74  if (!isset($this->registry[$id])) {
76  $entity = $this->metadata->getNewInstance()->load($id);
77  if (!$entity->getEntityId()) {
78  throw new NoSuchEntityException(
79  __("The entity that was requested doesn't exist. Verify the entity and try again.")
80  );
81  }
82 
83  $this->registry[$id] = $entity;
84  }
85 
86  return $this->registry[$id];
87  }
88 
95  public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
96  {
98  $searchResult = $this->searchResultFactory->create();
99  $this->collectionProcessor->process($searchCriteria, $searchResult);
100  $searchResult->setSearchCriteria($searchCriteria);
101 
102  return $searchResult;
103  }
104 
113  {
114  try {
115  $this->metadata->getMapper()->delete($entity);
116 
117  unset($this->registry[$entity->getEntityId()]);
118  } catch (\Exception $e) {
119  throw new CouldNotDeleteException(__("The order address couldn't be deleted."), $e);
120  }
121 
122  return true;
123  }
124 
131  public function deleteById($id)
132  {
133  $entity = $this->get($id);
134 
135  return $this->delete($entity);
136  }
137 
145  public function save(\Magento\Sales\Api\Data\OrderAddressInterface $entity)
146  {
147  try {
148  $this->metadata->getMapper()->save($entity);
149  $this->registry[$entity->getEntityId()] = $entity;
150  } catch (\Exception $e) {
151  throw new CouldNotSaveException(__("The order address couldn't be saved."), $e);
152  }
153 
154  return $this->registry[$entity->getEntityId()];
155  }
156 
162  public function create()
163  {
164  return $this->metadata->getNewInstance();
165  }
166 
173  private function getCollectionProcessor()
174  {
175  if (!$this->collectionProcessor) {
176  $this->collectionProcessor = \Magento\Framework\App\ObjectManager::getInstance()->get(
177  \Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface::class
178  );
179  }
180  return $this->collectionProcessor;
181  }
182 }
getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
$id
Definition: fieldset.phtml:14
__construct(Metadata $metadata, SearchResultFactory $searchResultFactory, CollectionProcessorInterface $collectionProcessor=null)
__()
Definition: __.php:13
$searchCriteria
$entity
Definition: element.phtml:22
save(\Magento\Sales\Api\Data\OrderAddressInterface $entity)