Skip to content

Commit f22ba8e

Browse files
me-shaongithub-actions[bot]
authored andcommitted
Fix styling
1 parent 02ef8d9 commit f22ba8e

12 files changed

Lines changed: 118 additions & 110 deletions

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use Illuminate\Support\Facades\Route;
4-
use MeShaon\RequestAnalytics\Controllers\RequestAnalyticsController;
54
use MeShaon\RequestAnalytics\Controllers\Api\AnalyticsApiController;
5+
use MeShaon\RequestAnalytics\Controllers\RequestAnalyticsController;
66

77
Route::middleware(['web', 'auth', 'request-analytics.access'])
88
->get(config('request-analytics.route.pathname'), [RequestAnalyticsController::class, 'show'])

src/Controllers/Api/AnalyticsApiController.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public function overview(Request $request): JsonResponse
2121
]);
2222

2323
$dateRange = $this->getDateRange($request);
24-
24+
2525
$data = Cache::remember("api_overview_{$dateRange['key']}", now()->addMinutes(5), function () use ($dateRange) {
2626
$query = $this->getBaseQuery($dateRange);
27-
27+
2828
return [
2929
'summary' => $this->getSummary($query),
3030
'chart' => $this->getChartData($query, $dateRange),
@@ -53,7 +53,7 @@ public function visitors(Request $request): JsonResponse
5353

5454
$dateRange = $this->getDateRange($request);
5555
$perPage = $request->input('per_page', 50);
56-
56+
5757
$visitors = $this->getBaseQuery($dateRange)
5858
->select(
5959
'visitor_id',
@@ -85,13 +85,13 @@ public function pageViews(Request $request): JsonResponse
8585

8686
$dateRange = $this->getDateRange($request);
8787
$perPage = $request->input('per_page', 50);
88-
88+
8989
$query = $this->getBaseQuery($dateRange);
90-
90+
9191
if ($path = $request->input('path')) {
9292
$query->where('path', 'like', "%{$path}%");
9393
}
94-
94+
9595
$pageViews = $query
9696
->select('*')
9797
->orderBy('visited_at', 'desc')
@@ -136,7 +136,7 @@ protected function getDateRange(Request $request): array
136136
'start' => $startDate,
137137
'end' => $endDate,
138138
'days' => $days,
139-
'key' => $startDate->format('Y-m-d') . '_' . $endDate->format('Y-m-d'),
139+
'key' => $startDate->format('Y-m-d').'_'.$endDate->format('Y-m-d'),
140140
];
141141
}
142142

@@ -181,15 +181,15 @@ protected function getChartData($query, array $dateRange): array
181181
while ($current <= $dateRange['end']) {
182182
$dateStr = $current->format('Y-m-d');
183183
$labels[] = $current->format('M d');
184-
184+
185185
if ($data->has($dateStr)) {
186186
$views[] = $data->get($dateStr)->views;
187187
$visitors[] = $data->get($dateStr)->visitors;
188188
} else {
189189
$views[] = 0;
190190
$visitors[] = 0;
191191
}
192-
192+
193193
$current->addDay();
194194
}
195195

@@ -265,4 +265,4 @@ protected function getCountries($query): array
265265
->get()
266266
->toArray();
267267
}
268-
}
268+
}

src/Controllers/RequestAnalyticsController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private function getBaseQuery()
4747
private function getCountries(): array
4848
{
4949
$cacheKey = "analytics_countries_{$this->dateRange}";
50-
50+
5151
return Cache::remember($cacheKey, now()->addMinutes(5), function () {
5252
$totalVisitors = $this->getBaseQuery()->count();
5353

@@ -80,7 +80,7 @@ private function getCountries(): array
8080
private function getBrowsers(): array
8181
{
8282
$cacheKey = "analytics_browsers_{$this->dateRange}";
83-
83+
8484
return Cache::remember($cacheKey, now()->addMinutes(5), function () {
8585
$totalVisitors = $this->getBaseQuery()->count();
8686

src/Services/BotDetectionService.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class BotDetectionService
2020
'python-requests', 'python-urllib', 'go-http-client',
2121
'java/', 'apache-httpclient', 'okhttp', 'postman',
2222
'insomnia', 'paw/', 'rest-client', 'ruby/', 'perl/',
23-
'php/', 'node-fetch', 'axios/', 'got/', 'superagent'
23+
'php/', 'node-fetch', 'axios/', 'got/', 'superagent',
2424
];
2525

2626
protected array $botIpRanges = [
@@ -64,9 +64,9 @@ public function isBot(?string $userAgent, ?string $ipAddress = null): bool
6464
}
6565

6666
// Check for missing browser indicators
67-
if (!str_contains($userAgentLower, 'mozilla') &&
68-
!str_contains($userAgentLower, 'opera') &&
69-
!str_contains($userAgentLower, 'webkit')) {
67+
if (! str_contains($userAgentLower, 'mozilla') &&
68+
! str_contains($userAgentLower, 'opera') &&
69+
! str_contains($userAgentLower, 'webkit')) {
7070
return true;
7171
}
7272

@@ -80,6 +80,7 @@ protected function isIpInBotRange(string $ip): bool
8080
return true;
8181
}
8282
}
83+
8384
return false;
8485
}
8586

@@ -89,12 +90,12 @@ protected function ipInRange(string $ip, string $range): bool
8990
return $ip === $range;
9091
}
9192

92-
list($subnet, $bits) = explode('/', $range);
93+
[$subnet, $bits] = explode('/', $range);
9394
$ip = ip2long($ip);
9495
$subnet = ip2long($subnet);
9596
$mask = -1 << (32 - $bits);
9697
$subnet &= $mask;
97-
98+
9899
return ($ip & $mask) == $subnet;
99100
}
100101

@@ -105,7 +106,7 @@ public function getBotName(?string $userAgent): ?string
105106
}
106107

107108
$userAgentLower = strtolower($userAgent);
108-
109+
109110
$botNames = [
110111
'googlebot' => 'Google',
111112
'bingbot' => 'Bing',
@@ -136,4 +137,4 @@ public function getBotName(?string $userAgent): ?string
136137

137138
return null;
138139
}
139-
}
140+
}

src/Services/ExportService.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ class ExportService
1212
public function exportToCsv(Collection $data, array $columns = []): string
1313
{
1414
$csv = Writer::createFromString('');
15-
15+
1616
// Add headers
1717
if (empty($columns) && $data->isNotEmpty()) {
1818
$columns = array_keys($data->first()->toArray());
1919
}
2020
$csv->insertOne($columns);
21-
21+
2222
// Add data rows
2323
foreach ($data as $row) {
2424
$csvRow = [];
@@ -27,7 +27,7 @@ public function exportToCsv(Collection $data, array $columns = []): string
2727
}
2828
$csv->insertOne($csvRow);
2929
}
30-
30+
3131
return $csv->toString();
3232
}
3333

@@ -39,51 +39,51 @@ public function exportToJson(Collection $data): string
3939
public function getAnalyticsData(array $filters = []): Collection
4040
{
4141
$query = RequestAnalytics::query();
42-
42+
4343
if (isset($filters['start_date'])) {
4444
$query->where('visited_at', '>=', $filters['start_date']);
4545
}
46-
46+
4747
if (isset($filters['end_date'])) {
4848
$query->where('visited_at', '<=', $filters['end_date']);
4949
}
50-
50+
5151
if (isset($filters['path'])) {
52-
$query->where('path', 'like', '%' . $filters['path'] . '%');
52+
$query->where('path', 'like', '%'.$filters['path'].'%');
5353
}
54-
54+
5555
if (isset($filters['country'])) {
5656
$query->where('country', $filters['country']);
5757
}
58-
58+
5959
if (isset($filters['browser'])) {
6060
$query->where('browser', $filters['browser']);
6161
}
62-
62+
6363
if (isset($filters['device'])) {
6464
$query->where('device', $filters['device']);
6565
}
66-
66+
6767
return $query->get();
6868
}
6969

7070
public function downloadCsv(Collection $data, string $filename = 'analytics_export.csv')
7171
{
7272
$csv = $this->exportToCsv($data);
73-
73+
7474
return Response::make($csv, 200, [
7575
'Content-Type' => 'text/csv',
76-
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
76+
'Content-Disposition' => 'attachment; filename="'.$filename.'"',
7777
]);
7878
}
7979

8080
public function downloadJson(Collection $data, string $filename = 'analytics_export.json')
8181
{
8282
$json = $this->exportToJson($data);
83-
83+
8484
return Response::make($json, 200, [
8585
'Content-Type' => 'application/json',
86-
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
86+
'Content-Disposition' => 'attachment; filename="'.$filename.'"',
8787
]);
8888
}
89-
}
89+
}

src/Services/GeolocationService.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
class GeolocationService
1010
{
1111
protected string $provider;
12+
1213
protected ?string $apiKey;
14+
1315
protected int $cacheMinutes = 1440; // 24 hours
1416

1517
public function __construct()
@@ -79,19 +81,20 @@ protected function lookupWithIpApi(string $ip): array
7981

8082
protected function lookupWithIpGeolocation(string $ip): array
8183
{
82-
if (!$this->apiKey) {
84+
if (! $this->apiKey) {
8385
return $this->getDefaultLocation();
8486
}
8587

8688
try {
87-
$response = Http::timeout(5)->get("https://api.ipgeolocation.io/ipgeo", [
89+
$response = Http::timeout(5)->get('https://api.ipgeolocation.io/ipgeo', [
8890
'apiKey' => $this->apiKey,
8991
'ip' => $ip,
9092
'fields' => 'country_name,country_code2,state_prov,city,latitude,longitude,time_zone,isp',
9193
]);
9294

9395
if ($response->successful()) {
9496
$data = $response->json();
97+
9598
return [
9699
'country' => $data['country_name'] ?? '',
97100
'country_code' => $data['country_code2'] ?? '',
@@ -119,13 +122,13 @@ protected function lookupWithMaxMind(string $ip): array
119122
// This would require the GeoIP2 PHP library
120123
// composer require geoip2/geoip2
121124
// Implementation would depend on whether using web service or local database
122-
125+
123126
return $this->getDefaultLocation();
124127
}
125128

126129
protected function isLocalIp(string $ip): bool
127130
{
128-
return in_array($ip, ['127.0.0.1', '::1']) ||
131+
return in_array($ip, ['127.0.0.1', '::1']) ||
129132
str_starts_with($ip, '192.168.') ||
130133
str_starts_with($ip, '10.') ||
131134
str_starts_with($ip, '172.');
@@ -144,4 +147,4 @@ protected function getDefaultLocation(): array
144147
'isp' => '',
145148
];
146149
}
147-
}
150+
}

src/Services/VisitorTrackingService.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class VisitorTrackingService
1010
{
1111
protected string $cookieName = 'ra_visitor_id';
12+
1213
protected int $cookieLifetime = 525600; // 1 year in minutes
1314

1415
public function getVisitorId(Request $request): string
@@ -20,20 +21,20 @@ public function getVisitorId(Request $request): string
2021

2122
// Generate new visitor ID
2223
$visitorId = $this->generateVisitorId($request);
23-
24+
2425
// Set cookie for future requests
2526
Cookie::queue($this->cookieName, $visitorId, $this->cookieLifetime);
26-
27+
2728
return $visitorId;
2829
}
2930

3031
public function generateVisitorId(Request $request): string
3132
{
3233
// Create a fingerprint based on various factors
3334
$fingerprint = $this->createFingerprint($request);
34-
35+
3536
// Generate a unique ID combining fingerprint and random string
36-
return hash('sha256', $fingerprint . Str::random(32));
37+
return hash('sha256', $fingerprint.Str::random(32));
3738
}
3839

3940
protected function createFingerprint(Request $request): string
@@ -65,17 +66,17 @@ protected function generateSessionId(Request $request): string
6566
// Create a session ID based on visitor ID and timestamp
6667
$visitorId = $this->getVisitorId($request);
6768
$timestamp = floor(time() / 1800); // 30-minute sessions
68-
69-
return hash('sha256', $visitorId . '|' . $timestamp);
69+
70+
return hash('sha256', $visitorId.'|'.$timestamp);
7071
}
7172

7273
public function isNewVisitor(Request $request): bool
7374
{
74-
return !$request->hasCookie($this->cookieName);
75+
return ! $request->hasCookie($this->cookieName);
7576
}
7677

7778
public function isReturningVisitor(Request $request): bool
7879
{
7980
return $request->hasCookie($this->cookieName);
8081
}
81-
}
82+
}

0 commit comments

Comments
 (0)