Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BarcodeRepository.php
1<?php
2
4
10
21{
23 protected $factory;
24
26 {
27 $this->factory = $factory;
28 }
32 public function getEntityById(int $id): ?BaseEntity
33 {
34 if ($id <= 0)
35 {
36 throw new \OutOfRangeException($id);
37 }
38
39 $entities = $this->getEntitiesBy([
40 'filter' => [
41 '=ID' => $id,
42 ],
43 ]);
44
45 return reset($entities) ?: null;
46 }
47
51 public function getEntitiesBy($params): array
52 {
53 $entities = [];
54
55 foreach ($this->getList((array)$params) as $item)
56 {
57 $entities[] = $this->createEntity($item);
58 }
59
60 return $entities;
61 }
62
63 public function getProductId(BaseEntity $entity): ?int
64 {
65 $id = null;
66
67 $parent = $entity->getParent();
68
69 if ($parent && !$parent->isNew())
70 {
71 $id = $parent->getId();
72 }
73
74 return $id;
75 }
76
80 public function save(BaseEntity ...$entities): Result
81 {
82 $result = new Result();
83
85 foreach ($entities as $entity)
86 {
87 if (!$entity->getProductId())
88 {
89 $productId = $this->getProductId($entity);
90
91 if ($productId)
92 {
93 $entity->setProductId($productId);
94 }
95 else
96 {
97 $result->addError(new Error('Wrong product id'));
98 continue;
99 }
100 }
101
102 if ($entityId = $entity->getId())
103 {
104 $res = $this->updateInternal($entityId, $entity->getChangedFields());
105
106 if (!$res->isSuccess())
107 {
108 $result->addErrors($res->getErrors());
109 }
110 }
111 else
112 {
113 $res = $this->addInternal($entity->getFields());
114
115 if ($res->isSuccess())
116 {
117 $entity->setId($res->getData()['ID']);
118 }
119 else
120 {
121 $result->addErrors($res->getErrors());
122 }
123 }
124 }
125
126 return $result;
127 }
128
132 public function delete(BaseEntity ...$entities): Result
133 {
134 $result = new Result();
135
137 foreach ($entities as $entity)
138 {
139 if ($entityId = $entity->getId())
140 {
141 $res = $this->deleteInternal($entityId);
142
143 if (!$res->isSuccess())
144 {
145 $result->addErrors($res->getErrors());
146 }
147 }
148 }
149
150 return $result;
151 }
152
154 {
155 if ($sku->isNew())
156 {
157 return $this->createCollection();
158 }
159
160 $result = $this->getByProductId($sku->getId());
161
162 return $this->createCollection($result);
163 }
164
165 protected function getByProductId(int $skuId): array
166 {
167 return $this->getList([
168 'filter' => [
169 '=PRODUCT_ID' => $skuId,
170 '=ORDER_ID' => null,
171 ],
172 ]);
173 }
174
175 protected function getList(array $params): array
176 {
177 $rows = Catalog\StoreBarcodeTable::getList($params)->fetchAll();
178
179 return array_column($rows, null, 'ID');
180 }
181
182 protected function createEntity(array $fields = []): Barcode
183 {
184 $entity = $this->factory->createEntity();
185
186 $entity->initFields($fields);
187
188 return $entity;
189 }
190
191 protected function createCollection(array $entityFields = []): BarcodeCollection
192 {
193 $collection = $this->factory->createCollection();
194
195 foreach ($entityFields as $fields)
196 {
197 $barcode = $this->createEntity($fields);
198 $collection->add($barcode);
199 }
200
201 return $collection;
202 }
203
204 protected function addInternal(array $fields): Result
205 {
206 $result = new Result();
207
208 $res = Catalog\StoreBarcodeTable::add($fields);
209 if ($res->isSuccess())
210 {
211 $result->setData(['ID' => $res->getId()]);
212 }
213 else
214 {
215 $result->addErrors($res->getErrors());
216 }
217
218 return $result;
219 }
220
221 protected function updateInternal(int $id, array $fields): Result
222 {
223 $result = new Result();
224
225 $res = Catalog\StoreBarcodeTable::update($id, $fields);
226
227 if (!$res->isSuccess())
228 {
229 $result->addErrors($res->getErrors());
230 }
231
232 return $result;
233 }
234
235 protected function deleteInternal(int $id): Result
236 {
237 $result = new Result();
238
239 $res = Catalog\StoreBarcodeTable::delete($id);
240
241 if (!$res->isSuccess())
242 {
243 $result->addErrors($res->getErrors());
244 }
245
246 return $result;
247 }
248}
save(BaseEntity ... $entities)