• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

visavi / rotor / 26723576697

31 May 2026 08:27PM UTC coverage: 14.172% (-0.4%) from 14.618%
26723576697

push

github

visavi
Перенес файлы обновлений бд в отдельную папку

785 of 5539 relevant lines covered (14.17%)

1.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

27.88
/app/Classes/Metrika.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\Classes;
6

7
use App\Models\Counter;
8
use App\Models\Counter24;
9
use App\Models\Counter31;
10
use App\Models\Online;
11
use Exception;
12
use Illuminate\Support\Facades\Cache;
13
use Illuminate\Support\Facades\DB;
14
use PDOException;
15

16
class Metrika
17
{
18
    /**
19
     * Закэшированный счетчик в пределах жизни инстанса
20
     */
21
    private ?Counter $resultCounter = null;
22

23
    /**
24
     * Генерирует счетчик
25
     */
26
    public function getCounter(int $online): void
3✔
27
    {
28
        $lockPath = public_path('uploads/counters/counter.lock');
3✔
29
        $lock = fopen($lockPath, 'cb');
3✔
30

31
        if (! $lock || ! flock($lock, LOCK_EX | LOCK_NB)) {
3✔
32
            if ($lock) {
×
33
                fclose($lock);
×
34
            }
35

36
            return;
×
37
        }
38

39
        try {
40
            $counter = $this->getResultCounter();
3✔
41

42
            if (! $counter) {
3✔
43
                $counter = (object) ['dayhosts' => 0, 'dayhits' => 0];
×
44
            }
45

46
            $font = public_path('assets/fonts/font.ttf');
3✔
47
            $template = public_path('assets/img/images/counter.png');
3✔
48

49
            if (! function_exists('imagecreatefrompng') || ! is_file($template) || ! is_file($font)) {
3✔
50
                return;
×
51
            }
52

53
            $img = @imagecreatefrompng($template);
3✔
54
            if ($img === false) {
3✔
55
                return;
×
56
            }
57

58
            $color = imagecolorallocate($img, 62, 62, 62);
3✔
59

60
            $onlineStr = $online >= 1000
3✔
61
                ? round($online / 1000, 1) . 'K'
×
62
                : (string) $online;
3✔
63

64
            $bbox = imagettfbbox(12, 0, $font, $onlineStr);
3✔
65
            $pos = 78 - abs($bbox[2] - $bbox[0]);
3✔
66

67
            imagettftext($img, 6, 0, 14, 7, $color, $font, (string) formatShortNum($counter->dayhosts));
3✔
68
            imagettftext($img, 6, 0, 14, 13, $color, $font, (string) formatShortNum($counter->dayhits));
3✔
69
            imagettftext($img, 12, 0, $pos, 13, $color, $font, $onlineStr);
3✔
70

71
            imagepng($img, public_path('uploads/counters/counter_new.png'));
3✔
72
            imagedestroy($img);
3✔
73

74
            try {
75
                rename(
3✔
76
                    public_path('uploads/counters/counter_new.png'),
3✔
77
                    public_path('uploads/counters/counter.png')
3✔
78
                );
3✔
79
            } catch (Exception) {
×
80
                // nothing
81
            }
82
        } finally {
83
            flock($lock, LOCK_UN);
3✔
84
            fclose($lock);
3✔
85
        }
86
    }
87

88
    /**
89
     * Сохраняет статистику
90
     */
91
    public function saveStatistic(): void
×
92
    {
93
        session()->increment('hits');
×
94

95
        if (session('online') > SITETIME) {
×
96
            return;
×
97
        }
98

99
        $period = date('Y-m-d H:00:00', SITETIME);
×
100
        $day = date('Y-m-d 00:00:00', SITETIME);
×
101

102
        // Чистка устаревших онлайн раз в 30с на весь сайт, а не на каждую сессию
103
        if (Cache::add('online_cleanup', true, 30)) {
×
104
            Online::query()->where('updated_at', '<', SITETIME - setting('timeonline'))->delete();
×
105
        }
106

107
        $user = getUser();
×
108
        $ip = getIp();
×
109
        $brow = getBrowser();
×
110
        $uid = md5($ip . $brow);
×
111

112
        if ($user) {
×
113
            $user->update(['updated_at' => SITETIME]);
×
114
        }
115

116
        try {
117
            $online = Online::query()
×
118
                ->where('uid', $uid)
×
119
                ->updateOrCreate([], [
×
120
                    'uid'        => $uid,
×
121
                    'ip'         => $ip,
×
122
                    'brow'       => $brow,
×
123
                    'updated_at' => SITETIME,
×
124
                    'user_id'    => $user->id ?? null,
×
125
                ]);
×
126
            $newHost = $online->wasRecentlyCreated;
×
127
        } catch (PDOException) {
×
128
            $newHost = false;
×
129
        }
130

131
        // -----------------------------------------------------------//
132
        $counter = $this->getResultCounter();
×
133
        if (! $counter) {
×
134
            return;
×
135
        }
136

137
        if (date('Y-m-d 00:00:00', strtotime($counter->period)) !== $day) {
×
138
            Counter31::query()->insertOrIgnore([
×
139
                'period' => $period,
×
140
                'hosts'  => $counter->dayhosts,
×
141
                'hits'   => $counter->dayhits,
×
142
            ]);
×
143

144
            $counter->update([
×
145
                'dayhosts' => 0,
×
146
                'dayhits'  => 0,
×
147
            ]);
×
148
        }
149

150
        if ($counter->period !== $period) {
×
151
            Counter24::query()->insertOrIgnore([
×
152
                'period' => $period,
×
153
                'hosts'  => $counter->hosts24,
×
154
                'hits'   => $counter->hits24,
×
155
            ]);
×
156

157
            $counter->update([
×
158
                'period'  => $period,
×
159
                'hosts24' => 0,
×
160
                'hits24'  => 0,
×
161
            ]);
×
162
        }
163

164
        // -----------------------------------------------------------//
165
        $hostsUpdate = [];
×
166
        if ($newHost) {
×
167
            $hostsUpdate = [
×
168
                'allhosts' => DB::raw('allhosts + 1'),
×
169
                'dayhosts' => DB::raw('dayhosts + 1'),
×
170
                'hosts24'  => DB::raw('hosts24 + 1'),
×
171
            ];
×
172
        }
173

174
        $hits = (int) session('hits', 1);
×
175

176
        $hitsUpdate = [
×
177
            'allhits' => DB::raw('allhits + ' . $hits),
×
178
            'dayhits' => DB::raw('dayhits + ' . $hits),
×
179
            'hits24'  => DB::raw('hits24 + ' . $hits),
×
180
        ];
×
181

182
        $counter->update(array_merge($hostsUpdate, $hitsUpdate));
×
183
        $counter->refresh();
×
184

185
        session(['hits' => 0]);
×
186
        session(['online' => strtotime('+30 seconds', SITETIME)]);
×
187
    }
188

189
    /**
190
     * Returns counter result
191
     */
192
    private function getResultCounter(): ?Counter
3✔
193
    {
194
        return $this->resultCounter ??= Counter::query()->first();
3✔
195
    }
196
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc