Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CheckoutAgreements getList refactoring #13221

Merged
merged 6 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CheckoutAgreements\Api;

/**
* Interface for retrieving list of checkout agreements.
*
* Extended variation of CheckoutAgreementsRepositoryInterface::getList with possibility to get results according
* search filters without predefined limitations.
*
* @api
*/
interface CheckoutAgreementsListInterface
{
/**
* List of checkout agreements.
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface[]
*/
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) : array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function get($id, $storeId = null);
* Lists active checkout agreements.
*
* @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface[]
* @deprecated
* @see \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface::getList
*/
public function getList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
namespace Magento\CheckoutAgreements\Model;

use Magento\Checkout\Model\ConfigProviderInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Store\Model\ScopeInterface;
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;

/**
* Configuration provider for GiftMessage rendering on "Shipping Method" step of checkout.
Expand All @@ -28,20 +30,40 @@ class AgreementsConfigProvider implements ConfigProviderInterface
*/
protected $escaper;

/**
* @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface
*/
private $checkoutAgreementsList;

/**
* @var ActiveStoreAgreementsFilter
*/
private $activeStoreAgreementsFilter;

/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface|null $checkoutAgreementsList
* @param ActiveStoreAgreementsFilter|null $activeStoreAgreementsFilter
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
\Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository,
\Magento\Framework\Escaper $escaper
\Magento\Framework\Escaper $escaper,
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList = null,
ActiveStoreAgreementsFilter $activeStoreAgreementsFilter = null
) {
$this->scopeConfiguration = $scopeConfiguration;
$this->checkoutAgreementsRepository = $checkoutAgreementsRepository;
$this->escaper = $escaper;
$this->checkoutAgreementsList = $checkoutAgreementsList ?: ObjectManager::getInstance()->get(
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface::class
);
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter ?: ObjectManager::getInstance()->get(
ActiveStoreAgreementsFilter::class
);
}

/**
Expand All @@ -67,7 +89,9 @@ protected function getAgreementsConfig()
ScopeInterface::SCOPE_STORE
);

$agreementsList = $this->checkoutAgreementsRepository->getList();
$agreementsList = $this->checkoutAgreementsList->getList(
$this->activeStoreAgreementsFilter->buildSearchCriteria()
);
$agreementConfiguration['isEnabled'] = (bool)($isAgreementsEnabled && count($agreementsList) > 0);

foreach ($agreementsList as $agreement) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CheckoutAgreements\Model\Api\SearchCriteria;

/**
* Build search criteria for agreements list.
*/
class ActiveStoreAgreementsFilter
{
/**
* @var \Magento\Framework\Api\FilterBuilder
*/
private $filterBuilder;

/**
* @var \Magento\Framework\Api\SearchCriteriaBuilder
*/
private $searchCriteriaBuilder;

/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $storeManager;

/**
* @param \Magento\Framework\Api\FilterBuilder $filterBuilder
* @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Framework\Api\FilterBuilder $filterBuilder,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
$this->filterBuilder = $filterBuilder;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->storeManager = $storeManager;
}

/**
* Build search criteria with store and is_active filters.
*
* @return \Magento\Framework\Api\SearchCriteriaInterface
*/
public function buildSearchCriteria() : \Magento\Framework\Api\SearchCriteriaInterface
{
$storeFilter = $this->filterBuilder
->setField('store_id')
->setConditionType('eq')
->setValue($this->storeManager->getStore()->getId())
->create();
$isActiveFilter = $this->filterBuilder->setField('is_active')
->setConditionType('eq')
->setValue(1)
->create();
$this->searchCriteriaBuilder->addFilters([$storeFilter]);
$this->searchCriteriaBuilder->addFilters([$isActiveFilter]);
return $this->searchCriteriaBuilder->create();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\CheckoutAgreements\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor;

use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Api\Filter;
use Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Collection;

/**
* Custom store filter to collection
*/
class StoreFilter implements CustomFilterInterface
{
/**
* Apply custom store filter to collection
*
* @param Filter $filter
* @param AbstractDb $collection
* @return bool
*/
public function apply(Filter $filter, AbstractDb $collection)
{
/** @var Collection $collection */
$collection->addStoreFilter($filter->getValue());
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\CheckoutAgreements\Model\AgreementsProvider;
use Magento\Store\Model\ScopeInterface;
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;

/**
* Class GuestValidation
Expand All @@ -23,28 +24,36 @@ class GuestValidation
private $scopeConfiguration;

/**
* @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface
* @var \Magento\Checkout\Api\AgreementsValidatorInterface
*/
private $checkoutAgreementsRepository;
private $agreementsValidator;

/**
* @var \Magento\Checkout\Api\AgreementsValidatorInterface
* @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface
*/
private $agreementsValidator;
private $checkoutAgreementsList;

/**
* @var \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter
*/
private $activeStoreAgreementsFilter;

/**
* @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
* @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
*/
public function __construct(
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
\Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
) {
$this->agreementsValidator = $agreementsValidator;
$this->scopeConfiguration = $scopeConfiguration;
$this->checkoutAgreementsRepository = $checkoutAgreementsRepository;
$this->checkoutAgreementsList = $checkoutAgreementsList;
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
}

/**
Expand All @@ -53,6 +62,7 @@ public function __construct(
* @param string $email
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand All @@ -74,6 +84,7 @@ public function beforeSavePaymentInformationAndPlaceOrder(
* @param string $email
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand Down Expand Up @@ -117,7 +128,9 @@ private function isAgreementEnabled()
AgreementsProvider::PATH_ENABLED,
ScopeInterface::SCOPE_STORE
);
$agreementsList = $isAgreementsEnabled ? $this->checkoutAgreementsRepository->getList() : [];
$agreementsList = $isAgreementsEnabled
? $this->checkoutAgreementsList->getList($this->activeStoreAgreementsFilter->buildSearchCriteria())
: [];
return (bool)($isAgreementsEnabled && count($agreementsList) > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\CheckoutAgreements\Model\AgreementsProvider;
use Magento\Store\Model\ScopeInterface;
use Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter;

/**
* Class Validation
Expand All @@ -19,35 +20,44 @@ class Validation
protected $scopeConfiguration;

/**
* @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface
* @var \Magento\Checkout\Api\AgreementsValidatorInterface
*/
protected $checkoutAgreementsRepository;
protected $agreementsValidator;

/**
* @var \Magento\Checkout\Api\AgreementsValidatorInterface
* @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface
*/
protected $agreementsValidator;
private $checkoutAgreementsList;

/**
* @var \Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter
*/
private $activeStoreAgreementsFilter;

/**
* @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList
* @param ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
*/
public function __construct(
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
\Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList,
\Magento\CheckoutAgreements\Model\Api\SearchCriteria\ActiveStoreAgreementsFilter $activeStoreAgreementsFilter
) {
$this->agreementsValidator = $agreementsValidator;
$this->scopeConfiguration = $scopeConfiguration;
$this->checkoutAgreementsRepository = $checkoutAgreementsRepository;
$this->checkoutAgreementsList = $checkoutAgreementsList;
$this->activeStoreAgreementsFilter = $activeStoreAgreementsFilter;
}

/**
* @param \Magento\Checkout\Api\PaymentInformationManagementInterface $subject
* @param int $cartId
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand All @@ -67,6 +77,7 @@ public function beforeSavePaymentInformationAndPlaceOrder(
* @param int $cartId
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
Expand Down Expand Up @@ -109,7 +120,9 @@ protected function isAgreementEnabled()
AgreementsProvider::PATH_ENABLED,
ScopeInterface::SCOPE_STORE
);
$agreementsList = $isAgreementsEnabled ? $this->checkoutAgreementsRepository->getList() : [];
$agreementsList = $isAgreementsEnabled
? $this->checkoutAgreementsList->getList($this->activeStoreAgreementsFilter->buildSearchCriteria())
: [];
return (bool)($isAgreementsEnabled && count($agreementsList) > 0);
}
}
Loading