|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jcowie\FeatureToggle\Model\Resource; |
|
4
|
|
|
|
|
5
|
|
|
use Jcowie\FeatureToggle\Api\Data\FeaturetoggleInterface; |
|
6
|
|
|
use Jcowie\FeatureToggle\Api\FeaturetoggleRepositoryInterface; |
|
7
|
|
|
use Jcowie\FeatureToggle\Model\Resource\Featuretoggle\Collection as FeaturetoggleCollection; |
|
8
|
|
|
use Jcowie\FeatureToggle\Model\Resource\Featuretoggle\CollectionFactory; |
|
9
|
|
|
use Jcowie\FeatureToggle\Model\FeaturetoggleRegistry; |
|
10
|
|
|
|
|
11
|
|
|
use Magento\Framework\Api\SearchCriteriaInterface; |
|
12
|
|
|
use Magento\Framework\Api\SearchResultsInterface; |
|
13
|
|
|
use Magento\Framework\Api\SearchResultsInterfaceFactory; |
|
14
|
|
|
use Magento\Framework\Exception\NoSuchEntityException; |
|
15
|
|
|
use Magento\Store\Model\StoreManagerInterface; |
|
16
|
|
|
|
|
17
|
|
|
class FeaturetoggleRepository implements FeaturetoggleRepositoryInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var CollectionFactory |
|
21
|
|
|
*/ |
|
22
|
|
|
private $collectionFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var SearchResultsInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $searchResult; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var SearchResultsInterfaceFactory |
|
31
|
|
|
*/ |
|
32
|
|
|
private $searchResultsFactory; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var FeaturetoggleRegistry |
|
36
|
|
|
*/ |
|
37
|
|
|
private $featuretoggleRegistry; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var StoreManagerInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
private $storeManager; |
|
43
|
|
|
/** |
|
44
|
|
|
* @param CollectionFactory $collectionFactory |
|
45
|
|
|
* @param SearchResultsInterface $searchResult |
|
46
|
|
|
* @param SearchResultsInterfaceFactory $searchResultsFactory |
|
47
|
|
|
* @param FeaturetoggleRegistry $featuretoggleRegistry |
|
48
|
|
|
* @param StoreManagerInterface $storeManager |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct( |
|
51
|
|
|
CollectionFactory $collectionFactory, |
|
52
|
|
|
SearchResultsInterface $searchResult, |
|
53
|
|
|
SearchResultsInterfaceFactory $searchResultsFactory, |
|
54
|
|
|
FeaturetoggleRegistry $featuretoggleRegistry, |
|
55
|
|
|
StoreManagerInterface $storeManager |
|
56
|
|
|
) { |
|
57
|
|
|
$this->collectionFactory = $collectionFactory; |
|
58
|
|
|
$this->searchResult = $searchResult; |
|
59
|
|
|
$this->searchResultsFactory = $searchResultsFactory; |
|
60
|
|
|
$this->featuretoggleRegistry = $featuretoggleRegistry; |
|
61
|
|
|
$this->storeManager = $storeManager; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getList(SearchCriteriaInterface $searchCriteria) |
|
65
|
|
|
{ |
|
66
|
|
|
/** @var FeatureToggle $collection */ |
|
67
|
|
|
$collection = $this->collectionFactory->create(); |
|
68
|
|
|
|
|
69
|
|
|
$this->setFilters($searchCriteria, $collection); |
|
70
|
|
|
$this->setSortOrder($searchCriteria, $collection); |
|
71
|
|
|
$this->setPageSize($searchCriteria, $collection); |
|
72
|
|
|
$this->setCurrentPage($searchCriteria, $collection); |
|
73
|
|
|
$searchResult = $this->searchResultsFactory->create(); |
|
74
|
|
|
$searchResult->setItems($collection->getItems()); |
|
75
|
|
|
$searchResult->setSearchCriteria($searchCriteria); |
|
76
|
|
|
$searchResult->setTotalCount($collection->getSize()); |
|
77
|
|
|
return $searchResult; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getById($featuretoggleId) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->featuretoggleRegistry->retrieve($featuretoggleId); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param SearchCriteriaInterface $searchCriteria |
|
87
|
|
|
* @param FeaturetoggleCollection $collection |
|
88
|
|
|
*/ |
|
89
|
|
|
private function setFilters(SearchCriteriaInterface $searchCriteria, $collection) |
|
90
|
|
|
{ |
|
91
|
|
|
foreach ($searchCriteria->getFilterGroups() as $filterGroup) { |
|
92
|
|
|
foreach ($filterGroup->getFilters() as $filter) { |
|
93
|
|
|
$collection->addFieldToFilter( |
|
94
|
|
|
$filter->getField(), |
|
95
|
|
|
[$filter->getConditionType() => $filter->getValue()] |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param SearchCriteriaInterface $searchCriteria |
|
103
|
|
|
* @param FeaturetoggleCollection $collection |
|
104
|
|
|
*/ |
|
105
|
|
|
private function setSortOrder(SearchCriteriaInterface $searchCriteria, $collection) |
|
106
|
|
|
{ |
|
107
|
|
|
if ($searchCriteria->getSortOrders()) { |
|
108
|
|
|
foreach ($searchCriteria->getSortOrders() as $sortOrder) { |
|
109
|
|
|
$collection->addOrder( |
|
110
|
|
|
$sortOrder->getField(), |
|
111
|
|
|
$sortOrder->getDirection() === SearchCriteriaInterface::SORT_ASC ? 'ASC' : 'DESC' |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
/** |
|
117
|
|
|
* @param SearchCriteriaInterface $searchCriteria |
|
118
|
|
|
* @param FeaturetoggleCollection $collection |
|
119
|
|
|
*/ |
|
120
|
|
|
private function setPageSize(SearchCriteriaInterface $searchCriteria, $collection) |
|
121
|
|
|
{ |
|
122
|
|
|
if ($searchCriteria->getPageSize()) { |
|
123
|
|
|
$collection->setPageSize($searchCriteria->getPageSize()); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
/** |
|
127
|
|
|
* @param SearchCriteriaInterface $searchCriteria |
|
128
|
|
|
* @param FeaturetoggleCollection $collection |
|
129
|
|
|
*/ |
|
130
|
|
|
private function setCurrentPage(SearchCriteriaInterface $searchCriteria, $collection) |
|
131
|
|
|
{ |
|
132
|
|
|
if ($searchCriteria->getCurrentPage()) { |
|
133
|
|
|
$collection->setCurPage($searchCriteria->getCurrentPage()); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|