diff --git a/src/Services/AnalyticsClient.php b/src/Services/AnalyticsClient.php index c70d214..8039fc9 100644 --- a/src/Services/AnalyticsClient.php +++ b/src/Services/AnalyticsClient.php @@ -3,7 +3,7 @@ namespace Mattitja\Analytics\Services; use Illuminate\Support\Facades\Http; -use InvalidArgumentException; +use Illuminate\Support\Facades\Log; class AnalyticsClient { @@ -26,12 +26,12 @@ class AnalyticsClient * Send analytics data to the Mattitja Analytics API. * * @param array $data Analytics data payload - * - * @throws InvalidArgumentException If required fields are missing */ public function send(array $data): void { - $this->validateData($data); + if (! $this->validateData($data)) { + return; + } $cleanData = $this->cleanData($data); @@ -47,21 +47,33 @@ class AnalyticsClient * Validate that all required fields are present and not empty. * * @param array $data Analytics data to validate - * - * @throws InvalidArgumentException If required fields are missing + * @return bool True if valid, false otherwise */ - protected function validateData(array $data): void + protected function validateData(array $data): bool { foreach (self::REQUIRED_FIELDS as $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 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; } /** diff --git a/tests/Unit/AnalyticsClientTest.php b/tests/Unit/AnalyticsClientTest.php index 5e1f5d0..6bd28f8 100644 --- a/tests/Unit/AnalyticsClientTest.php +++ b/tests/Unit/AnalyticsClientTest.php @@ -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; - expect(fn () => $client->send([]))->toThrow(InvalidArgumentException::class) - ->and(fn () => $client->send(['k' => 'test']))->toThrow(InvalidArgumentException::class) - ->and(fn () => $client->send(['k' => 'test', 'u' => 'url', 'ip' => '1.1.1.1']))->toThrow(InvalidArgumentException::class); + // Should not throw exceptions, just skip silently + $client->send([]); + $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 () {