Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
StoreProductRepository.php
1<?php
2
4
9
20{
22 protected $factory;
23
25 {
26 $this->factory = $factory;
27 }
31 public function getEntityById(int $id): ?BaseEntity
32 {
33 if ($id <= 0)
34 {
35 throw new \OutOfRangeException($id);
36 }
37
38 $entities = $this->getEntitiesBy([
39 'filter' => [
40 '=ID' => $id,
41 ],
42 ]);
43
44 return reset($entities) ?: null;
45 }
46
50 public function getEntitiesBy($params): array
51 {
52 $entities = [];
53
54 foreach ($this->getList((array)$params) as $item)
55 {
56 $entities[] = $this->createEntity($item);
57 }
58
59 return $entities;
60 }
61
62 public function getProductId(BaseEntity $entity): ?int
63 {
64 $id = null;
65
66 $parent = $entity->getParent();
67
68 if ($parent && !$parent->isNew())
69 {
70 $id = $parent->getId();
71 }
72
73 return $id;
74 }
75
79 public function save(BaseEntity ...$entities): Result
80 {
81 return new Result();
82 }
83
87 public function delete(BaseEntity ...$entities): Result
88 {
89 return new Result();
90 }
91
93 {
94 if ($sku->isNew())
95 {
96 return $this->createCollection();
97 }
98
99 $result = $this->getByProductId($sku->getId());
100
101 return $this->createCollection($result);
102 }
103
104 protected function getByProductId(int $skuId): array
105 {
106 return $this->getList([
107 'filter' => [
108 '=PRODUCT_ID' => $skuId,
109 ],
110 ]);
111 }
112
113 protected function getList(array $params): array
114 {
115 $rows = Catalog\StoreProductTable::getList($params)->fetchAll();
116
117 return array_column($rows, null, 'STORE_ID');
118 }
119
120 protected function createEntity(array $fields = []): StoreProduct
121 {
122 $entity = $this->factory->createEntity();
123
124 $entity->initFields($fields);
125
126 return $entity;
127 }
128
129 protected function createCollection(array $entityFields = []): StoreProductCollection
130 {
131 $collection = $this->factory->createCollection();
132
133 foreach ($this->getStoreSettings() as $settings)
134 {
135 $fields = $entityFields[$settings['ID']]
136 ?? [
137 'STORE_ID' => $settings['ID'],
138 ];
139 $storeProduct = $this->createEntity($fields);
140 $storeProduct->setSettings($settings);
141 $collection->add($storeProduct);
142 }
143
144 return $collection;
145 }
146
147 protected function addInternal(array $fields): Result
148 {
149 $result = new Result();
150
151 $res = Catalog\StoreProductTable::add($fields);
152 if ($res->isSuccess())
153 {
154 $result->setData(['ID' => $res->getId()]);
155 }
156 else
157 {
158 $result->addErrors($res->getErrors());
159 }
160
161 return $result;
162 }
163
164 protected function updateInternal(int $id, array $fields): Result
165 {
166 $result = new Result();
167
168 $res = Catalog\StoreProductTable::update($id, $fields);
169
170 if (!$res->isSuccess())
171 {
172 $result->addErrors($res->getErrors());
173 }
174
175 return $result;
176 }
177
178 protected function deleteInternal(int $id): Result
179 {
180 $result = new Result();
181
182 $res = Catalog\StoreProductTable::delete($id);
183
184 if (!$res->isSuccess())
185 {
186 $result->addErrors($res->getErrors());
187 }
188
189 return $result;
190 }
191
192 private function getStoreSettings(): array
193 {
194 static $storeSettings = null;
195
196 if ($storeSettings === null)
197 {
198 $storeSettings = Catalog\StoreTable::getList([
199 'filter' => ['=ACTIVE' => 'Y'],
200 ])->fetchAll();
201 }
202
203 return $storeSettings;
204 }
205}