fix: added logs instead of exception

This commit is contained in:
mathiaspalmqvist committed 2026-01-03 14:55:34 +01:00
1 parent f25b67f6f1
commit 630620e4e6
2 files changed
+43 -13

No files matched your search

+21 -9
View File
@@ -3,7 +3,7 @@
namespace Mattitja\Analytics\Services; namespace Mattitja\Analytics\Services;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Http;
use InvalidArgumentException; use Illuminate\Support\Facades\Log;
class AnalyticsClient class AnalyticsClient
{ {
@@ -26,12 +26,12 @@ class AnalyticsClient
* Send analytics data to the Mattitja Analytics API. * Send analytics data to the Mattitja Analytics API.
* *
* @param array $data Analytics data payload * @param array $data Analytics data payload
*
* @throws InvalidArgumentException If required fields are missing
*/ */
public function send(array $data): void public function send(array $data): void
{ {
$this->validateData($data); if (! $this->validateData($data)) {
return;
}
$cleanData = $this->cleanData($data); $cleanData = $this->cleanData($data);
@@ -47,21 +47,33 @@ class AnalyticsClient
* Validate that all required fields are present and not empty. * Validate that all required fields are present and not empty.
* *
* @param array $data Analytics data to validate * @param array $data Analytics data to validate
* * @return bool True if valid, false otherwise
* @throws InvalidArgumentException If required fields are missing
*/ */
protected function validateData(array $data): void protected function validateData(array $data): bool
{ {
foreach (self::REQUIRED_FIELDS as $field) { foreach (self::REQUIRED_FIELDS as $field) {
if (! isset($data[$field])) { if (! isset($data[$field])) {
throw new InvalidArgumentException("Required field '{$field}' is missing"); Log::warning("Analytics: Required field '{$field}' is missing");
return false;
} }
// User agent can be empty string, but other fields cannot // User agent can be empty string, but other fields cannot
if ($field !== 'ua' && empty($data[$field])) { if ($field !== 'ua' && empty($data[$field])) {
throw new InvalidArgumentException("Required field '{$field}' cannot be empty"); Log::warning("Analytics: Required field '{$field}' cannot be empty");
return false;
} }
} }
// URL max length check (API spec: max 512 characters)
if (isset($data['u']) && strlen($data['u']) > 512) {
Log::warning('Analytics: URL exceeds 512 characters, skipping');
return false;
}
return true;
} }
/** /**
+22 -4
View File
@@ -60,12 +60,30 @@ it('uses hardcoded timeout', function () {
}); });
}); });
it('validates required data fields', function () { it('skips sending when required data fields are missing', function () {
$client = new AnalyticsClient; $client = new AnalyticsClient;
expect(fn () => $client->send([]))->toThrow(InvalidArgumentException::class) // Should not throw exceptions, just skip silently
->and(fn () => $client->send(['k' => 'test']))->toThrow(InvalidArgumentException::class) $client->send([]);
->and(fn () => $client->send(['k' => 'test', 'u' => 'url', 'ip' => '1.1.1.1']))->toThrow(InvalidArgumentException::class); $client->send(['k' => 'test']);
$client->send(['k' => 'test', 'u' => 'url', 'ip' => '1.1.1.1']);
// No HTTP requests should be sent for invalid data
Http::assertNothingSent();
});
it('skips sending when URL exceeds 512 characters', function () {
$client = new AnalyticsClient;
$longUrl = 'https://example.com/'.str_repeat('a', 500);
$client->send([
'k' => 'testsiteky',
'u' => $longUrl,
'ip' => '192.168.1.1',
'ua' => 'Mozilla/5.0 Chrome',
]);
Http::assertNothingSent();
}); });
it('filters out empty referrer URLs', function () { it('filters out empty referrer URLs', function () {