1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Base Repository Class |
6
|
|
|
* @category Ticaje |
7
|
|
|
* @package Ticaje_Base |
8
|
|
|
* @author Hector Luis Barrientos <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Ticaje\Base\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\Base\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
|
|
|
protected $object; // The current model class name |
32
|
|
|
|
33
|
|
|
protected $collection; // The current model collection class name |
34
|
|
|
|
35
|
|
|
/** @var SearchResultsInterfaceFactory $searchResultsFactory */ |
36
|
|
|
protected $searchResultsFactory; |
37
|
|
|
|
38
|
|
|
/** @var ObjectManagerInterface $objectManager */ |
39
|
|
|
protected $objectManager; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* BaseRepository constructor. |
43
|
|
|
* @param string $object |
44
|
|
|
* @param string $collection |
45
|
|
|
* @param SearchResultsInterfaceFactory $searchResultsFactory |
46
|
|
|
* @param ObjectManagerInterface $objectManager |
47
|
|
|
*/ |
48
|
|
|
public function __construct( |
49
|
|
|
string $object, |
50
|
|
|
string $collection, |
51
|
|
|
SearchResultsInterfaceFactory $searchResultsFactory, |
52
|
|
|
ObjectManagerInterface $objectManager |
53
|
|
|
) { |
54
|
|
|
$this->object = $object; |
55
|
|
|
$this->collection = $collection; |
56
|
|
|
$this->searchResultsFactory = $searchResultsFactory; |
57
|
|
|
$this->objectManager = $objectManager; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @inheritDoc |
62
|
|
|
*/ |
63
|
|
|
public function save($object) |
64
|
|
|
{ |
65
|
|
|
try { |
66
|
|
|
$object->save(); |
67
|
|
|
} catch (Exception $exception) { |
68
|
|
|
throw new CouldNotSaveException(__($exception->getMessage())); |
69
|
|
|
} |
70
|
|
|
return $object; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
|
|
public function getById($id) |
77
|
|
|
{ |
78
|
|
|
$object = $this->objectManager->get($this->object); |
79
|
|
|
try { |
80
|
|
|
$object->load($id); |
81
|
|
|
if (!$object->getId()) { |
82
|
|
|
throw new NoSuchEntityException(__('Object with id "%1" does not exist.', $id)); |
83
|
|
|
} |
84
|
|
|
return $object; |
85
|
|
|
} catch (Exception $exception) { |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
|
|
public function getSingle(SearchCriteriaInterface $criteria) |
94
|
|
|
{ |
95
|
|
|
$list = $this->getList($criteria); |
96
|
|
|
return $list->getTotalCount() > 0 ? $list->getItems()[0] : null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritDoc |
101
|
|
|
*/ |
102
|
|
|
public function getList(SearchCriteriaInterface $criteria): SearchResultsInterface |
103
|
|
|
{ |
104
|
|
|
$searchResults = $this->searchResultsFactory->create(); |
105
|
|
|
$searchResults->setSearchCriteria($criteria); |
106
|
|
|
$collection = $this->objectManager->get($this->collection); |
107
|
|
|
foreach ($criteria->getFilterGroups() as $filterGroup) { |
108
|
|
|
$fields = []; |
109
|
|
|
$conditions = []; |
110
|
|
|
foreach ($filterGroup->getFilters() as $filter) { |
111
|
|
|
$condition = $filter->getConditionType() ? $filter->getConditionType() : 'eq'; |
112
|
|
|
$fields[] = $filter->getField(); |
113
|
|
|
$conditions[] = [$condition => $filter->getValue()]; |
114
|
|
|
} |
115
|
|
|
if ($fields) { |
116
|
|
|
$collection->addFieldToFilter($fields, $conditions); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
$searchResults->setTotalCount($collection->getSize()); |
120
|
|
|
$sortOrders = $criteria->getSortOrders(); |
121
|
|
|
if ($sortOrders) { |
122
|
|
|
/** @var SortOrder $sortOrder */ |
123
|
|
|
foreach ($sortOrders as $sortOrder) { |
124
|
|
|
$collection->addOrder( |
125
|
|
|
$sortOrder->getField(), |
126
|
|
|
($sortOrder->getDirection() == SortOrder::SORT_ASC) ? 'ASC' : 'DESC' |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
$collection->setCurPage($criteria->getCurrentPage()); |
131
|
|
|
$collection->setPageSize($criteria->getPageSize()); |
132
|
|
|
$objects = []; |
133
|
|
|
foreach ($collection as $objectModel) { |
134
|
|
|
$objects[] = $objectModel; |
135
|
|
|
} |
136
|
|
|
$searchResults->setItems($objects); |
137
|
|
|
return $searchResults; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @inheritDoc |
142
|
|
|
* We're dealing here with some high level issue like returning an object with information about deleting action |
143
|
|
|
* Perhaps a returning normalized interface would be more convenient instead |
144
|
|
|
*/ |
145
|
|
|
public function delete($object): array |
146
|
|
|
{ |
147
|
|
|
$result = ['success' => true, 'message' => 'successfully deleted!!!']; |
148
|
|
|
try { |
149
|
|
|
$object->delete(); |
150
|
|
|
} catch (Throwable $exception) { |
151
|
|
|
$result = ['success' => false, 'message' => $exception->getMessage()]; |
152
|
|
|
} |
153
|
|
|
return $result; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
|
|
public function deleteById($id): array |
160
|
|
|
{ |
161
|
|
|
$object = $this->getById($id); |
162
|
|
|
$result = $this->delete($object); |
163
|
|
|
return $result; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
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