22 private ?
string $section;
23 private ?
string $subSection;
24 private ?
string $element;
25 private ?
string $type;
32 private string $status = self::STATUS_SUCCESS;
34 private string $userAgent;
36 private string $dbname;
38 private bool $isInvalid =
false;
41 private readonly
string $event,
42 private readonly
string $tool,
43 private readonly
string $category,
46 $userAgent = Context::getCurrent()?->getRequest()->getUserAgent();
47 if ($userAgent && \is_string($userAgent))
52 $httpHost = Context::getCurrent()?->getServer()->getHttpHost();
53 if ($httpHost && \is_string($httpHost))
58 $dbname = \defined(
'BX24_DB_NAME') ? BX24_DB_NAME :
null;
59 if ($dbname && \is_string($dbname))
67 $this->userId = $userId;
74 $this->section = $section;
81 $this->subSection = $subSection;
88 $this->element = $element;
93 public function setType(
string $type): self
103 public function setP1(
string $p1): self
105 $this->validatePField($p1);
114 public function setP2(
string $p2): self
116 $this->validatePField($p2);
125 public function setP3(
string $p3): self
127 $this->validatePField($p3);
136 public function setP4(
string $p4): self
138 $this->validatePField($p4);
147 public function setP5(
string $p5): self
149 $this->validatePField($p5);
157 $this->status = $status;
164 $this->userAgent = $userAgent;
178 $this->dbname = $dbname;
185 return $this->
setStatus(self::STATUS_SUCCESS);
190 return $this->
setStatus(self::STATUS_CANCEL);
195 return $this->
setStatus(self::STATUS_ERROR);
200 return $this->
setStatus(self::STATUS_ATTEMPT);
206 private function validatePField(
string $value): void
208 $invalidValue = substr_count($value,
'_') > 1;
211 $this->isInvalid =
true;
214 if ($invalidValue && $this->isDevMode())
216 throw new ArgumentException(
'Value for p{1-5} field must contain a single underscore.');
223 private function validateRequiredFields(): void
225 if (empty($this->event) || empty($this->tool) || empty($this->category))
227 $this->isInvalid =
true;
229 if ($this->isDevMode())
231 throw new ArgumentException(
'Event, tool and category fields are required and should be filled.');
236 private function isDevMode(): bool
238 $exceptionHandling = Configuration::getValue(
'exception_handling');
240 return !empty($exceptionHandling[
'debug']);
247 'tool' => $this->tool,
248 'category' => $this->category,
249 'section' => $this->section ??
null,
250 'subSection' => $this->subSection ??
null,
251 'element' => $this->element ??
null,
252 'type' => $this->type ??
null,
253 'p1' => $this->p1 ??
null,
254 'p2' => $this->p2 ??
null,
255 'p3' => $this->p3 ??
null,
256 'p4' => $this->p4 ??
null,
257 'p5' => $this->p5 ??
null,
258 'status' => $this->status ??
null,
259 'userAgent' => $this->userAgent ??
null,
268 if (!\defined(
'ANALYTICS_V2_FILENAME') || !is_writable(ANALYTICS_V2_FILENAME))
273 $this->validateRequiredFields();
274 if ($this->isInvalid)
279 if (!isset($this->userId) && !\defined(
'BX_CHECK_AGENT_START') && (
$GLOBALS[
'USER'] instanceof \CUser))
281 $this->userId = (int)
$GLOBALS[
'USER']->getId();
284 $data = $this->buildLogData();
285 if ($this->isDevMode())
287 $this->triggerDebugEvent(
$data);
290 $logger =
new FileLogger(ANALYTICS_V2_FILENAME, 0);
292 $logger->debug(
'',
$data);
295 private function triggerDebugEvent(
array $data): void
297 $event =
new Event(
'main',
'OnAnalyticsEvent', [
'analyticsEvent' => $this,
'eventData' =>
$data]);
301 private function buildLogData():
array
304 'date' => date(
'Y-m-d H:i:s'),
305 'host' => $this->host ??
null,
306 'dbname' => $this->dbname ??
null,
307 'userId' => $this->userId ?? 0,
313 $data[
'license'] = \CBitrix24::getLicenseType();
setUserAgent(string $userAgent)
setElement(string $element)
setSubSection(string $subSection)
setStatus(string $status)
__construct(private readonly string $event, private readonly string $tool, private readonly string $category,)
setSection(string $section)
setDbName(string $dbname)
static includeModule($moduleName)
$GLOBALS['____1690880296']