20 private ?
string $section;
21 private ?
string $subSection;
22 private ?
string $element;
23 private ?
string $type;
32 private string $userAgent;
34 private bool $isInvalid =
false;
37 private string $event,
39 private string $category,
43 if ($userAgent && \is_string($userAgent))
45 $this->userAgent = $userAgent;
51 $this->userId = $userId;
58 $this->section = $section;
65 $this->subSection = $subSection;
72 $this->element = $element;
77 public function setType(
string $type): self
87 public function setP1(
string $p1): self
89 $this->validatePField($p1);
98 public function setP2(
string $p2): self
100 $this->validatePField($p2);
109 public function setP3(
string $p3): self
111 $this->validatePField($p3);
120 public function setP4(
string $p4): self
122 $this->validatePField($p4);
131 public function setP5(
string $p5): self
133 $this->validatePField($p5);
141 $this->status = $status;
148 $this->userAgent = $userAgent;
155 return $this->
setStatus(self::STATUS_SUCCESS);
160 return $this->
setStatus(self::STATUS_CANCEL);
165 return $this->
setStatus(self::STATUS_ERROR);
170 return $this->
setStatus(self::STATUS_ATTEMPT);
176 private function validatePField(
string $value): void
178 $invalidValue = substr_count($value,
'_') > 1;
181 $this->isInvalid =
true;
184 if ($invalidValue && $this->isDevMode())
186 throw new ArgumentException(
'Value for p{1-5} field must contain a single underscore.');
193 private function validateRequiredFields(): void
195 if (empty($this->event) || empty($this->tool) || empty($this->category))
197 $this->isInvalid =
true;
199 if ($this->isDevMode())
201 throw new ArgumentException(
'Event, tool and category fields are required and should be filled.');
206 private function isDevMode(): bool
208 $exceptionHandling = Configuration::getValue(
'exception_handling');
210 return !empty($exceptionHandling[
'debug']);
216 'event' => $this->event,
217 'tool' => $this->tool,
218 'category' => $this->category,
219 'section' => $this->section ??
null,
220 'subSection' => $this->subSection ??
null,
221 'element' => $this->element ??
null,
222 'type' => $this->type ??
null,
223 'p1' => $this->p1 ??
null,
224 'p2' => $this->p2 ??
null,
225 'p3' => $this->p3 ??
null,
226 'p4' => $this->p4 ??
null,
227 'p5' => $this->p5 ??
null,
228 'status' => $this->status ??
null,
229 'userAgent' => $this->userAgent ??
null,
238 if (!\defined(
'ANALYTICS_V2_FILENAME') || !is_writable(ANALYTICS_V2_FILENAME))
243 $this->validateRequiredFields();
244 if ($this->isInvalid)
249 if (!isset($this->userId) && !\defined(
'BX_CHECK_AGENT_START') && (
$GLOBALS[
'USER'] instanceof \CUser))
251 $this->userId = (int)
$GLOBALS[
'USER']->getId();
254 $data = $this->buildLogData();
255 $jsonData = Json::encode($data, JSON_UNESCAPED_UNICODE);
257 $fp = @fopen(ANALYTICS_V2_FILENAME,
"ab");
258 if ($fp && flock($fp, LOCK_EX))
260 @fwrite($fp, $jsonData . PHP_EOL);
262 @flock($fp, LOCK_UN);
267 private function buildLogData(): array
270 'date' => date(
'Y-m-d H:i:s'),
272 'dbname' => \defined(
'BX24_DB_NAME') ? BX24_DB_NAME :
null,
273 'userId' => $this->userId ?? 0,
279 $data[
'license'] = \CBitrix24::getLicenseType();
setUserAgent(string $userAgent)
setElement(string $element)
setSubSection(string $subSection)
setStatus(string $status)
setSection(string $section)
__construct(private string $event, private string $tool, private string $category,)
static includeModule($moduleName)
$GLOBALS['____1444769544']