1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ConverterService.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Socialnetwork\Collab\Converter;
6
7use Bitrix\Main\Application;
8use Bitrix\Main\DB\Connection;
9use Bitrix\Main\Error;
10use Bitrix\Main\Result;
11use Bitrix\Socialnetwork\Collab\Converter\Command\AbstractConverterCommand;
12use Bitrix\Socialnetwork\Collab\Converter\Event\ConverterEvent;
13use Bitrix\Socialnetwork\Collab\Converter\Handler\AbstractHandler;
14use Bitrix\Socialnetwork\Collab\Converter\Result\ConverterResult;
15use Bitrix\Socialnetwork\Internals\EventService;
16use Bitrix\Socialnetwork\Internals\Registry\GroupRegistry;
17
19{
20 private Connection $connection;
21
22 public function __construct()
23 {
24 $this->connection = Application::getConnection();
25 }
26
28 {
30
31 $validationResult = $command->validateGroup();
32 if (!$validationResult->isSuccess())
33 {
34 return $result->addErrors($validationResult->getErrors());
35 }
36
37 $entityBefore = $command->getGroup();
38
39 $this->connection->startTransaction();
40
41 try
42 {
43 $handlersResult = $this->runHandlers($command);
44
45 if (!$handlersResult->isSuccess())
46 {
47 $this->revertChanges($command->getGroup()->getId());
48
49 return $result->addErrors($handlersResult->getErrors());
50 }
51
52 GroupRegistry::getInstance()->invalidate($command->getGroup()->getId());
53 $entityAfter = GroupRegistry::getInstance()->get($entityBefore->getId());
54
55 if (!$entityAfter)
56 {
57 $this->revertChanges($command->getGroup()->getId());
58
59 return $result->addError(new Error('Group not found after conversion'));
60 }
61
62 $this->connection->commitTransaction();
63 }
64 catch (\Throwable $exception)
65 {
66 $this->revertChanges($command->getGroup()->getId());
67
68 return $result->addError(Error::createFromThrowable($exception));
69 }
70
71 (new ConverterEvent($command, $entityBefore, $entityAfter))->send();
72
74 'GROUP_ID' => $command->getGroup()->getId(),
75 ]);
76
77 return $result->setEntityAfter($entityAfter);
78 }
79
80 private function runHandlers(AbstractConverterCommand $command): Result
81 {
82 $result = new Result();
83 foreach ($command->getHandlers() as $handler)
84 {
85 if (!$handler instanceof AbstractHandler)
86 {
87 continue;
88 }
89
90 $handlerResult = $handler->execute($command);
91
92 if (!$handlerResult->isSuccess())
93 {
94 $result->addErrors($handlerResult->getErrors());
95 break;
96 }
97 }
98
99 return $result;
100 }
101
102 private function revertChanges(int $id): void
103 {
104 global $CACHE_MANAGER;
105 $CACHE_MANAGER->ClearByTag('sonet_group_' . $id);
106 $this->connection->rollbackTransaction();
107 }
108}
Определения error.php:15
convert(AbstractConverterCommand $command)
Определения ConverterService.php:27
static addEvent(string $type, array $data)
Определения service.php:45
global $CACHE_MANAGER
Определения clear_component_cache.php:7
$result
Определения get_property_values.php:14