Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Manager.php
1<?php
2
4
10
12{
13 public const ERROR_INCORRECT_MODE = 'INCORRECT DEBUGGER MODE';
14 public const ERROR_DEBUGGER_ALREADY_STARTED = 'DEBUGGER IS ALREADY STARTED';
15
16 private static ?Session $activeSession = null;
17 private static bool $isActiveSessionChecked = false;
18
19 private const CACHE_TTL = 3600;
20
21 private static function getList(array $filter = []): ?Session
22 {
24 'select' => ['*', 'DOCUMENTS', 'WORKFLOW_CONTEXTS'],
25 'filter' => $filter,
26 ])->fetchObject();
27 }
28
29 public static function getCachedSession(): ?Session
30 {
32 'select' => ['*'],
33 'filter' => ['=ACTIVE' => 'Y'],
34 'cache' => ['ttl' => self::CACHE_TTL],
35 ])->fetchObject();
36 }
37
38 public static function getActiveSession(): ?Session
39 {
40 if (!self::$isActiveSessionChecked)
41 {
42 self::setActiveSession(static::getList(['=ACTIVE' => 'Y']));
43 }
44
45 return self::$activeSession;
46 }
47
48 public static function getSessionById(string $sessionId): ?Session
49 {
50 if (self::$activeSession && self::$activeSession->getId() === $sessionId)
51 {
52 return self::$activeSession;
53 }
54
55 $session = static::getList(['=ID' => $sessionId]);
56 if ($session && $session->isActive())
57 {
58 self::setActiveSession($session);
59 }
60
61 return $session;
62 }
63
64 public static function startSession(array $parameterDocumentType, int $mode, int $userId, int $categoryId = 0)
65 {
66 if (!Mode::isMode($mode))
67 {
68 return static::getResult(self::ERROR_INCORRECT_MODE);
69 }
70
71 [$module, $entity, $documentType] = \CBPHelper::ParseDocumentId($parameterDocumentType);
72
73 $session = new Session();
74 $session
75 ->setId(uniqid('', true))
76 ->setModuleId($module)
77 ->setEntity($entity)
78 ->setDocumentType($documentType)
79 ->setMode($mode)
80 ->setStartedBy($userId)
81 //->setStartedDate(new \Bitrix\Main\Type\DateTime())
82 ->setActive(true)
83 ->setDocumentCategoryId($categoryId)
84 ;
85
86 $activeSession = self::getActiveSession();
87 if ($activeSession)
88 {
89 return self::getResult(
90 self::ERROR_DEBUGGER_ALREADY_STARTED,
91 ['session' => $activeSession]
92 );
93 }
94 self::clearStaticCache();
95
96 $result = $session->save();
97 if ($result->isSuccess())
98 {
100 $sessionFromResult = $result->getObject();
101 $sessionFromResult->fillWorkflowContexts();
102 $sessionFromResult->fillDocuments();
103 self::setActiveSession($sessionFromResult);
104 }
105
106 return $result;
107 }
108
109 public static function finishSession(Session $session)
110 {
111 $result = $session->finish();
112 if ($result->isSuccess())
113 {
114 self::clearStaticCache();
115 }
116
117 return $result;
118 }
119
120 public static function canUserStartSession(int $userId, array $parameterDocumentType): bool
121 {
122 $hasRights = self::canUserDebugAutomation($userId, $parameterDocumentType);
123 if ($hasRights)
124 {
125 return self::canStartSession();
126 }
127
128 return false;
129 }
130
131 public static function canUserDebugAutomation(int $userId, array $parameterDocumentType): bool
132 {
133 return static::isAvailable($parameterDocumentType) && \CBPDocument::canUserOperateDocumentType(
134 \CBPCanUserOperateOperation::DebugAutomation,
135 $userId,
136 $parameterDocumentType
137 );
138 }
139
140 public static function canStartSession(): bool
141 {
142 if (self::getActiveSession())
143 {
144 return false;
145 }
146
147 return true;
148 }
149
150 private static function getResult(string $errorCode, array $data = []): \Bitrix\Main\Result
151 {
152 $result = new \Bitrix\Main\Result();
153 $result->setData($data);
154
155 $error = static::getCustomError($errorCode, $data);
156 if ($error)
157 {
158 $result->addError($error);
159 }
160
161 return $result;
162 }
163
164 private static function getCustomError(string $errorCode, array $data = []): ?\Bitrix\Main\Error
165 {
167 $session = $data['session'];
168
169 if ($errorCode === self::ERROR_INCORRECT_MODE)
170 {
171 return new \Bitrix\Main\Error(
172 Loc::getMessage('BIZPROC_DEBUGGER_SESSION_MANAGER_ERROR_INCORRECT_MODE'),
173 $errorCode
174 );
175 }
176 elseif ($errorCode === self::ERROR_DEBUGGER_ALREADY_STARTED)
177 {
178 return new \Bitrix\Main\Error($session->getShortDescription(), $errorCode);
179 }
180
181 return null;
182 }
183
184 public static function deleteInactiveSession(string $sessionId): Result
185 {
186 $session = static::getList(['=ID' => $sessionId]);
187
188 $result = new Result();
189 if (!$session)
190 {
191 $errorMessage = Loc::getMessage('BIZPROC_DEBUGGER_SESSION_MANAGER_ERROR_SESSION_NOT_FOUND');
192 $result->addError(new Error($errorMessage));
193 }
194 else if ($session->isActive())
195 {
196 $errorMessage = Loc::getMessage('BIZPROC_DEBUGGER_SESSION_MANAGER_ERROR_SESSION_STILL_ACTIVE');
197 $result->addError(new Error($errorMessage));
198 }
199 else
200 {
201 $deletionResult = $session->deleteAll();
202
203 $result->setData($deletionResult->getData());
204 $result->addErrors($deletionResult->getErrors());
205 }
206
207 return $result;
208 }
209
210 public static function isDebugWorkflow(string $workflowId): bool
211 {
212 return (bool)DebuggerSessionWorkflowContextTable::getRow([
213 'filter' => ['=WORKFLOW_ID' => $workflowId],
214 ]);
215 }
216
217 public static function getDebuggerState(): DebuggerState
218 {
219 $session = static::getActiveSession();
220
221 if ($session)
222 {
223 return new DebuggerState($session->getDebuggerState());
224 }
225
227 }
228
229 public static function setDebuggerState(DebuggerState $state)
230 {
231 $session = static::getActiveSession();
232
233 if ($session)
234 {
235 $session->setDebuggerState($state->getId());
236 $session->save();
237 }
238 }
239
240 private static function isAvailable(array $documentType): bool
241 {
242 //todo: temporary
243
244 return (
245 $documentType[0] === 'crm'
246 && $documentType[2] === 'DEAL'
247 );
248 }
249
250 private static function setActiveSession(?Session $activeSession)
251 {
252 self::$activeSession = $activeSession;
253 self::$isActiveSessionChecked = true;
254 }
255
256 private static function clearStaticCache()
257 {
258 self::$activeSession = null;
259 self::$isActiveSessionChecked = false;
260 }
261}
static canUserStartSession(int $userId, array $parameterDocumentType)
Definition Manager.php:120
static deleteInactiveSession(string $sessionId)
Definition Manager.php:184
static finishSession(Session $session)
Definition Manager.php:109
static setDebuggerState(DebuggerState $state)
Definition Manager.php:229
static canUserDebugAutomation(int $userId, array $parameterDocumentType)
Definition Manager.php:131
static isDebugWorkflow(string $workflowId)
Definition Manager.php:210
static getSessionById(string $sessionId)
Definition Manager.php:48
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())