refactor: use DTO and terminate middleware for better performance
- Add PageviewData readonly DTO class for type safety - Update AnalyticsClient to accept PageviewData instead of array - Move analytics sending to terminate() method (runs after response sent) - Update all tests for new DTO pattern
This commit is contained in:
6 files changed
+154
-116
No files matched your search
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Mattitja\Analytics\Data;
|
||||
|
||||
readonly class PageviewData
|
||||
{
|
||||
public function __construct(
|
||||
public string $siteKey,
|
||||
public string $url,
|
||||
public string $ip,
|
||||
public string $userAgent,
|
||||
public ?string $referrer = null,
|
||||
) {}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
$data = [
|
||||
'k' => $this->siteKey,
|
||||
'u' => $this->url,
|
||||
'ip' => $this->ip,
|
||||
'ua' => $this->userAgent,
|
||||
];
|
||||
|
||||
if ($this->referrer !== null && $this->referrer !== '') {
|
||||
$data['r'] = $this->referrer;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user