diff --git a/composer.json b/composer.json index 5f74e39..3564536 100644 --- a/composer.json +++ b/composer.json @@ -12,15 +12,15 @@ ], "require": { "php": "^8.0", - "symfony/framework-bundle": "^6.0 | ^7.0", + "symfony/framework-bundle": "^6.0 | ^7.0 | ^8.0", "symfony/orm-pack": "^2.3", "twig/twig": "^3.0", - "symfony/yaml": "^6.0 | ^7.0", - "symfony/asset": "^6.0 | ^7.0", - "symfony/console": "^6.0 | ^7.0", - "symfony/http-foundation": "^6.0 | ^7.0", - "symfony/http-kernel": "^6.0 | ^7.0", - "symfony/dependency-injection": "^6.0 | ^7.0" + "symfony/yaml": "^6.0 | ^7.0 | ^8.0", + "symfony/asset": "^6.0 | ^7.0 | ^8.0", + "symfony/console": "^6.0 | ^7.0 | ^8.0", + "symfony/http-foundation": "^6.0 | ^7.0 | ^8.0", + "symfony/http-kernel": "^6.0 | ^7.0 | ^8.0", + "symfony/dependency-injection": "^6.0 | ^7.0 | ^8.0" }, "autoload": { "psr-4": { diff --git a/src/Controller/CookieConsentController.php b/src/Controller/CookieConsentController.php index aebb6d2..e241220 100644 --- a/src/Controller/CookieConsentController.php +++ b/src/Controller/CookieConsentController.php @@ -5,7 +5,7 @@ use Exception; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Routing\Annotation\Route; +use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RequestStack; use Eveltic\CookieBundle\Service\CookieConsentService; diff --git a/src/Service/CookieConsentService.php b/src/Service/CookieConsentService.php index 140fcc8..b1a00d3 100644 --- a/src/Service/CookieConsentService.php +++ b/src/Service/CookieConsentService.php @@ -112,11 +112,14 @@ public function isConsentGiven(): bool { $consentData = $this->getConsentCookie(); + if (!isset($consentData['expiration'], $consentData['version'])) { + return false; + } + $currentDate = new DateTime(); - $expirationDate = (new DateTime())->setTimestamp(strtotime($consentData['expiration']??null)); - - if (isset($consentData['version']) && - $consentData['version'] === $this->currentVersion && + $expirationDate = (new DateTime())->setTimestamp((int) strtotime($consentData['expiration'])); + + if ($consentData['version'] === $this->currentVersion && $expirationDate > $currentDate) { return true; } @@ -171,7 +174,12 @@ public function getConsentCookie(): ?array $request = $this->requestStack->getCurrentRequest(); // Get the value of the consent cookie using the defined cookie name. - $cookieValue = $request->cookies->get(self::COOKIE_NAME, null); + $cookieValue = $request?->cookies->get(self::COOKIE_NAME); + + // No consent cookie present yet — nothing to decode. + if (!is_string($cookieValue) || '' === $cookieValue) { + return $this->cachedConsentCookie = null; + } // Decode the JSON-encoded cookie value into an associative array. try {