Skip to content

Commit 892ed8d

Browse files
committed
Update VisitMonitoringMiddleware.php
1 parent 65af246 commit 892ed8d

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/Middlewares/VisitMonitoringMiddleware.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Binafy\LaravelUserMonitoring\Middlewares;
44

5+
use Binafy\LaravelUserMonitoring\Contracts\MonitoringCondition;
56
use Binafy\LaravelUserMonitoring\Utills\Detector;
67
use Binafy\LaravelUserMonitoring\Utills\UserUtils;
78
use Closure;
@@ -25,6 +26,11 @@ public function handle(Request $request, Closure $next): mixed
2526
return $next($request);
2627
}
2728

29+
// Custom conditions from config
30+
if (! $this->shouldMonitor($request)) {
31+
return $next($request);
32+
}
33+
2834
$detector = new Detector;
2935
$exceptPages = config('user-monitoring.visit_monitoring.except_pages', []);
3036

@@ -53,4 +59,30 @@ protected function checkIsExceptPages(string $page, array $exceptPages): bool
5359
{
5460
return collect($exceptPages)->contains($page);
5561
}
62+
63+
/**
64+
* Determine if monitoring should be performed for the given request and user.
65+
*/
66+
protected function shouldMonitor(Request $request): bool
67+
{
68+
$config = config('user-monitoring.visit_monitoring.conditions', []);
69+
70+
foreach ($config as $condition) {
71+
if (is_callable($condition)) {
72+
if (! $condition($request)) {
73+
return false;
74+
}
75+
} elseif (is_string($condition)) {
76+
$instance = new $condition;
77+
if (! $instance instanceof MonitoringCondition) {
78+
continue;
79+
}
80+
if (! $instance->shouldMonitor($request)) {
81+
return false;
82+
}
83+
}
84+
}
85+
86+
return true;
87+
}
5688
}

0 commit comments

Comments
 (0)