Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie Factory - Json validation and log if null #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Elao\Bundle\ConsentBundle\EventListener\ConsentEventSubscriber;
use Elao\Bundle\ConsentBundle\Renderer\ToastRenderer;
use Elao\Bundle\ConsentBundle\Twig\ConsentExtension;
use Psr\Log\LoggerInterface;
use Symfony\Component\Asset\Packages;

return function(ContainerConfigurator $configurator) {
Expand All @@ -17,6 +18,7 @@
->args([
'$name' => abstract_arg('cookie.name'),
'$ttl' => abstract_arg('cookie.ttl'),
'$logger' => service(LoggerInterface::class),
])
;

Expand Down
17 changes: 15 additions & 2 deletions src/Cookie/CookieFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@

namespace Elao\Bundle\ConsentBundle\Cookie;

use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;

class CookieFactory
{
private string $name;
private int $ttl;
private LoggerInterface $logger;

public function __construct(string $name, int $ttl)
public function __construct(string $name, int $ttl, LoggerInterface $logger)
{
$this->name = $name;
$this->ttl = $ttl;
$this->logger = $logger;
}

public function create(array $consents): Cookie
Expand All @@ -34,6 +37,16 @@ public function create(array $consents): Cookie

public function read(Request $request): array
{
return json_decode($request->cookies->get($this->name, '[]'), true);
$cookieValue = json_decode($request->cookies->get($this->name, '[]'), true);

if (null === $cookieValue) {
$this->logger->warning('Consent Bundle - Decoded cookie value return null', [
'Cookie value' => $request->cookies->get($this->name),
]);

return [];
}

return $cookieValue;
}
}