Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
counterstate.php
1<?php
2
4
7
8abstract class CounterState implements \Iterator
9{
10 private Counter\Loader $loader;
11
12 protected int $userId;
13 protected array $counters = [];
14
15 protected function __construct(int $userId, Counter\Loader $loader)
16 {
17 $this->userId = $userId;
18 $this->loader = $loader;
19 $this->init();
20 }
21
22 abstract public function rewind(): void;
23
24 #[\ReturnTypeWillChange]
25 abstract public function current();
26
27 #[\ReturnTypeWillChange]
28 abstract public function key();
29
30 abstract public function next(): void;
31
32 abstract public function valid(): bool;
33
34 abstract public function getSize(): int;
35
36 abstract protected function loadCounters(): void;
37
38 abstract public function updateState(array $rawCounters, array $types = [], array $sonetLogIds = []): void;
39
40 public function init(): void
41 {
42 $this->counters = $this->getCountersEmptyState();
43 $this->loadCounters();
44 }
45
46 public function getLoader(): Counter\Loader
47 {
48 return $this->loader;
49 }
50
51 public function isCounted(): bool
52 {
53 return $this->loader->isCounted();
54 }
55
56 public function getClearedDate(): int
57 {
58 return $this->loader->getClearedDate();
59 }
60
61 public function resetCache(): void
62 {
63 $this->loader->resetCache();
64 }
65
66 public function getRawCounters(string $meta = CounterDictionary::META_PROP_ALL): array
67 {
68 return $this->counters[$meta] ?? [];
69 }
70
71 public function getValue(string $name, int $groupId = null): int
72 {
74
75 if ($groupId > 0)
76 {
77 if (
78 !array_key_exists($name, $counters)
79 || !array_key_exists($groupId, $counters[$name])
80 )
81 {
82 return 0;
83 }
84
85 return $counters[$name][$groupId];
86 }
87
88 if (!array_key_exists($name, $counters))
89 {
90 return 0;
91 }
92
93 return array_sum($counters[$name]);
94 }
95
100 protected function updateRawCounters(): void
101 {
102 $this->counters = $this->getCountersEmptyState();
103
104 $user = UserRegistry::getInstance($this->userId);
105 $groups = $user->getUserGroups(UserRegistry::MODE_GROUP);
106
107 $tmpHeap[] = [];
108 foreach ($this as $item)
109 {
110 if ($this->getLoader()->isCounterFlag($item['TYPE']))
111 {
112 continue;
113 }
114
115 $logId = $item['SONET_LOG_ID'];
116 $groupId = $item['GROUP_ID'];
117 $value = $item['VALUE'];
118 $type = $item['TYPE'];
119
120 $meta = $this->getMetaProp($item, $groups);
121 $subType = $this->getItemSubType($type);
122
123 if (!isset($this->counters[$meta][$type][$groupId]))
124 {
125 $this->counters[$meta][$type][$groupId] = 0;
126 }
127 if (!isset($this->counters[$meta][$subType][$groupId]))
128 {
129 $this->counters[$meta][$subType][$groupId] = 0;
130 }
131 if (!isset($tmpHeap[$meta][$subType][$groupId]))
132 {
133 $tmpHeap[$meta][$subType][$groupId] = [];
134 }
135 if (!isset($this->counters[CounterDictionary::META_PROP_SONET][$type][$groupId]))
136 {
137 $this->counters[CounterDictionary::META_PROP_SONET][$type][$groupId] = 0;
138 }
139 if (!isset($this->counters[CounterDictionary::META_PROP_SONET][$subType][$groupId]))
140 {
141 $this->counters[CounterDictionary::META_PROP_SONET][$subType][$groupId] = 0;
142 }
143 if (!isset($this->counters[CounterDictionary::META_PROP_ALL][$type][$groupId]))
144 {
145 $this->counters[CounterDictionary::META_PROP_ALL][$type][$groupId] = 0;
146 }
147 if (!isset($this->counters[CounterDictionary::META_PROP_ALL][$subType][$groupId]))
148 {
149 $this->counters[CounterDictionary::META_PROP_ALL][$subType][$groupId] = 0;
150 }
151
152 if (!isset($tmpHeap[$meta][$type][$groupId][$logId]))
153 {
154 $tmpHeap[$meta][$type][$groupId][$logId] = $value;
155 $this->counters[$meta][$type][$groupId] += $value;
156
157 if (in_array($meta, [CounterDictionary::META_PROP_GROUP]))
158 {
159 $this->counters[CounterDictionary::META_PROP_SONET][$type][$groupId] += $value;
160 }
161 }
162
163 if (
164 $type !== $subType
165 && !isset($tmpHeap[$meta][$subType][$groupId][$logId])
166 )
167 {
168 $tmpHeap[$meta][$subType][$groupId][$logId] = $value;
169 $this->counters[$meta][$subType][$groupId] += $value;
170
171 if (in_array($meta, [CounterDictionary::META_PROP_GROUP]))
172 {
173 $this->counters[CounterDictionary::META_PROP_SONET][$subType][$groupId] += $value;
174 }
175 }
176
177 if (!isset($tmpHeap[CounterDictionary::META_PROP_ALL][$type][$groupId][$logId]))
178 {
179 $tmpHeap[CounterDictionary::META_PROP_ALL][$type][$groupId][$logId] = $value;
180 $this->counters[CounterDictionary::META_PROP_ALL][$type][$groupId] += $value;
181 }
182
183 if (
184 $type !== $subType
185 && !isset($tmpHeap[CounterDictionary::META_PROP_ALL][$subType][$groupId][$logId])
186 )
187 {
188 $tmpHeap[CounterDictionary::META_PROP_ALL][$subType][$groupId][$logId] = $value;
189 $this->counters[CounterDictionary::META_PROP_ALL][$subType][$groupId] += $value;
190 }
191 }
192
193 unset($tmpHeap);
194 }
195
196 private function getCountersEmptyState(): array
197 {
198 return [
202 ];
203 }
204
205 private function getItemSubType(string $type): string
206 {
207 if (in_array($type, [CounterDictionary::COUNTER_NEW_COMMENTS]))
208 {
210 }
211
212 if (in_array($type, [CounterDictionary::COUNTER_NEW_POSTS]))
213 {
215 }
216
217 return $type;
218 }
219
220 private function getMetaProp(array $item, array $groups): string
221 {
222 if (array_key_exists($item['GROUP_ID'], $groups))
223 {
225 }
226
228 }
229}
updateState(array $rawCounters, array $types=[], array $sonetLogIds=[])
getRawCounters(string $meta=CounterDictionary::META_PROP_ALL)