TransactionsCollection.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Braintree\Model\Report;
  7. use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
  8. use Magento\Braintree\Model\Report\Row\TransactionMap;
  9. use Magento\Framework\Api\Search\SearchResultInterface;
  10. use Magento\Framework\Api\SearchCriteriaInterface;
  11. use Magento\Framework\Data\Collection;
  12. use Magento\Framework\Data\Collection\EntityFactoryInterface;
  13. /**
  14. * Class TransactionsCollection
  15. */
  16. class TransactionsCollection extends Collection implements SearchResultInterface
  17. {
  18. /**
  19. * Transaction maximum count
  20. */
  21. const TRANSACTION_MAXIMUM_COUNT = 100;
  22. /**
  23. * Item object class name
  24. *
  25. * @var string
  26. */
  27. protected $_itemObjectClass = TransactionMap::class;
  28. /**
  29. * @var array
  30. */
  31. private $filtersList = [];
  32. /**
  33. * @var FilterMapper
  34. */
  35. private $filterMapper;
  36. /**
  37. * @var BraintreeAdapterFactory
  38. */
  39. private $braintreeAdapterFactory;
  40. /**
  41. * @var \Braintree\ResourceCollection | null
  42. */
  43. private $collection;
  44. /**
  45. * @param EntityFactoryInterface $entityFactory
  46. * @param BraintreeAdapterFactory $braintreeAdapterFactory
  47. * @param FilterMapper $filterMapper
  48. */
  49. public function __construct(
  50. EntityFactoryInterface $entityFactory,
  51. BraintreeAdapterFactory $braintreeAdapterFactory,
  52. FilterMapper $filterMapper
  53. ) {
  54. parent::__construct($entityFactory);
  55. $this->filterMapper = $filterMapper;
  56. $this->braintreeAdapterFactory = $braintreeAdapterFactory;
  57. }
  58. /**
  59. * @return \Magento\Framework\Api\Search\DocumentInterface[]
  60. */
  61. public function getItems()
  62. {
  63. if (!$this->fetchIdsCollection()) {
  64. return [];
  65. }
  66. $result = [];
  67. $counter = 0;
  68. $pageSize = $this->getPageSize();
  69. $skipCounter = ($this->_curPage - 1) * $pageSize;
  70. // To optimize the processing of large searches, data is retrieved from the server lazily.
  71. foreach ($this->collection as $item) {
  72. if ($skipCounter > 0) {
  73. $skipCounter --;
  74. } else {
  75. $entity = $this->_entityFactory->create($this->_itemObjectClass, ['transaction' => $item]);
  76. if ($entity) {
  77. $result[] = $entity;
  78. $counter ++;
  79. if ($pageSize && $counter >= $pageSize) {
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. return $result;
  86. }
  87. /**
  88. * Fetch collection from Braintree
  89. * @return \Braintree\ResourceCollection|null
  90. */
  91. protected function fetchIdsCollection()
  92. {
  93. if (empty($this->filtersList)) {
  94. return null;
  95. }
  96. // Fetch all transaction IDs in order to filter
  97. if (empty($this->collection)) {
  98. $filters = $this->getFilters();
  99. $this->collection = $this->braintreeAdapterFactory->create()
  100. ->search($filters);
  101. }
  102. return $this->collection;
  103. }
  104. /**
  105. * Set items list.
  106. *
  107. * @param \Magento\Framework\Api\Search\DocumentInterface[] $items
  108. * @return $this
  109. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  110. */
  111. public function setItems(array $items = null)
  112. {
  113. return $this;
  114. }
  115. /**
  116. * @return \Magento\Framework\Api\Search\AggregationInterface
  117. */
  118. public function getAggregations()
  119. {
  120. return null;
  121. }
  122. /**
  123. * @param \Magento\Framework\Api\Search\AggregationInterface $aggregations
  124. * @return $this
  125. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  126. */
  127. public function setAggregations($aggregations)
  128. {
  129. return $this;
  130. }
  131. /**
  132. * Get search criteria.
  133. *
  134. * @return \Magento\Framework\Api\Search\SearchCriteriaInterface
  135. */
  136. public function getSearchCriteria()
  137. {
  138. return null;
  139. }
  140. /**
  141. * Set search criteria.
  142. *
  143. * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
  144. * @return $this
  145. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  146. */
  147. public function setSearchCriteria(SearchCriteriaInterface $searchCriteria)
  148. {
  149. return $this;
  150. }
  151. /**
  152. * Get total count.
  153. *
  154. * @return int
  155. */
  156. public function getTotalCount()
  157. {
  158. $collection = $this->fetchIdsCollection();
  159. return null === $collection ? 0 : $collection->maximumCount();
  160. }
  161. /**
  162. * Retrieve collection page size
  163. *
  164. * @return int
  165. */
  166. public function getPageSize()
  167. {
  168. $pageSize = parent::getPageSize();
  169. return $pageSize === null ? static::TRANSACTION_MAXIMUM_COUNT : $pageSize;
  170. }
  171. /**
  172. * Set total count.
  173. *
  174. * @param int $totalCount
  175. * @return $this
  176. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  177. */
  178. public function setTotalCount($totalCount)
  179. {
  180. return $this;
  181. }
  182. /**
  183. * @inheritdoc
  184. */
  185. public function addFieldToFilter($field, $condition)
  186. {
  187. if (is_array($field)) {
  188. return $this;
  189. }
  190. if (!is_array($condition)) {
  191. $condition = ['eq' => $condition];
  192. }
  193. $this->addFilterToList($this->filterMapper->getFilter($field, $condition));
  194. return $this;
  195. }
  196. /**
  197. * Add filter to list
  198. *
  199. * @param object $filter
  200. * @return void
  201. */
  202. private function addFilterToList($filter)
  203. {
  204. if (null !== $filter) {
  205. $this->filtersList[] = $filter;
  206. }
  207. }
  208. /**
  209. * @return array
  210. */
  211. private function getFilters()
  212. {
  213. return $this->filtersList;
  214. }
  215. }