Skip to content

Commit a9f4f92

Browse files
authored
Merge pull request #31 from binafy/remove-agent-package
[1.x] Remove Agent package
2 parents 053b148 + 1185078 commit a9f4f92

File tree

6 files changed

+100
-35
lines changed

6 files changed

+100
-35
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
},
4747
"require": {
4848
"php": "^8.0",
49-
"laravel/framework": "^9.0|^10.0",
50-
"jenssegers/agent": "^2.6.4"
49+
"laravel/framework": "^9.0|^10.0"
5150
},
5251
"require-dev": {
5352
"pestphp/pest-plugin-laravel": "^1.4.0|^2.0.0",

src/Middlewares/VisitMonitoringMiddleware.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Binafy\LaravelUserMonitoring\Middlewares;
44

5+
use Binafy\LaravelUserMonitoring\Utills\Detector;
56
use Closure;
67
use Illuminate\Http\Request;
78
use Illuminate\Support\Facades\DB;
8-
use Jenssegers\Agent\Agent;
99

1010
class VisitMonitoringMiddleware
1111
{
@@ -21,17 +21,17 @@ public function handle(Request $request, Closure $next): mixed
2121
return $next($request);
2222
}
2323

24-
$agent = new Agent();
24+
$detector = new Detector();
2525
$guard = config('user-monitoring.user.guard', 'web');
2626
$exceptPages = config('user-monitoring.visit_monitoring.except_pages', []);
2727

2828
if (empty($exceptPages) || !$this->checkIsExceptPages($request->path(), $exceptPages)) {
2929
// Store visit
3030
DB::table(config('user-monitoring.visit_monitoring.table'))->insert([
3131
'user_id' => auth($guard)->id(),
32-
'browser_name' => $agent->browser(),
33-
'platform' => $agent->platform(),
34-
'device' => $agent->device(),
32+
'browser_name' => $detector->getBrowser(),
33+
'platform' => $detector->getBrowser(),
34+
'device' => $detector->getDevice(),
3535
'ip' => $request->ip(),
3636
'page' => $request->url(),
3737
'created_at' => now(),

src/Providers/LaravelUserMonitoringEventServiceProvider.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,53 @@
22

33
namespace Binafy\LaravelUserMonitoring\Providers;
44

5+
use Binafy\LaravelUserMonitoring\Utills\Detector;
56
use Illuminate\Auth\Events\Login;
67
use Illuminate\Auth\Events\Logout;
78
use Illuminate\Foundation\Support\Providers\EventServiceProvider;
89
use Illuminate\Support\Facades\DB;
910
use Illuminate\Support\Facades\Event;
10-
use Jenssegers\Agent\Agent;
1111

1212
class LaravelUserMonitoringEventServiceProvider extends EventServiceProvider
1313
{
14-
public function boot()
14+
public function boot(): void
1515
{
16-
$agent = new Agent();
16+
$detector = new Detector();
1717
$guard = config('user-monitoring.user.guard');
1818
$table = config('user-monitoring.authentication_monitoring.table');
1919

2020
// Login Event
2121
if (config('user-monitoring.authentication_monitoring.on_login', false)) {
22-
Event::listen(function (Login $event) use ($agent, $guard, $table) {
22+
Event::listen(function (Login $event) use ($detector, $guard, $table) {
2323
DB::table($table)
2424
->insert(
25-
$this->insertData($guard, $agent, 'login'),
25+
$this->insertData($guard, $detector, 'login'),
2626
);
2727
});
2828
}
2929

3030
// Logout Event
3131
if (config('user-monitoring.authentication_monitoring.on_logout', false)) {
32-
Event::listen(function (Logout $event) use ($agent, $guard, $table) {
32+
Event::listen(function (Logout $event) use ($detector, $guard, $table) {
3333
DB::table($table)
3434
->insert(
35-
$this->insertData($guard, $agent, 'logout'),
35+
$this->insertData($guard, $detector, 'logout'),
3636
);
3737
});
3838
}
3939
}
4040

4141
/**
42-
* Insert data.
43-
*
44-
* @param string $guard
45-
* @param Agent $agent
46-
* @param string $actionType
47-
* @return array
42+
* Get insert data.
4843
*/
49-
private function insertData(string $guard, Agent $agent, string $actionType): array
44+
private function insertData(string $guard, Detector $detector, string $actionType): array
5045
{
5146
return [
5247
'user_id' => auth($guard)->id(),
5348
'action_type' => $actionType,
54-
'browser_name' => $agent->browser(),
55-
'platform' => $agent->platform(),
56-
'device' => $agent->device(),
49+
'browser_name' => $detector->getBrowser(),
50+
'platform' => $detector->getBrowser(),
51+
'device' => $detector->getDevice(),
5752
'ip' => request()->ip(),
5853
'page' => request()->url(),
5954
'created_at' => now(),

src/Traits/Actionable.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Binafy\LaravelUserMonitoring\Traits;
44

55
use Binafy\LaravelUserMonitoring\Utills\ActionType;
6+
use Binafy\LaravelUserMonitoring\Utills\Detector;
67
use Illuminate\Support\Facades\DB;
7-
use Jenssegers\Agent\Agent;
88

99
trait Actionable
1010
{
@@ -60,27 +60,23 @@ protected static function boot(): void
6060

6161
/**
6262
* Insert action monitoring into DB.
63-
*
64-
* @param mixed $model
65-
* @param string $actionType
66-
* @return void
6763
*/
6864
private static function insertActionMonitoring(mixed $model, string $actionType): void
6965
{
70-
$agent = new Agent();
66+
$detector = new Detector();
7167
$guard = config('user-monitoring.user.guard');
7268

7369
DB::table(config('user-monitoring.action_monitoring.table'))->insert([
7470
'user_id' => auth($guard)->id(),
7571
'action_type' => $actionType,
7672
'table_name' => $model->getTable(),
77-
'browser_name' => $agent->browser(),
78-
'platform' => $agent->platform(),
79-
'device' => $agent->device(),
73+
'browser_name' => $detector->getBrowser(),
74+
'platform' => $detector->getBrowser(),
75+
'device' => $detector->getDevice(),
8076
'ip' => request()->ip(),
8177
'page' => request()->url(),
8278
'created_at' => now(),
8379
'updated_at' => now(),
8480
]);
8581
}
86-
}
82+
}

src/Utills/Detector.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Binafy\LaravelUserMonitoring\Utills;
4+
5+
class Detector
6+
{
7+
/**
8+
* An array of browser names.
9+
*
10+
* @var array
11+
*/
12+
public array $browserName = [
13+
'Edge' => 'Edge',
14+
'MSIE' => 'Internet Explorer',
15+
'Trident' => 'Internet Explorer',
16+
'Firefox' => 'Firefox',
17+
'OPR' => 'Opera',
18+
'Chrome' => 'Chrome',
19+
'Safari' => 'Safari',
20+
'Opera' => 'Opera',
21+
];
22+
23+
/**
24+
* An array of device names.
25+
*
26+
* @var array
27+
*/
28+
public array $deviceName = [
29+
'/iPhone/i' => 'iPhone',
30+
'/iPad/i' => 'iPad',
31+
'/Android/i' => 'Android Device',
32+
'/Windows/i' => 'Windows',
33+
];
34+
35+
/**
36+
* Get browser name.
37+
*/
38+
public function getBrowser(): string
39+
{
40+
if (PHP_SAPI === 'cli') {
41+
return 'CLI';
42+
}
43+
44+
$userAgent = $_SERVER['HTTP_USER_AGENT'];
45+
46+
foreach ($this->browserName as $key => $browser) {
47+
if (str_contains($userAgent, $key)) {
48+
return $browser;
49+
}
50+
}
51+
52+
return 'Unknown Browser';
53+
}
54+
55+
/**
56+
* Get device name.
57+
*/
58+
public function getDevice(): string
59+
{
60+
if (PHP_SAPI === 'cli') {
61+
return 'CLI';
62+
}
63+
64+
$userAgent = $_SERVER['HTTP_USER_AGENT'];
65+
66+
foreach ($this->deviceName as $pattern => $name) {
67+
if (preg_match($pattern, $userAgent)) {
68+
return $name;
69+
}
70+
}
71+
72+
return 'Unknown Device Name';
73+
}
74+
}

tests/Feature/VisitMonitoringTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@
6262
// Ajax
6363

6464
test('visit monitoring store ajax requests', function () {
65+
\Pest\Laravel\withoutExceptionHandling();
6566
get('/', ['X-Requested-With' => 'XMLHttpRequest']);
6667

6768
// DB Assertions
6869
assertDatabaseCount(config('user-monitoring.visit_monitoring.table'), 1);
69-
assertDatabaseHas(config('user-monitoring.visit_monitoring.table'), ['created_at' => now()]);
70+
assertDatabaseHas(config('user-monitoring.visit_monitoring.table'), ['id' => 1]);
7071
});
7172

7273
test('visit monitoring skip store when ajax mode is off for ajax requests', function () {

0 commit comments

Comments
 (0)