CountryRepository.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. * MageSpecialist
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to info@magespecialist.it so we can send you a copy immediately.
  14. *
  15. * @category MSP
  16. * @package MSP_TwoFactorAuth
  17. * @copyright Copyright (c) 2017 Skeeller srl (http://www.magespecialist.it)
  18. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  19. */
  20. /**
  21. * Automatically created by MageSpecialist CodeMonkey
  22. * https://github.com/magespecialist/m2-MSP_CodeMonkey
  23. */
  24. namespace MSP\TwoFactorAuth\Model\ResourceModel;
  25. use Magento\Framework\Api\ExtensibleDataObjectConverter;
  26. /**
  27. * @SuppressWarnings(PHPMD.ShortVariable)
  28. * @SuppressWarnings(PHPMD.LongVariable)
  29. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  30. */
  31. class CountryRepository implements \MSP\TwoFactorAuth\Api\CountryRepositoryInterface
  32. {
  33. /**
  34. * @var \MSP\TwoFactorAuth\Api\Data\CountryInterfaceFactory
  35. */
  36. private $countryFactory;
  37. /**
  38. * @var \MSP\TwoFactorAuth\Model\ResourceModel\Country
  39. */
  40. private $resource;
  41. /**
  42. * @var \MSP\TwoFactorAuth\Model\ResourceModel\Country\CollectionFactory
  43. */
  44. private $collectionFactory;
  45. /**
  46. * @var \MSP\TwoFactorAuth\Api\Data\CountrySearchResultsInterfaceFactory
  47. */
  48. private $searchResultsFactory;
  49. /**
  50. * @var \MSP\TwoFactorAuth\Model\CountryRegistry
  51. */
  52. private $registry;
  53. /**
  54. * @var ExtensibleDataObjectConverter
  55. */
  56. private $extensibleDataObjectConverter;
  57. public function __construct(
  58. \MSP\TwoFactorAuth\Model\CountryFactory $countryFactory,
  59. \MSP\TwoFactorAuth\Model\ResourceModel\Country $resource,
  60. \MSP\TwoFactorAuth\Model\ResourceModel\Country\CollectionFactory $collectionFactory,
  61. \MSP\TwoFactorAuth\Api\Data\CountrySearchResultsInterfaceFactory $searchResultsFactory,
  62. \MSP\TwoFactorAuth\Model\CountryRegistry $registry,
  63. ExtensibleDataObjectConverter $extensibleDataObjectConverter
  64. ) {
  65. $this->searchResultsFactory = $searchResultsFactory;
  66. $this->countryFactory = $countryFactory;
  67. $this->resource = $resource;
  68. $this->registry = $registry;
  69. $this->collectionFactory = $collectionFactory;
  70. $this->extensibleDataObjectConverter = $extensibleDataObjectConverter;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function save(\MSP\TwoFactorAuth\Api\Data\CountryInterface $country)
  76. {
  77. $countryData = $this->extensibleDataObjectConverter->toNestedArray(
  78. $country,
  79. [],
  80. \MSP\TwoFactorAuth\Api\Data\CountryInterface::class
  81. );
  82. /** @var \MSP\TwoFactorAuth\Model\Country $countryModel */
  83. $countryModel = $this->countryFactory->create(['data' => $countryData]);
  84. $countryModel->setDataChanges(true);
  85. $this->resource->save($countryModel);
  86. $country->setId($countryModel->getId());
  87. $this->registry->push($countryModel);
  88. return $this->getById($countryModel->getId());
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getById($id)
  94. {
  95. $fromRegistry = $this->registry->retrieveById($id);
  96. if ($fromRegistry === null) {
  97. $country = $this->countryFactory->create();
  98. $this->resource->load($country, $id);
  99. if (!$country->getId()) {
  100. throw new \Magento\Framework\Exception\NoSuchEntityException(__('No such Country'));
  101. }
  102. $this->registry->push($country);
  103. }
  104. return $this->registry->retrieveById($id);
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getByCode($value)
  110. {
  111. $fromRegistry = $this->registry->retrieveByCode($value);
  112. if ($fromRegistry === null) {
  113. $country = $this->countryFactory->create();
  114. $this->resource->load($country, $value, 'code');
  115. if (!$country->getId()) {
  116. throw new \Magento\Framework\Exception\NoSuchEntityException(__('No such Country'));
  117. }
  118. $this->registry->push($country);
  119. }
  120. return $this->registry->retrieveByCode($value);
  121. }
  122. /**
  123. * {@inheritdoc}
  124. */
  125. public function delete(\MSP\TwoFactorAuth\Api\Data\CountryInterface $country)
  126. {
  127. $countryData = $this->extensibleDataObjectConverter->toNestedArray(
  128. $country,
  129. [],
  130. \MSP\TwoFactorAuth\Api\Data\CountryInterface::class
  131. );
  132. /** @var \MSP\TwoFactorAuth\Model\Country $countryModel */
  133. $countryModel = $this->countryFactory->create(['data' => $countryData]);
  134. $countryModel->setData($this->resource->getIdFieldName(), $country->getId());
  135. $this->resource->delete($countryModel);
  136. $this->registry->removeById($countryModel->getId());
  137. return true;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
  143. {
  144. /** @var \MSP\TwoFactorAuth\Api\Data\CountrySearchResultsInterface $searchResults */
  145. $searchResults = $this->searchResultsFactory->create();
  146. $searchResults->setSearchCriteria($searchCriteria);
  147. /** @var \MSP\TwoFactorAuth\Model\ResourceModel\Country\Collection $collection */
  148. $collection = $this->countryFactory->create()->getCollection();
  149. $this->applySearchCriteriaToCollection($searchCriteria, $collection);
  150. $items = $this->convertCollectionToDataItemsArray($collection);
  151. $searchResults->setTotalCount($collection->getSize());
  152. $searchResults->setItems($items);
  153. return $searchResults;
  154. }
  155. private function addFilterGroupToCollection(
  156. \Magento\Framework\Api\Search\FilterGroup $filterGroup,
  157. \MSP\TwoFactorAuth\Model\ResourceModel\Country\Collection $collection
  158. ) {
  159. $fields = [];
  160. $conditions = [];
  161. foreach ($filterGroup->getFilters() as $filter) {
  162. $condition = $filter->getConditionType() ?: 'eq';
  163. $fields[] = $filter->getField();
  164. $conditions[] = [$condition => $filter->getValue()];
  165. }
  166. if ($fields) {
  167. $collection->addFieldToFilter($fields, $conditions);
  168. }
  169. }
  170. private function convertCollectionToDataItemsArray(
  171. \MSP\TwoFactorAuth\Model\ResourceModel\Country\Collection $collection
  172. ) {
  173. $vendors = array_map(function (\MSP\TwoFactorAuth\Model\Country $item) {
  174. return $item->getDataModel();
  175. }, $collection->getItems());
  176. return $vendors;
  177. }
  178. private function applySearchCriteriaToCollection(
  179. \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria,
  180. \MSP\TwoFactorAuth\Model\ResourceModel\Country\Collection $collection
  181. ) {
  182. $this->applySearchCriteriaFiltersToCollection($searchCriteria, $collection);
  183. $this->applySearchCriteriaSortOrdersToCollection($searchCriteria, $collection);
  184. $this->applySearchCriteriaPagingToCollection($searchCriteria, $collection);
  185. }
  186. private function applySearchCriteriaFiltersToCollection(
  187. \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria,
  188. \MSP\TwoFactorAuth\Model\ResourceModel\Country\Collection $collection
  189. ) {
  190. foreach ($searchCriteria->getFilterGroups() as $group) {
  191. $this->addFilterGroupToCollection($group, $collection);
  192. }
  193. }
  194. private function applySearchCriteriaSortOrdersToCollection(
  195. \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria,
  196. \MSP\TwoFactorAuth\Model\ResourceModel\Country\Collection $collection
  197. ) {
  198. $sortOrders = $searchCriteria->getSortOrders();
  199. if ($sortOrders) {
  200. foreach ($sortOrders as $sortOrder) {
  201. $isAscending = $sortOrder->getDirection() == \Magento\Framework\Api\SortOrder::SORT_ASC;
  202. $collection->addOrder($sortOrder->getField(), $isAscending ? 'ASC' : 'DESC');
  203. }
  204. }
  205. }
  206. private function applySearchCriteriaPagingToCollection(
  207. \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria,
  208. \MSP\TwoFactorAuth\Model\ResourceModel\Country\Collection $collection
  209. ) {
  210. $collection->setCurPage($searchCriteria->getCurrentPage());
  211. $collection->setPageSize($searchCriteria->getPageSize());
  212. }
  213. }