Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
userprocessor.php
1<?php
2
4
16
18{
19 use CommandTrait;
20
21 private int $userId;
22 private ?Collector\UserCollector $userCollector = null;
23 private ?Collector\SonetLogCollector $sonetLogCollector = null;
24
25 private array|null $userSonetLogs = null;
26
27 private static array $instances = [];
28
29 public static function getInstance(int $userId)
30 {
31 if (!array_key_exists($userId, self::$instances))
32 {
33 self::$instances[$userId] = new self($userId);
34 }
35
36 return self::$instances[$userId];
37 }
38
39 private function __construct(int $userId)
40 {
41 $this->userId = $userId;
42 }
43
44 public function readAll(int $groupId = 0): void
45 {
46 $types = [
49 ];
50
51 $groupsToReset = $groupId === 0
52 ? []
53 : [$groupId];
54
55 $state = Counter\State\Factory::getState($this->userId);
56 // mark everything as 'viewed'
57 foreach ($state as $row)
58 {
59 $stateGroupId = (int)$row['GROUP_ID'];
60 $stateLogId = (int)$row['SONET_LOG_ID'];
61
62 if ($stateGroupId !== $groupId || $stateLogId === 0)
63 {
64 continue;
65 }
66
67 $logItem = Log::getById($row['SONET_LOG_ID']);
68 if ($logItem === false)
69 {
70 continue;
71 }
72
73 $typeId = $logItem->getFields()['RATING_TYPE_ID'] ?? null;
74 $entityId = $logItem->getFields()['RATING_ENTITY_ID'] ?? null;
75
76 if (!$typeId || !$entityId)
77 {
78 continue;
79 }
80
82 'userId' => $this->userId,
83 'typeId' => $typeId,
84 'entityId' => $entityId,
85 'logId' => $row['SONET_LOG_ID'],
86 'save' => true
87 ]);
88 }
89
90 self::reset($this->userId, $types, [], $groupsToReset);
91 Counter\State\Factory::reloadState($this->userId);
92 Service::addEvent(EventDictionary::EVENT_SPACE_LIVEFEED_COUNTER_UPD,[
93 'USER_ID' => $this->userId,
94 ]);
95 }
96
97 public function recountAll(string $counter): void
98 {
99 if (!in_array($counter, [CounterDictionary::COUNTER_NEW_POSTS]))
100 {
101 throw new UnknownCounterException();
102 }
103
104 $total = $this->getSonetLogCollector()->fetchTotal();
106
107 foreach ((new Collector\Paginator($total, $limit)) as $offset)
108 {
109 $rows = $this->getSonetLogCollector()->fetch($limit, $offset);
110 $this->addToQueue($counter, $rows);
111 }
112
113 (new Agent())->addAgent();
114 }
115
116 public function recount(string $counter, array $logIds = []): void
117 {
119 {
120 throw new UnknownCounterException();
121 }
122
123 $counters = $this->getUserCollector()->recount($counter, $logIds);
124
125 $counterTypes = [$counter];
126 self::reset($this->userId, $counterTypes, $logIds);
127 $this->batchInsert($counters);
128
129 Counter\State\Factory::getState($this->userId)->updateState($counters, $counterTypes, $logIds);
130 Service::addEvent(EventDictionary::EVENT_SPACE_LIVEFEED_COUNTER_UPD,[
131 'USER_ID' => $this->userId,
132 ]);
133 }
134
135 private function addToQueue(string $counter, array $logIds): void
136 {
137 Queue::getInstance()->add($this->userId, $counter, $logIds);
138 }
139
140 private function getUserCollector(): Collector\UserCollector
141 {
142 if (!$this->userCollector)
143 {
144 $this->userCollector = Collector\UserCollector::getInstance($this->userId);
145 }
146 return $this->userCollector;
147 }
148
149 private function getSonetLogCollector(): Collector\SonetLogCollector
150 {
151 if (!$this->sonetLogCollector)
152 {
153 $this->sonetLogCollector = Collector\SonetLogCollector::getInstance($this->userId);
154 }
155 return $this->sonetLogCollector;
156 }
157}