123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Braintree\Model\Report;
- use Magento\Braintree\Model\Adapter\BraintreeAdapterFactory;
- use Magento\Braintree\Model\Report\Row\TransactionMap;
- use Magento\Framework\Api\Search\SearchResultInterface;
- use Magento\Framework\Api\SearchCriteriaInterface;
- use Magento\Framework\Data\Collection;
- use Magento\Framework\Data\Collection\EntityFactoryInterface;
- /**
- * Class TransactionsCollection
- */
- class TransactionsCollection extends Collection implements SearchResultInterface
- {
- /**
- * Transaction maximum count
- */
- const TRANSACTION_MAXIMUM_COUNT = 100;
- /**
- * Item object class name
- *
- * @var string
- */
- protected $_itemObjectClass = TransactionMap::class;
- /**
- * @var array
- */
- private $filtersList = [];
- /**
- * @var FilterMapper
- */
- private $filterMapper;
- /**
- * @var BraintreeAdapterFactory
- */
- private $braintreeAdapterFactory;
- /**
- * @var \Braintree\ResourceCollection | null
- */
- private $collection;
- /**
- * @param EntityFactoryInterface $entityFactory
- * @param BraintreeAdapterFactory $braintreeAdapterFactory
- * @param FilterMapper $filterMapper
- */
- public function __construct(
- EntityFactoryInterface $entityFactory,
- BraintreeAdapterFactory $braintreeAdapterFactory,
- FilterMapper $filterMapper
- ) {
- parent::__construct($entityFactory);
- $this->filterMapper = $filterMapper;
- $this->braintreeAdapterFactory = $braintreeAdapterFactory;
- }
- /**
- * @return \Magento\Framework\Api\Search\DocumentInterface[]
- */
- public function getItems()
- {
- if (!$this->fetchIdsCollection()) {
- return [];
- }
- $result = [];
- $counter = 0;
- $pageSize = $this->getPageSize();
- $skipCounter = ($this->_curPage - 1) * $pageSize;
- // To optimize the processing of large searches, data is retrieved from the server lazily.
- foreach ($this->collection as $item) {
- if ($skipCounter > 0) {
- $skipCounter --;
- } else {
- $entity = $this->_entityFactory->create($this->_itemObjectClass, ['transaction' => $item]);
- if ($entity) {
- $result[] = $entity;
- $counter ++;
- if ($pageSize && $counter >= $pageSize) {
- break;
- }
- }
- }
- }
- return $result;
- }
- /**
- * Fetch collection from Braintree
- * @return \Braintree\ResourceCollection|null
- */
- protected function fetchIdsCollection()
- {
- if (empty($this->filtersList)) {
- return null;
- }
- // Fetch all transaction IDs in order to filter
- if (empty($this->collection)) {
- $filters = $this->getFilters();
- $this->collection = $this->braintreeAdapterFactory->create()
- ->search($filters);
- }
- return $this->collection;
- }
- /**
- * Set items list.
- *
- * @param \Magento\Framework\Api\Search\DocumentInterface[] $items
- * @return $this
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function setItems(array $items = null)
- {
- return $this;
- }
- /**
- * @return \Magento\Framework\Api\Search\AggregationInterface
- */
- public function getAggregations()
- {
- return null;
- }
- /**
- * @param \Magento\Framework\Api\Search\AggregationInterface $aggregations
- * @return $this
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function setAggregations($aggregations)
- {
- return $this;
- }
- /**
- * Get search criteria.
- *
- * @return \Magento\Framework\Api\Search\SearchCriteriaInterface
- */
- public function getSearchCriteria()
- {
- return null;
- }
- /**
- * Set search criteria.
- *
- * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
- * @return $this
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function setSearchCriteria(SearchCriteriaInterface $searchCriteria)
- {
- return $this;
- }
- /**
- * Get total count.
- *
- * @return int
- */
- public function getTotalCount()
- {
- $collection = $this->fetchIdsCollection();
- return null === $collection ? 0 : $collection->maximumCount();
- }
- /**
- * Retrieve collection page size
- *
- * @return int
- */
- public function getPageSize()
- {
- $pageSize = parent::getPageSize();
- return $pageSize === null ? static::TRANSACTION_MAXIMUM_COUNT : $pageSize;
- }
- /**
- * Set total count.
- *
- * @param int $totalCount
- * @return $this
- * @SuppressWarnings(PHPMD.UnusedFormalParameter)
- */
- public function setTotalCount($totalCount)
- {
- return $this;
- }
- /**
- * @inheritdoc
- */
- public function addFieldToFilter($field, $condition)
- {
- if (is_array($field)) {
- return $this;
- }
- if (!is_array($condition)) {
- $condition = ['eq' => $condition];
- }
- $this->addFilterToList($this->filterMapper->getFilter($field, $condition));
- return $this;
- }
- /**
- * Add filter to list
- *
- * @param object $filter
- * @return void
- */
- private function addFilterToList($filter)
- {
- if (null !== $filter) {
- $this->filtersList[] = $filter;
- }
- }
- /**
- * @return array
- */
- private function getFilters()
- {
- return $this->filtersList;
- }
- }
|