Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 44e7090

Browse files
author
Stanislav Idolov
committed
CheckoutAgreements getList refactoring
-- review fixes
1 parent 3c446dc commit 44e7090

File tree

17 files changed

+490
-98
lines changed

17 files changed

+490
-98
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CheckoutAgreements\Api;
8+
9+
/**
10+
* Interface for retrieving list of checkout agreements.
11+
*
12+
* Extended variation of CheckoutAgreementsRepositoryInterface::getList with possibility to get results according
13+
* search filters without predefined limitations.
14+
*
15+
* @api
16+
*/
17+
interface CheckoutAgreementsListInterface
18+
{
19+
/**
20+
* List of checkout agreements.
21+
*
22+
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
23+
* @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface[]
24+
*/
25+
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) : array;
26+
}

app/code/Magento/CheckoutAgreements/Api/CheckoutAgreementsListingInterface.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

app/code/Magento/CheckoutAgreements/Api/CheckoutAgreementsRepositoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function get($id, $storeId = null);
2626
*
2727
* @return \Magento\CheckoutAgreements\Api\Data\AgreementInterface[]
2828
* @deprecated
29-
* @see \Magento\CheckoutAgreements\Api\CheckoutAgreementsListingInterface::getListing
29+
* @see \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface::getList
3030
*/
3131
public function getList();
3232

app/code/Magento/CheckoutAgreements/Model/AgreementsConfigProvider.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CheckoutAgreements\Model;
77

88
use Magento\Checkout\Model\ConfigProviderInterface;
9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Store\Model\ScopeInterface;
1011

1112
/**
@@ -28,20 +29,60 @@ class AgreementsConfigProvider implements ConfigProviderInterface
2829
*/
2930
protected $escaper;
3031

32+
/**
33+
* @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface
34+
*/
35+
private $checkoutAgreementsList;
36+
37+
/**
38+
* @var \Magento\Framework\Api\FilterBuilder
39+
*/
40+
private $filterBuilder;
41+
42+
/**
43+
* @var \Magento\Framework\Api\SearchCriteriaBuilder
44+
*/
45+
private $searchCriteriaBuilder;
46+
47+
/**
48+
* @var \Magento\Store\Model\StoreManagerInterface
49+
*/
50+
private $storeManager;
51+
3152
/**
3253
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
3354
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository
3455
* @param \Magento\Framework\Escaper $escaper
56+
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface|null $checkoutAgreementsList
57+
* @param \Magento\Framework\Api\FilterBuilder|null $filterBuilder
58+
* @param \Magento\Framework\Api\SearchCriteriaBuilder|null $searchCriteriaBuilder
59+
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
3560
* @codeCoverageIgnore
3661
*/
3762
public function __construct(
3863
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
3964
\Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository,
40-
\Magento\Framework\Escaper $escaper
65+
\Magento\Framework\Escaper $escaper,
66+
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList = null,
67+
\Magento\Framework\Api\FilterBuilder $filterBuilder = null,
68+
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder = null,
69+
\Magento\Store\Model\StoreManagerInterface $storeManager = null
4170
) {
4271
$this->scopeConfiguration = $scopeConfiguration;
4372
$this->checkoutAgreementsRepository = $checkoutAgreementsRepository;
4473
$this->escaper = $escaper;
74+
$this->checkoutAgreementsList = $checkoutAgreementsList ?: ObjectManager::getInstance()->get(
75+
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface::class
76+
);
77+
$this->filterBuilder = $filterBuilder ?: ObjectManager::getInstance()->get(
78+
\Magento\Framework\Api\FilterBuilder::class
79+
);
80+
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()->get(
81+
\Magento\Framework\Api\SearchCriteriaBuilder::class
82+
);
83+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
84+
\Magento\Store\Model\StoreManagerInterface::class
85+
);
4586
}
4687

4788
/**
@@ -67,7 +108,20 @@ protected function getAgreementsConfig()
67108
ScopeInterface::SCOPE_STORE
68109
);
69110

70-
$agreementsList = $this->checkoutAgreementsRepository->getList();
111+
$storeFilter = $this->filterBuilder
112+
->setField('store_id')
113+
->setConditionType('eq')
114+
->setValue($this->storeManager->getStore()->getId())
115+
->create();
116+
$isActiveFilter = $this->filterBuilder
117+
->setField('is_active')
118+
->setConditionType('eq')
119+
->setValue(1)
120+
->create();
121+
$this->searchCriteriaBuilder->addFilters([$storeFilter]);
122+
$this->searchCriteriaBuilder->addFilters([$isActiveFilter]);
123+
124+
$agreementsList = $this->checkoutAgreementsList->getList($this->searchCriteriaBuilder->create());
71125
$agreementConfiguration['isEnabled'] = (bool)($isAgreementsEnabled && count($agreementsList) > 0);
72126

73127
foreach ($agreementsList as $agreement) {

app/code/Magento/CheckoutAgreements/Model/Api/SearchCriteria/CollectionProcessor/FilterProcessor/StoreFilter.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@
66

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

9-
class StoreFilter implements
10-
\Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface
9+
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
10+
use Magento\Framework\Data\Collection\AbstractDb;
11+
use Magento\Framework\Api\Filter;
12+
use Magento\CheckoutAgreements\Model\ResourceModel\Agreement\Collection;
13+
14+
/**
15+
* Custom store filter to collection
16+
*/
17+
class StoreFilter implements CustomFilterInterface
1118
{
1219
/**
1320
* Apply custom store filter to collection
1421
*
15-
* @param \Magento\Framework\Api\Filter $filter
16-
* @param \Magento\Framework\Data\Collection\AbstractDb $collection
22+
* @param Filter $filter
23+
* @param AbstractDb $collection
1724
* @return bool
1825
*/
19-
public function apply(
20-
\Magento\Framework\Api\Filter $filter,
21-
\Magento\Framework\Data\Collection\AbstractDb $collection
22-
) {
23-
/** @var \Magento\Cms\Model\ResourceModel\Page\Collection $collection */
24-
$collection->addStoreFilter($filter->getValue(), false);
26+
public function apply(Filter $filter, AbstractDb $collection)
27+
{
28+
/** @var Collection $collection */
29+
$collection->addStoreFilter($filter->getValue());
2530
return true;
2631
}
2732
}
28-

app/code/Magento/CheckoutAgreements/Model/Checkout/Plugin/GuestValidation.php

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Magento\CheckoutAgreements\Model\Checkout\Plugin;
77

88
use Magento\CheckoutAgreements\Model\AgreementsProvider;
9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Store\Model\ScopeInterface;
1011

1112
/**
@@ -32,19 +33,59 @@ class GuestValidation
3233
*/
3334
private $agreementsValidator;
3435

36+
/**
37+
* @var \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface
38+
*/
39+
private $checkoutAgreementsList;
40+
41+
/**
42+
* @var \Magento\Framework\Api\FilterBuilder
43+
*/
44+
private $filterBuilder;
45+
46+
/**
47+
* @var \Magento\Framework\Api\SearchCriteriaBuilder
48+
*/
49+
private $searchCriteriaBuilder;
50+
51+
/**
52+
* @var \Magento\Store\Model\StoreManagerInterface
53+
*/
54+
private $storeManager;
55+
3556
/**
3657
* @param \Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator
3758
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration
3859
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository
60+
* @param \Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface|null $checkoutAgreementsList
61+
* @param \Magento\Framework\Api\FilterBuilder|null $filterBuilder
62+
* @param \Magento\Framework\Api\SearchCriteriaBuilder|null $searchCriteriaBuilder
63+
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
3964
*/
4065
public function __construct(
4166
\Magento\Checkout\Api\AgreementsValidatorInterface $agreementsValidator,
4267
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfiguration,
43-
\Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository
68+
\Magento\CheckoutAgreements\Api\CheckoutAgreementsRepositoryInterface $checkoutAgreementsRepository,
69+
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface $checkoutAgreementsList = null,
70+
\Magento\Framework\Api\FilterBuilder $filterBuilder = null,
71+
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder = null,
72+
\Magento\Store\Model\StoreManagerInterface $storeManager = null
4473
) {
4574
$this->agreementsValidator = $agreementsValidator;
4675
$this->scopeConfiguration = $scopeConfiguration;
4776
$this->checkoutAgreementsRepository = $checkoutAgreementsRepository;
77+
$this->checkoutAgreementsList = $checkoutAgreementsList ?: ObjectManager::getInstance()->get(
78+
\Magento\CheckoutAgreements\Api\CheckoutAgreementsListInterface::class
79+
);
80+
$this->filterBuilder = $filterBuilder ?: ObjectManager::getInstance()->get(
81+
\Magento\Framework\Api\FilterBuilder::class
82+
);
83+
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()->get(
84+
\Magento\Framework\Api\SearchCriteriaBuilder::class
85+
);
86+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
87+
\Magento\Store\Model\StoreManagerInterface::class
88+
);
4889
}
4990

5091
/**
@@ -53,7 +94,7 @@ public function __construct(
5394
* @param string $email
5495
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
5596
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
56-
* @return void
97+
* @throws \Magento\Framework\Exception\CouldNotSaveException
5798
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5899
*/
59100
public function beforeSavePaymentInformationAndPlaceOrder(
@@ -74,7 +115,7 @@ public function beforeSavePaymentInformationAndPlaceOrder(
74115
* @param string $email
75116
* @param \Magento\Quote\Api\Data\PaymentInterface $paymentMethod
76117
* @param \Magento\Quote\Api\Data\AddressInterface|null $billingAddress
77-
* @return void
118+
* @throws \Magento\Framework\Exception\CouldNotSaveException
78119
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
79120
*/
80121
public function beforeSavePaymentInformation(
@@ -117,7 +158,21 @@ private function isAgreementEnabled()
117158
AgreementsProvider::PATH_ENABLED,
118159
ScopeInterface::SCOPE_STORE
119160
);
120-
$agreementsList = $isAgreementsEnabled ? $this->checkoutAgreementsRepository->getList() : [];
161+
$storeFilter = $this->filterBuilder
162+
->setField('store_id')
163+
->setConditionType('eq')
164+
->setValue($this->storeManager->getStore()->getId())
165+
->create();
166+
$isActiveFilter = $this->filterBuilder->setField('is_active')
167+
->setConditionType('eq')
168+
->setValue(1)
169+
->create();
170+
$this->searchCriteriaBuilder->addFilters([$storeFilter]);
171+
$this->searchCriteriaBuilder->addFilters([$isActiveFilter]);
172+
173+
$agreementsList = $isAgreementsEnabled
174+
? $this->checkoutAgreementsList->getList($this->searchCriteriaBuilder->create())
175+
: [];
121176
return (bool)($isAgreementsEnabled && count($agreementsList) > 0);
122177
}
123178
}

0 commit comments

Comments
 (0)