Skip to content

Commit

Permalink
Обновления трэйтов
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashagm committed Jul 10, 2023
1 parent 40223da commit 44c7555
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"require": {
"php": "^8.0",
"laravel/framework": "^10.0",
"guzzlehttp/guzzle": "^7.5",
"guzzlehttp/guzzle": "^7.7",
"jenssegers/agent": "^2.6"
},
"require-dev": {
"phpunit/phpunit": "^10.1"
"phpunit/phpunit": "^10.3"
},
"autoload": {
"psr-4": {
Expand Down
38 changes: 27 additions & 11 deletions src/Traits/UniqueViewsCounterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,55 @@ public function run($request)
{
$isEnabled = config('analytics.enabled', true);
$cookieLifetime = config('analytics.cookie_lifetime', 60 * 24 * 30); // 30 дней
$savePeriod = config('analytics.save_period', 60 * 24 * 7); // 7 дней
$ip = $request->ip();

if ($isEnabled) {
$routeName = $request->route()->getName();

if ($routeName && !str_starts_with($routeName, 'admin.')) {
$views = Cookie::get('views') ? json_decode(Cookie::get('views'), true) : [];
$views = $this->getViewsFromCookie();

if (!in_array($routeName, $views)) {
$views[] = $routeName;
Cookie::queue('views', json_encode($views), $cookieLifetime);

// Check if the visitor with the same IP and route already exists
$existingVisitor = Visitor::where('ip_address', $ip)
->where('route', $request->path())
->first();
$this->storeViewsInCookie($views, $cookieLifetime);

if (!$existingVisitor) {
if (!$this->isVisitorExists($ip, $request->path())) {
$this->createVisitorLog($routeName, $request->path(), $ip);
}
}
}

// сохраняем статистику раз в неделю
if (Carbon::now()->minute % ($savePeriod * 60) == 0) {
if ($this->isSavePeriod()) {
$this->createStatisticLog($views);
}
}

return $request;
}

protected function getViewsFromCookie()
{
return Cookie::get('views') ? json_decode(Cookie::get('views'), true) : [];
}

protected function storeViewsInCookie($views, $lifetime)
{
Cookie::queue('views', json_encode($views), $lifetime);
}

protected function isVisitorExists($ip, $routePath)
{
return Visitor::where('ip_address', $ip)
->where('route', $routePath)
->exists();
}

protected function isSavePeriod()
{
$savePeriod = config('analytics.save_period', 60 * 24 * 7); // 7 дней
return Carbon::now()->minute % ($savePeriod * 60) == 0;
}

protected function createVisitorLog($routeName, $routePath, $ip)
{
Visitor::create([
Expand Down
13 changes: 8 additions & 5 deletions src/Traits/UniqueVisitorsCounterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ public function run($request)
Cookie::queue('visitor_id', $visitorId, $cookieLifetime);

// Проверяем, существует ли уже запись с таким же IP-адресом и значением пользователя
$existingVisitor = Visitor::where('category', $user)
->where('ip_address', $ip)
->first();

if (!$existingVisitor) {
if (!$this->isVisitorExists($user, $ip)) {
$this->createVisitorLog($user, $visitorId, $ip);
}
}
Expand All @@ -54,6 +50,13 @@ public function run($request)
return $request;
}

protected function isVisitorExists($user, $ip)
{
return Visitor::where('category', $user)
->where('ip_address', $ip)
->exists();
}

protected function createVisitorLog($user, $visitorId, $ip)
{
Visitor::create([
Expand Down

0 comments on commit 44c7555

Please sign in to comment.