Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
loader.php
1<?php
2
4
8
12class Loader
13{
14 private int $userId;
15 private int $flagCounted = 0;
16 private int $flagCleared = 0;
17 private array $rows;
18
19 private const CACHE_PREFIX = 'sonet_scorer_cache_';
20 private const CACHE_TTL = 10 * 60;
21 private const CACHE_DIR = '/sonet/counterstate';
22
23 private const FLAGS = [
26 ];
27
28 public function __construct(int $userId)
29 {
30 $this->userId = $userId;
31 $this->fetchCounters();
32 }
33
34 public function isCounterFlag(string $type): bool
35 {
36 return in_array($type, self::FLAGS, true);
37 }
38
39 public function getRawCounters(): array
40 {
41 return $this->rows;
42 }
43
44 public function getTotalCounters(): int
45 {
46 return count($this->rows);
47 }
48
49 public function isCounted(): bool
50 {
51 return (bool) $this->flagCounted;
52 }
53
54 public function getClearedDate(): int
55 {
56 return $this->flagCleared;
57 }
58
59 public function resetCache(): void
60 {
61 $cache = Cache::createInstance();
62 $cache->clean($this->getCacheTag(), $this->getCacheDir());
63 }
64
65 private function getCacheDir(): string
66 {
67 return self::CACHE_DIR . '/' . substr(md5($this->userId),2,2) . '/';
68 }
69
70 private function getCacheTag(): string
71 {
72 return self::CACHE_PREFIX . $this->userId;
73 }
74
75 private function fetchCounters(): void
76 {
77 $limit = Counter::getGlobalLimit();
78 if ($limit === 0)
79 {
80 $this->rows = $this->getFlags();
81 return;
82 }
83
84 $query = CounterTable::query()
85 ->setSelect([
86 'VALUE',
87 'SONET_LOG_ID',
88 'GROUP_ID',
89 'TYPE'
90 ])
91 ->where('USER_ID', $this->userId);
92
93 $rowsFlag = null;
94 if (!is_null($limit))
95 {
96 $rowsFlag = $this->getFlags();
97 $query->setLimit($limit);
98 }
99
100 $this->rows = $query->exec()->fetchAll();
101 if (!is_null($rowsFlag))
102 {
103 $this->rows = array_merge($this->rows, $rowsFlag);
104 }
105 }
106
107 private function getFlags(): array
108 {
109 $rows = [];
110 $cache = Cache::createInstance();
111
112 if ($cache->initCache(self::CACHE_TTL, $this->getCacheTag(), $this->getCacheDir()))
113 {
114 $rows = $cache->getVars();
115 }
116 else
117 {
118 $rows = CounterTable::query()
119 ->setSelect([
120 'VALUE',
121 'SONET_LOG_ID',
122 'GROUP_ID',
123 'TYPE'
124 ])
125 ->where('USER_ID', $this->userId)
126 ->whereIn('TYPE', self::FLAGS)
127 ->setLimit(2)
128 ->fetchAll();
129
130 if (!empty($rows))
131 {
132 $taggedCache = Application::getInstance()->getTaggedCache();
133 $taggedCache->StartTagCache($this->getCacheDir());
134 $taggedCache->RegisterTag($this->getCacheTag());
135
136 $cache->startDataCache();
137 $cache->endDataCache($rows);
138 $taggedCache->EndTagCache();
139 }
140 }
141
142 foreach ($rows as $row)
143 {
144 switch ($row['TYPE'])
145 {
147 $this->flagCounted = (int) $row['VALUE'];
148 break;
150 $this->flagCleared = (int) $row['VALUE'];
151 break;
152 }
153 }
154
155 return $rows;
156 }
157}