Collection.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Model\ResourceModel\Webservice;
  6. use Magento\Framework\Api\Filter;
  7. use Magento\Framework\Api\FilterBuilder;
  8. use Magento\Framework\Api\Search\AggregationInterface;
  9. use Magento\Framework\Api\Search\DocumentInterface;
  10. use Magento\Framework\Api\Search\SearchResultInterface;
  11. use Magento\Framework\Api\SearchCriteriaBuilder;
  12. use Magento\Framework\Api\SearchCriteriaInterface;
  13. use Magento\Framework\Data\Collection as DataCollection;
  14. use Magento\Framework\Data\Collection\EntityFactoryInterface;
  15. use Magento\Framework\Message\ManagerInterface;
  16. use Magento\Framework\View\Element\UiComponent\DataProvider\Document;
  17. /**
  18. * Temando API Resource Collection
  19. *
  20. * @package Temando\Shipping\Model
  21. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  22. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  23. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * @link https://www.temando.com/
  25. */
  26. abstract class Collection extends DataCollection implements SearchResultInterface
  27. {
  28. /**
  29. * @var string
  30. */
  31. protected $_itemObjectClass = Document::class;
  32. /**
  33. * @var ManagerInterface
  34. */
  35. private $messageManager;
  36. /**
  37. * @var FilterBuilder
  38. */
  39. private $filterBuilder;
  40. /**
  41. * @var SearchCriteriaBuilder
  42. */
  43. private $searchCriteriaBuilder;
  44. /**
  45. * @var Filter[]
  46. */
  47. private $filters = [];
  48. /**
  49. * @var int
  50. */
  51. private $totalCount;
  52. /**
  53. * @var AggregationInterface
  54. */
  55. private $aggregations;
  56. /**
  57. * @var SearchCriteriaInterface
  58. */
  59. private $searchCriteria;
  60. /**
  61. * Collection constructor.
  62. * @param EntityFactoryInterface $entityFactory
  63. * @param ManagerInterface $messageManager
  64. * @param FilterBuilder $filterBuilder
  65. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  66. */
  67. public function __construct(
  68. EntityFactoryInterface $entityFactory,
  69. ManagerInterface $messageManager,
  70. FilterBuilder $filterBuilder,
  71. SearchCriteriaBuilder $searchCriteriaBuilder
  72. ) {
  73. $this->messageManager = $messageManager;
  74. $this->filterBuilder = $filterBuilder;
  75. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  76. parent::__construct($entityFactory);
  77. }
  78. /**
  79. * @param string|array $field
  80. * @param string|int|array $condition
  81. * @throws \Magento\Framework\Exception\LocalizedException if some error in the input could be detected.
  82. * @return $this
  83. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  84. */
  85. public function addFieldToFilter($field, $condition)
  86. {
  87. foreach ($condition as $type => $value) {
  88. $this->filters[]= $this->filterBuilder
  89. ->setField($field)
  90. ->setValue($value)
  91. ->setConditionType($type)
  92. ->create();
  93. }
  94. return $this;
  95. }
  96. /**
  97. * Perform API call
  98. *
  99. * @param SearchCriteriaInterface $criteria
  100. * @return \Magento\Framework\DataObject[]
  101. */
  102. abstract public function fetchData(SearchCriteriaInterface $criteria);
  103. /**
  104. * Sort documents
  105. * @return void
  106. */
  107. private function sortItems()
  108. {
  109. foreach ($this->_orders as $field => $direction) {
  110. if ($direction === self::SORT_ORDER_ASC) {
  111. uasort($this->_items, function (Document $itemA, Document $itemB) use ($field, $direction) {
  112. return $itemA->getDataByKey($field) > $itemB->getDataByKey($field) ? 1 : -1;
  113. });
  114. } else {
  115. uasort($this->_items, function (Document $itemA, Document $itemB) use ($field, $direction) {
  116. return $itemA->getDataByKey($field) < $itemB->getDataByKey($field) ? 1 : -1;
  117. });
  118. }
  119. }
  120. }
  121. /**
  122. * Paginate documents
  123. * @return void
  124. */
  125. private function sliceItems()
  126. {
  127. if ($this->_pageSize !== false) {
  128. $offset = $this->_pageSize * ($this->_curPage - 1);
  129. $limit = $this->_pageSize;
  130. $this->_items = array_slice($this->_items, $offset, $limit);
  131. }
  132. }
  133. /**
  134. * Load data from repository/api and convert to Document class
  135. *
  136. * @see \Magento\Framework\Data\Collection\AbstractDb::loadWithFilter()
  137. * @see \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider::searchResultToOutput()
  138. * @see \Magento\Framework\Api\Search\SearchResultInterface::getItems()
  139. *
  140. * @param bool $printQuery
  141. * @param bool $logQuery
  142. * @return $this
  143. */
  144. public function loadData($printQuery = false, $logQuery = false)
  145. {
  146. if ($this->isLoaded()) {
  147. return $this;
  148. }
  149. try {
  150. // load list from webservice
  151. array_walk($this->filters, function (Filter $filter) {
  152. $this->searchCriteriaBuilder->addFilters([$filter]);
  153. });
  154. $data = $this->fetchData($this->searchCriteriaBuilder->create());
  155. // shift response items to document class
  156. foreach ($data as $apiItem) {
  157. $item = $this->getNewEmptyItem();
  158. $item->addData($apiItem->getData());
  159. $this->addItem($item);
  160. }
  161. $this->_totalRecords = count($this->_items);
  162. $this->sortItems();
  163. $this->sliceItems();
  164. } catch (\Exception $e) {
  165. $this->messageManager->addExceptionMessage($e, 'An error occurred while requesting API listing.');
  166. }
  167. $this->_setIsLoaded();
  168. return $this;
  169. }
  170. /**
  171. * Set items list.
  172. *
  173. * @param DocumentInterface[] $items
  174. * @return $this
  175. * @throws \Exception
  176. */
  177. public function setItems(array $items = null)
  178. {
  179. if ($items) {
  180. foreach ($items as $item) {
  181. $this->addItem($item);
  182. }
  183. unset($this->totalCount);
  184. }
  185. return $this;
  186. }
  187. /**
  188. * @return AggregationInterface
  189. */
  190. public function getAggregations()
  191. {
  192. return $this->aggregations;
  193. }
  194. /**
  195. * @param AggregationInterface $aggregations
  196. * @return $this
  197. */
  198. public function setAggregations($aggregations)
  199. {
  200. $this->aggregations = $aggregations;
  201. return $this;
  202. }
  203. /**
  204. * Get search criteria.
  205. *
  206. * @return SearchCriteriaInterface|null
  207. */
  208. public function getSearchCriteria()
  209. {
  210. return $this->searchCriteria;
  211. }
  212. /**
  213. * Set search criteria.
  214. *
  215. * @param SearchCriteriaInterface $searchCriteria
  216. * @return $this
  217. */
  218. public function setSearchCriteria(SearchCriteriaInterface $searchCriteria)
  219. {
  220. $this->searchCriteria = $searchCriteria;
  221. return $this;
  222. }
  223. /**
  224. * Get total count.
  225. *
  226. * @return int
  227. */
  228. public function getTotalCount()
  229. {
  230. if (!$this->totalCount) {
  231. $this->totalCount = $this->getSize();
  232. }
  233. return $this->totalCount;
  234. }
  235. /**
  236. * Set total count.
  237. *
  238. * @param int $totalCount
  239. * @return $this
  240. */
  241. public function setTotalCount($totalCount)
  242. {
  243. $this->totalCount = $totalCount;
  244. return $this;
  245. }
  246. }