22
33namespace Binafy \LaravelUserMonitoring \Middlewares ;
44
5+ use Binafy \LaravelUserMonitoring \Contracts \MonitoringCondition ;
56use Binafy \LaravelUserMonitoring \Utills \Detector ;
67use Binafy \LaravelUserMonitoring \Utills \UserUtils ;
78use 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