Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
pushservice.php
1<?php
2
4
7
15{
16 public const MODULE_NAME = 'socialnetwork';
17
18 private static $instance;
19 private static $isJobOn = false;
20
21 private $registry = [];
22
26 private function __construct()
27 {
28
29 }
30
34 public static function getInstance(): PushService
35 {
36 if (!self::$instance)
37 {
38 self::$instance = new self();
39 }
40
41 return self::$instance;
42 }
43
48 public static function addEvent($recipients, array $params): void
49 {
50 $parameters = [
51 'RECIPIENTS' => $recipients,
52 'PARAMS' => $params,
53 ];
54 self::getInstance()->registerEvent($parameters);
55 self::getInstance()->addBackgroundJob();
56 }
57
58 public static function addEventByTag(string $tag, array $params): void
59 {
60 $parameters = [
61 'TAG' => $tag,
62 'PARAMS' => $params,
63 ];
64 self::getInstance()->registerEvent($parameters);
65 self::getInstance()->addBackgroundJob();
66 }
67
71 public static function proceed(): void
72 {
73 if (!Main\Loader::includeModule('pull'))
74 {
75 return;
76 }
77
78 self::getInstance()->sendEvents();
79 }
80
81 private function addBackgroundJob(): void
82 {
83 if (!self::$isJobOn)
84 {
85 $application = Main\Application::getInstance();
86 $application && $application->addBackgroundJob([__CLASS__, 'proceed'], [], 0);
87
88 self::$isJobOn = true;
89 }
90 }
91
95 private function registerEvent(array $parameters): void
96 {
97 $this->registry[] = [
98 'TAG' => $parameters['TAG'] ?? '',
99 'RECIPIENTS' => $parameters['RECIPIENTS'],
100 'PARAMS' => $parameters['PARAMS'],
101 ];
102 }
103
104 private function sendEvents(): void
105 {
106 foreach ($this->registry as $event)
107 {
108 if (isset($event['TAG']) && $event['TAG'] !== '')
109 {
110 \CPullWatch::AddToStack($event['TAG'], $event['PARAMS']);
111 }
112 else
113 {
114 Event::add($event['RECIPIENTS'], $event['PARAMS']);
115 }
116 }
117 }
118
119}
static addEvent($recipients, array $params)
static addEventByTag(string $tag, array $params)