Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
counter.php
1<?php
2
4
14
16{
17 private static array $instance = [];
18
19 public static function getInstance(int $userId): self
20 {
21 if (!array_key_exists($userId, self::$instance))
22 {
23 self::$instance[$userId] = new self(
24 $userId,
25 new ProviderCollection(...[
26 new Tasks\CounterProvider($userId),
27 new Calendar\CounterProvider($userId),
28 new WorkGroup\CounterProvider($userId),
30 ])
31 );
32 }
33
34 return self::$instance[$userId];
35 }
36
44 public function getTotal(int $spaceId = 0): int
45 {
46 $total = 0;
47
48 foreach ($this->providerCollection as $provider)
49 {
50 $total += $provider->getTotal($spaceId);
51 }
52
53 return $total;
54 }
55
63 public function getValue(int $spaceId = 0, array $metrics = []): int
64 {
65 if (empty($metrics))
66 {
67 return $this->getTotal($spaceId);
68 }
69
70 $value = 0;
71
72 foreach ($this->providerCollection as $provider)
73 {
74 $value += $provider->getValue($spaceId, $metrics);
75 }
76
77 return $value;
78 }
79
84 public function getAvailableMetrics(): array
85 {
86 $availableMetrics = [];
87
88 foreach ($this->providerCollection as $provider)
89 {
90 $availableMetrics = array_merge($availableMetrics, $provider->getAvailableMetrics());
91 }
92
93 return $availableMetrics;
94 }
95
107 public function getMemberSpaceCounters(): array
108 {
109 $total = 0;
110 $spacesTotal = $this->getMemberSpacesTotal();
111 $invitationsTotal = $this->getInvitationsTotal();
112 $total += $invitationsTotal;
113 $total += $spacesTotal['total'];
114
115 return [
116 'userId' => $this->userId,
117 'total' => $total,
118 'spaces' => $spacesTotal['spaces'],
119 'invitations' => $invitationsTotal,
120 ];
121 }
122
123 private function __construct(private int $userId, private ProviderCollection $providerCollection)
124 {
125 $this->recount();
126 }
127
133 public function recount()
134 {
135 $memberCounters = $this->getMemberSpaceCounters();
136 $value = $memberCounters['total'];
137
138 if (!$this->isSameValueCached($value))
139 {
140 \CUserCounter::Set(
141 $this->userId,
143 $value,
144 '**',
145 '',
146 true
147 );
148 }
149
150 // push data to the client
151 (new PushSender())->createPush(
152 [$this->userId],
154 $memberCounters
155 );
156 }
157
158 private function isSameValueCached(int $value): bool
159 {
160 global $CACHE_MANAGER;
161
162 $cache = $CACHE_MANAGER->Get('user_counter' . $this->userId);
163 if (!$cache)
164 {
165 return false;
166 }
167
168 foreach ($cache as $item)
169 {
170 if (
171 $item['CODE'] === Dictionary::LEFT_MENU_SPACES
172 && $item['SITE_ID'] === '**'
173 && (int)$item['CNT'] === $value
174 )
175 {
176 return true;
177 }
178 }
179
180 return false;
181 }
182
183 private function getUserSpaces(int $userId): array
184 {
185 $userSpaces = (new Builder($userId))
186 ->addModeFilter(\Bitrix\Socialnetwork\Space\List\Dictionary::FILTER_MODES['my'])
187 ->build()
188 ->exec()
189 ->fetchAll();
190
191 // append common space
192 $userSpaces[] = ['ID' => 0];
193
194 return $userSpaces;
195 }
196
197 private function getMemberSpacesTotal(): array
198 {
199 $total = 0;
200 $spaces = [];
201
202 foreach ($this->getUserSpaces($this->userId) as $space)
203 {
204 $spaceId = (int)$space['ID'];
205 $spaceTotal = $this->getTotal($spaceId);
206 $total += $spaceTotal;
207
208 $spaceCounters = [
209 'id' => $spaceId,
210 'total' => $spaceTotal,
211 ];
212
213 foreach ($this->getAvailableMetrics() as $metric)
214 {
215 $spaceCounters['metrics'][$metric] = $this->getValue($spaceId, [$metric]);
216 }
217
218 $spaces[] = $spaceCounters;
219 }
220
221 return [
222 'total' => $total,
223 'spaces' => $spaces,
224 ];
225 }
226
227 private function getInvitationsTotal(): int
228 {
229 $total = 0;
230
231 $invitations = (new InvitationManager($this->userId))->getInvitations()->toArray();
232 foreach ($invitations as $invitation)
233 {
234 $total += $this->getValue($invitation->getSpaceId(), [Dictionary::COUNTERS_WORKGROUP_REQUEST_OUT]);
235 }
236
237 return $total;
238 }
239}
getValue(int $spaceId=0, array $metrics=[])
Definition counter.php:63