Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
counter.php
1<?php
2
4
10
12{
13 private const DEFAULT_LIMIT = 4999;
14 private static array $instance = [];
15 private int $userId;
16
17 public static function isReady($userId): bool
18 {
19 return array_key_exists($userId, self::$instance);
20 }
21
22 public static function getGlobalLimit(): int
23 {
24 $limit = \COption::GetOptionString('socialnetwork', 'sonetCounterLimit', '');
25 if ($limit === '')
26 {
27 return self::DEFAULT_LIMIT;
28 }
29 return (int)$limit;
30 }
31
32 public static function getInstance($userId): self
33 {
34 if (!array_key_exists($userId, self::$instance))
35 {
36 self::$instance[$userId] = new self($userId);
37 (new CounterController($userId))->updateInOptionCounter();
38 }
39
40 return self::$instance[$userId];
41 }
42
43 private function __construct($userId)
44 {
45 $this->userId = (int)$userId;
46
47 $state = $this->getState();
48
49 if (
50 $this->userId
51 && !$state->isCounted()
52 )
53 {
54 (new CounterController($this->userId))->recountAll();
55 }
56
57 Service::getInstance();
58
59 //$this->dropOldCounters();
60 }
61
62 public function isCounted(): bool
63 {
64 return $this->getState()->isCounted();
65 }
66
67 public function getRawCounters(string $meta = CounterDictionary::META_PROP_ALL): array
68 {
69 return $this->getState()->getRawCounters($meta);
70 }
71
72 public function getCounters(int $groupId = 0, array $params = []): array
73 {
74 $skipAccessCheck = (isset($params['SKIP_ACCESS_CHECK']) && $params['SKIP_ACCESS_CHECK']);
75
76 if (!$skipAccessCheck && !$this->isAccessToCounters())
77 {
78 return [];
79 }
80
81 return [
82 'total' => [
83 'counter' => $this->get(CounterDictionary::COUNTER_TOTAL, $groupId),
84 'code' => '',
85 ],
86 'new_posts' => [
87 'counter' => $this->get(CounterDictionary::COUNTER_NEW_POSTS, $groupId),
88 'code' => '',
89 ],
90 'new_comments' => [
91 'counter' => $this->get(CounterDictionary::COUNTER_NEW_COMMENTS, $groupId),
92 'code' => '',
93 ],
94 ];
95 }
96
97 public function get($name, int $groupId = 0)
98 {
99 switch ($name)
100 {
102 $value = $this->get(CounterDictionary::COUNTER_NEW_POSTS, $groupId)
103 + $this->get(CounterDictionary::COUNTER_NEW_COMMENTS, $groupId);
104 break;
105 default:
106 $value = $this->getState()->getValue($name, $groupId);
107 break;
108 }
109
110 return $value;
111 }
112
113 private function dropOldCounters(): void
114 {
115 $state = $this->getState();
116
117 if (!$state->isCounted())
118 {
119 return;
120 }
121
122 if (Counter\Queue\Queue::isInQueue($this->userId))
123 {
124 return;
125 }
126
127 if ($state->getClearedDate() >= (int) date('ymd'))
128 {
129 return;
130 }
131
132 Service::addEvent(
133 \Bitrix\Socialnetwork\Internals\EventService\EventDictionary::EVENT_GARBAGE_COLLECT,
134 [
135 'USER_ID' => $this->userId,
136 ]
137 );
138 }
139
140 private function isAccessToCounters(): bool
141 {
142 return $this->userId === \Bitrix\Socialnetwork\Helper\User::getCurrentUserId();
143 }
144
148 private function getState(): CounterState
149 {
150 return Factory::getState($this->userId);
151 }
152}
getCounters(int $groupId=0, array $params=[])
Definition counter.php:72
getRawCounters(string $meta=CounterDictionary::META_PROP_ALL)
Definition counter.php:67