1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
AbstractGroupService.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
6
23
25{
28
29 public function __construct()
30 {
31 $this->init();
32 }
33
35 abstract protected function getAddHandlers(): array;
36
38 abstract protected function getUpdateHandlers(): array;
39
41 abstract protected function getDeleteHandlers(): array;
42
43 public function add(AddCommand $command): GroupResult
44 {
45 $checkResult = $this->checkAddCommand($command);
46 if (!$checkResult->isSuccess())
47 {
48 return $this->finalizeAddResult($checkResult);
49 }
50
51 $result = new GroupResult();
52
53 $validationResult = $this->validate($command);
54 if (!$validationResult->isSuccess())
55 {
56 return $this->finalizeAddResult($validationResult);
57 }
58
59 $operationResult = (new AddOperation($command))->run();
60 if (!$operationResult->isSuccess())
61 {
62 return $this->finalizeAddResult($operationResult);
63 }
64
65 $entity = $operationResult->getGroup();
66 if ($entity === null)
67 {
68 return $this->finalizeAddResult($operationResult);
69 }
70
71 $result->merge($operationResult);
72
73 $handlerResult = $this->runAddHandlers($command, $entity);
74
75 $this->sendAddEvent($command, $entity);
76
77 $result->merge($handlerResult);
78
79 $entity = $this->getEntityById($entity->getId());
80
81 $result->setGroup($entity);
82
83 return $this->finalizeAddResult($result);
84 }
85
86 public function update(UpdateCommand $command): GroupResult
87 {
88 $checkResult = $this->checkUpdateCommand($command);
89 if (!$checkResult->isSuccess())
90 {
91 return $this->finalizeUpdateResult($checkResult);
92 }
93
94 $result = new GroupResult();
95
96 $validationResult = $this->validate($command);
97 if (!$validationResult->isSuccess())
98 {
99 return $this->finalizeUpdateResult($validationResult);
100 }
101
102 $entityBefore = $this->getEntityById($command->getId(), false);
103 if ($entityBefore === null)
104 {
105 $result->addError(new Error('Group not found'));
106
107 return $this->finalizeUpdateResult($result);
108 }
109
110 $this->sendBeforeUpdateEvent($command, $entityBefore);
111
112 $operationResult = (new UpdateOperation($command))->run();
113 if (!$operationResult->isSuccess())
114 {
115 return $this->finalizeUpdateResult($operationResult);
116 }
117
118 $entityAfter = $operationResult->getGroup();
119 if ($entityAfter === null)
120 {
121 return $this->finalizeUpdateResult($operationResult);
122 }
123
124 $result->merge($operationResult);
125
126 $handlerResult = $this->runUpdateHandlers($command, $entityBefore, $entityAfter);
127
128 $this->sendUpdateEvent($command, $entityBefore, $entityAfter);
129
130 $result->merge($handlerResult);
131
132 $entity = $this->getEntityById($entityAfter->getId());
133
134 $result->setGroup($entity);
135
136 return $this->finalizeUpdateResult($result);
137 }
138
139 public function delete(DeleteCommand $command): GroupResult
140 {
141 $checkResult = $this->checkDeleteCommand($command);
142 if (!$checkResult->isSuccess())
143 {
144 return $this->finalizeDeleteResult($checkResult);
145 }
146
147 $result = new GroupResult();
148
149 $validationResult = $this->validate($command);
150 if (!$validationResult->isSuccess())
151 {
152 return $this->finalizeDeleteResult($validationResult);
153 }
154
155 $entityBefore = $this->getEntityById($command->getId(), false);
156 if ($entityBefore === null)
157 {
158 $result = new GroupResult();
159 $result->addError(new Error('Group not found'));
160
161 return $this->finalizeDeleteResult($result);
162 }
163
164 $operationResult = (new DeleteOperation($command))->run();
165
166 if (!$operationResult->isSuccess())
167 {
168 return $this->finalizeDeleteResult($operationResult);
169 }
170
171 $result->merge($operationResult);
172
173 $handlerResult = $this->runDeleteHandlers($command, $entityBefore);
174
175 $this->sendDeleteEvent($command, $entityBefore);
176
177 $result->merge($handlerResult);
178
179 return $this->finalizeDeleteResult($result);
180 }
181
182 protected function checkAddCommand(AddCommand $command): GroupResult
183 {
184 return new GroupResult();
185 }
186
187 protected function checkUpdateCommand(UpdateCommand $command): GroupResult
188 {
189 return new GroupResult();
190 }
191
192 protected function checkDeleteCommand(DeleteCommand $command): GroupResult
193 {
194 return new GroupResult();
195 }
196
198 {
199 return $result;
200 }
201
203 {
204 return $result;
205 }
206
208 {
209 return $result;
210 }
211
212 protected function sendAddEvent(AddCommand $command, Workgroup $entity): void
213 {
214
215 }
216
217 protected function sendBeforeUpdateEvent(UpdateCommand $command, Workgroup $entity): void
218 {
219
220 }
221
222 protected function sendUpdateEvent(UpdateCommand $command, Workgroup $entityBefore, Workgroup $entityAfter): void
223 {
224
225 }
226
227 protected function sendDeleteEvent(DeleteCommand $command, Workgroup $entityBefore): void
228 {
229
230 }
231
232 protected function init(): void
233 {
234 $this->validationService = ServiceLocator::getInstance()->get('main.validation.service');
235 $this->registry = GroupRegistry::getInstance();
236 }
237
238 private function runAddHandlers(AddCommand $command, Workgroup &$entity): GroupResult
239 {
240 $id = $entity->getId();
241
242 $result = new GroupResult();
243 foreach ($this->getAddHandlers() as $handler)
244 {
245 $handlerResult = $handler->add($command, $entity);
246 if ($handlerResult->isGroupChanged())
247 {
248 $entity = $this->registry->invalidate($id)->get($id);
249 if ($entity === null)
250 {
251 $result->addError(new Error('Collab lost during add'));
252
253 return $result;
254 }
255 }
256 $result->merge($handlerResult);
257 }
258
259 return $result;
260 }
261
262 private function runUpdateHandlers(
263 UpdateCommand $command,
264 Workgroup $entityBefore,
265 Workgroup &$entityAfter,
266 ): GroupResult
267 {
268 $id = $command->getId();
269
270 $result = new GroupResult();
271 foreach ($this->getUpdateHandlers() as $handler)
272 {
273 $handlerResult = $handler->update($command, $entityBefore, $entityAfter);
274 if ($handlerResult->isGroupChanged())
275 {
276 $entityAfter = $this->registry->invalidate($id)->get($id);
277 if ($entityAfter === null)
278 {
279 $result->addError(new Error('Collab lost during update'));
280
281 return $result;
282 }
283 }
284
285 $result->merge($handlerResult);
286 }
287
288 return $result;
289 }
290
291 private function runDeleteHandlers(DeleteCommand $command, Workgroup $entityBefore): GroupResult
292 {
293 $result = new GroupResult();
294 foreach ($this->getDeleteHandlers() as $handler)
295 {
296 $handlerResult = $handler->delete($command, $entityBefore);
297 $result->merge($handlerResult);
298 }
299
300 return $result;
301 }
302
303 private function getEntityById(int $id, bool $force = true): ?Workgroup
304 {
305 if ($force)
306 {
307 $this->registry->invalidate($id);
308 }
309
310 return $this->registry->get($id);
311 }
312
313 private function validate(AbstractCommand $command): GroupResult
314 {
315 $result = new GroupResult();
316
317 $validationResult = $this->validationService->validate($command);
318 if (!$validationResult->isSuccess())
319 {
320 $result->merge($validationResult);
321
322 return $result;
323 }
324
325 return $result;
326 }
327}
Определения error.php:15
sendAddEvent(AddCommand $command, Workgroup $entity)
Определения AbstractGroupService.php:212
sendUpdateEvent(UpdateCommand $command, Workgroup $entityBefore, Workgroup $entityAfter)
Определения AbstractGroupService.php:222
sendDeleteEvent(DeleteCommand $command, Workgroup $entityBefore)
Определения AbstractGroupService.php:227
checkDeleteCommand(DeleteCommand $command)
Определения AbstractGroupService.php:192
sendBeforeUpdateEvent(UpdateCommand $command, Workgroup $entity)
Определения AbstractGroupService.php:217
checkUpdateCommand(UpdateCommand $command)
Определения AbstractGroupService.php:187
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$entity
trait Error
Определения error.php:11