| Current Path : /home/rtorresani/www/vendor/paypal/module-braintree-core/Model/Report/ |
| Current File : //home/rtorresani/www/vendor/paypal/module-braintree-core/Model/Report/TransactionsCollection.php |
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace PayPal\Braintree\Model\Report;
use Braintree\ResourceCollection;
use PayPal\Braintree\Model\Adapter\BraintreeAdapter;
use PayPal\Braintree\Model\Report\Row\TransactionMap;
use Magento\Framework\Api\Search\AggregationInterface;
use Magento\Framework\Api\Search\DocumentInterface;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Data\Collection;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
class TransactionsCollection extends Collection implements SearchResultInterface
{
const TRANSACTION_MAXIMUM_COUNT = 100;
/**
* Item object class name
*
* @var string
*/
protected $_itemObjectClass = TransactionMap::class;
/**
* @var array
*/
private $filtersList = [];
/**
* @var FilterMapper
*/
private $filterMapper;
/**
* @var BraintreeAdapter
*/
private $braintreeAdapter;
/**
* @var ResourceCollection | null
*/
private $collection;
/**
* @param EntityFactoryInterface $entityFactory
* @param BraintreeAdapter $braintreeAdapter
* @param FilterMapper $filterMapper
*/
public function __construct(
EntityFactoryInterface $entityFactory,
BraintreeAdapter $braintreeAdapter,
FilterMapper $filterMapper
) {
parent::__construct($entityFactory);
$this->filterMapper = $filterMapper;
$this->braintreeAdapter = $braintreeAdapter;
}
/**
* @return array
*/
public function getItems(): array
{
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 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->braintreeAdapter->search($filters);
}
return $this->collection;
}
/**
* Set items list.
*
* @param DocumentInterface[] $items
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setItems(array $items = null)
{
return $this;
}
/**
* @return AggregationInterface
*/
public function getAggregations()
{
return null;
}
/**
* @param 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 SearchCriteriaInterface $searchCriteria
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setSearchCriteria(SearchCriteriaInterface $searchCriteria)
{
return $this;
}
/**
* Get total count.
*
* @return int
*/
public function getTotalCount(): int
{
$collection = $this->fetchIdsCollection();
return null === $collection ? 0 : $collection->maximumCount();
}
/**
* Retrieve collection page size
*
* @return int
*/
public function getPageSize(): int
{
$pageSize = parent::getPageSize();
return $pageSize ?? static::TRANSACTION_MAXIMUM_COUNT;
}
/**
* 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(): array
{
return $this->filtersList;
}
}