-
Notifications
You must be signed in to change notification settings - Fork 26
/
UrlKeyCollection.php
144 lines (118 loc) · 4.42 KB
/
UrlKeyCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
declare(strict_types=1);
namespace Baldwin\UrlDataIntegrityChecker\Model\ResourceModel\Catalog\Category;
use Baldwin\UrlDataIntegrityChecker\Checker\Catalog\Category\UrlKey as UrlKeyChecker;
use Baldwin\UrlDataIntegrityChecker\Storage\StorageInterface;
use Magento\Framework\Api\AttributeInterface;
use Magento\Framework\Api\AttributeValue;
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Data\Collection as DataCollection;
use Magento\Framework\Data\Collection\EntityFactoryInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
class UrlKeyCollection extends DataCollection implements SearchResultInterface
{
private $storage;
public function __construct(
EntityFactoryInterface $entityFactory,
StorageInterface $storage
) {
parent::__construct($entityFactory);
$this->storage = $storage;
}
/**
* @param bool $printQuery
* @param bool $logQuery
*
* @return UrlKeyCollection<DataObject>
*/
public function loadData($printQuery = false, $logQuery = false)
{
if (!$this->isLoaded()) {
$urlKeys = $this->storage->read(UrlKeyChecker::STORAGE_IDENTIFIER);
foreach ($urlKeys as $urlKey) {
$this->addItem($this->createDataObject($urlKey));
}
foreach ($this->_orders as $field => $direction) {
usort($this->_items, function ($itemA, $itemB) use ($field, $direction) {
$comparisonFieldA = $itemA->getData($field);
$comparisonFieldB = $itemB->getData($field);
if ($direction === DataCollection::SORT_ORDER_ASC) {
return $comparisonFieldA <=> $comparisonFieldB;
} else {
return $comparisonFieldB <=> $comparisonFieldA;
}
});
break; // breaking after using one entry of $this->_orders
}
$this->_setIsLoaded();
// fill $this->_totalRecords by calling getSize
// if we don't do this and leave this up to Magento, it will do it too late in Magento 2.4.x
$this->getSize();
// page the data, need to do this after setting the data as loaded,
// otherwise the getCurPage would create a recursive problem
$startIndex = ($this->getCurPage() - 1) * $this->getPageSize();
$this->_items = array_slice($this->_items, $startIndex, $this->getPageSize());
}
return $this;
}
/**
* @param array<string, mixed> $arguments
*/
public function createDataObject(array $arguments = []): DataObject
{
$arguments['hash'] = sha1(json_encode($arguments) ?: '');
$obj = $this->_entityFactory->create($this->_itemObjectClass, ['data' => $arguments]);
$attributes = [];
foreach ($arguments as $key => $value) {
$attribute = new AttributeValue([
AttributeInterface::ATTRIBUTE_CODE => $key,
AttributeInterface::VALUE => $value,
]);
$attributes[] = $attribute;
}
$obj->setCustomAttributes($attributes);
return $obj;
}
/**
* @return UrlKeyCollection<DataObject>
*/
public function setItems(array $items = null)
{
throw new LocalizedException(__('Not implemented: setItems!'));
}
public function getAggregations()
{
throw new LocalizedException(__('Not implemented: getAggregations!'));
}
/**
* @return UrlKeyCollection<DataObject>
*/
public function setAggregations($aggregations)
{
throw new LocalizedException(__('Not implemented: setAggregations!'));
}
public function getSearchCriteria()
{
throw new LocalizedException(__('Not implemented: getSearchCriteria!'));
}
/**
* @return UrlKeyCollection<DataObject>
*/
public function setSearchCriteria(SearchCriteriaInterface $searchCriteria)
{
throw new LocalizedException(__('Not implemented: setSearchCriteria!'));
}
public function getTotalCount()
{
return $this->getSize();
}
/**
* @return UrlKeyCollection<DataObject>
*/
public function setTotalCount($totalCount)
{
throw new LocalizedException(__('Not implemented: setTotalCount!'));
}
}