1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Base Repository Class |
6
|
|
|
* @category Ticaje |
7
|
|
|
* @package Ticaje_Persistence |
8
|
|
|
* @author Hector Luis Barrientos <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Ticaje\Persistence\Repository\Base; |
12
|
|
|
|
13
|
|
|
use Magento\Framework\Api\SearchCriteriaInterface; |
14
|
|
|
use Magento\Framework\Api\SearchResultsInterface; |
15
|
|
|
use Magento\Framework\Exception\CouldNotSaveException; |
16
|
|
|
use Magento\Framework\Exception\NoSuchEntityException; |
17
|
|
|
use Magento\Framework\Api\SortOrder; |
18
|
|
|
use Magento\Framework\Api\SearchResultsInterfaceFactory; |
|
|
|
|
19
|
|
|
use Magento\Framework\ObjectManagerInterface; // For the sake of simplicity we're using Magento OM. |
20
|
|
|
use Exception; |
21
|
|
|
use Throwable; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class BaseRepository |
25
|
|
|
* @package Ticaje\Persistence\Repository\Base |
26
|
|
|
* The drawbacks of using this abstraction is having three dependencies, defined in this class attributes. |
27
|
|
|
* It is a must to pass such a dependencies through |
28
|
|
|
*/ |
29
|
|
|
class BaseRepository implements BaseRepositoryInterface |
30
|
|
|
{ |
31
|
|
|
use BaseTrait; |
32
|
|
|
|
33
|
|
|
private $object; // The current model class name |
34
|
|
|
|
35
|
|
|
private $collection; // The current model collection class name |
36
|
|
|
|
37
|
|
|
/** @var SearchResultsInterfaceFactory $searchResultsFactory */ |
38
|
|
|
private $searchResultsFactory; |
39
|
|
|
|
40
|
|
|
/** @var ObjectManagerInterface $objectManager */ |
41
|
|
|
private $objectManager; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* PersistenceRepository constructor. |
45
|
|
|
* @param string $object |
46
|
|
|
* @param string $collection |
47
|
|
|
* @param SearchResultsInterfaceFactory $searchResultsFactory |
48
|
|
|
* @param ObjectManagerInterface $objectManager |
49
|
|
|
*/ |
50
|
|
|
public function __construct( |
51
|
|
|
string $object, |
52
|
|
|
string $collection, |
53
|
|
|
SearchResultsInterfaceFactory $searchResultsFactory, |
54
|
|
|
ObjectManagerInterface $objectManager |
55
|
|
|
) { |
56
|
|
|
|
57
|
|
|
$this->object = $object; |
58
|
|
|
$this->collection = $collection; |
59
|
|
|
$this->searchResultsFactory = $searchResultsFactory; |
60
|
|
|
$this->objectManager = $objectManager; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @inheritDoc |
65
|
|
|
*/ |
66
|
|
|
public function save($object) |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
|
|
$object->save(); |
70
|
|
|
} catch (Exception $exception) { |
71
|
|
|
throw new CouldNotSaveException(__($exception->getMessage())); |
72
|
|
|
} |
73
|
|
|
return $object; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
*/ |
79
|
|
|
public function getById($id) |
80
|
|
|
{ |
81
|
|
|
$object = $this->objectManager->create($this->object); |
82
|
|
|
try { |
83
|
|
|
$object->load($id); |
84
|
|
|
if (!$object->getId()) { |
85
|
|
|
throw new NoSuchEntityException(__('Object with id "%1" does not exist.', $id)); |
86
|
|
|
} |
87
|
|
|
return $object; |
88
|
|
|
} catch (Exception $exception) { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function getSingle(SearchCriteriaInterface $criteria) |
97
|
|
|
{ |
98
|
|
|
$list = $this->getList($criteria); |
99
|
|
|
return $list->getTotalCount() > 0 ? $list->getItems()[0] : null; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritDoc |
104
|
|
|
*/ |
105
|
|
|
public function getList(SearchCriteriaInterface $criteria): SearchResultsInterface |
106
|
|
|
{ |
107
|
|
|
$searchResults = $this->searchResultsFactory->create(); |
108
|
|
|
$searchResults->setSearchCriteria($criteria); |
109
|
|
|
$collection = $this->objectManager->create($this->collection); |
110
|
|
|
foreach ($criteria->getFilterGroups() as $filterGroup) { |
111
|
|
|
$fields = []; |
112
|
|
|
$conditions = []; |
113
|
|
|
foreach ($filterGroup->getFilters() as $filter) { |
114
|
|
|
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq'; |
115
|
|
|
$fields[] = $filter->getField(); |
116
|
|
|
$conditions[] = [$condition => $filter->getValue()]; |
117
|
|
|
} |
118
|
|
|
if ($fields) { |
119
|
|
|
$collection->addFieldToFilter($fields, $conditions); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
$searchResults->setTotalCount($collection->getSize()); |
123
|
|
|
$sortOrders = $criteria->getSortOrders(); |
124
|
|
|
if ($sortOrders) { |
125
|
|
|
/** @var SortOrder $sortOrder */ |
126
|
|
|
foreach ($sortOrders as $sortOrder) { |
127
|
|
|
$collection->addOrder( |
128
|
|
|
$sortOrder->getField(), |
129
|
|
|
($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC' |
130
|
|
|
); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
$collection->setCurPage($criteria->getCurrentPage()); |
134
|
|
|
$collection->setPageSize($criteria->getPageSize()); |
135
|
|
|
$objects = []; |
136
|
|
|
foreach ($collection as $objectModel) { |
137
|
|
|
$objects[] = $objectModel; |
138
|
|
|
} |
139
|
|
|
$searchResults->setItems($objects); |
140
|
|
|
return $searchResults; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @inheritDoc |
145
|
|
|
* We're dealing here with some high level issue like returning an object with information about deleting action |
146
|
|
|
* Perhaps a returning normalized interface would be more convenient instead |
147
|
|
|
*/ |
148
|
|
|
public function delete($object): array |
149
|
|
|
{ |
150
|
|
|
$result = ['success' => true, 'message' => 'successfully deleted!!!']; |
151
|
|
|
try { |
152
|
|
|
$object->delete(); |
153
|
|
|
} catch (Throwable $exception) { |
154
|
|
|
$result = ['success' => false, 'message' => $exception->getMessage()]; |
155
|
|
|
} |
156
|
|
|
return $result; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @inheritDoc |
161
|
|
|
*/ |
162
|
|
|
public function deleteById($id): array |
163
|
|
|
{ |
164
|
|
|
$object = $this->getById($id); |
165
|
|
|
$result = $this->delete($object); |
166
|
|
|
return $result; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths