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:
mathiaspalmqvist committed 2026-01-03 15:09:48 +01:00
1 parent 448ac3e4cf
commit e3911ca12c
6 files changed
+154 -116

No files matched your search

+30
View File
@@ -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;
}
}