Skip to content

Commit

Permalink
Merge pull request #60 from acrobat/configurable-lifetime
Browse files Browse the repository at this point in the history
Make consent cookie lifetime configurable
  • Loading branch information
acrobat authored Sep 9, 2020
2 parents 8055a21 + f1bda1e commit 1d79a00
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kunstmaan\CookieBundle\DependencyInjection;

use Kunstmaan\CookieBundle\Helper\LegalCookieHelper;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -25,9 +26,15 @@ public function getConfigTreeBuilder()
$rootNode = $treeBuilder->root('kunstmaan_cookie');
}

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNode
->children()
->arrayNode('consent_cookie')
->children()
->integerNode('lifetime')->defaultValue(LegalCookieHelper::DEFAULT_COOKIE_LIFETIME)->info('Default lifetime of 10 years')->end()
->end
->end
->end()
;

return $treeBuilder;
}
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/KunstmaanCookieExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kunstmaan\CookieBundle\DependencyInjection;

use Kunstmaan\CookieBundle\Helper\LegalCookieHelper;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
Expand All @@ -24,5 +25,7 @@ public function load(array $configs, ContainerBuilder $container)

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

$container->getDefinition(LegalCookieHelper::class)->setArgument(2, $config['cookie_lifetime']);
}
}
9 changes: 6 additions & 3 deletions src/Helper/LegalCookieHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
class LegalCookieHelper
{
const LEGAL_COOKIE_NAME = 'legal_cookie';

const FUNCTIONAL_COOKIE_NAME = 'functional_cookie';
const DEFAULT_COOKIE_LIFETIME = 10 * 365 * 24 * 60 * 60; // 10 years

/** @var array */
private $legalCookie;
Expand All @@ -25,17 +25,20 @@ class LegalCookieHelper

/** @var string */
private $adminFirewallName;
/** @var int */
private $cookieLifetime;

/**
* LegalCookieHelper constructor.
*
* @param EntityManagerInterface $em
* @param string $adminFirewallName
*/
public function __construct(EntityManagerInterface $em, $adminFirewallName)
public function __construct(EntityManagerInterface $em, $adminFirewallName, $cookieLifeTime = self::DEFAULT_COOKIE_LIFETIME)
{
$this->em = $em;
$this->adminFirewallName = $adminFirewallName;
$this->cookieLifetime = $cookieLifeTime;
}

/**
Expand Down Expand Up @@ -129,7 +132,7 @@ public function saveLegalCookie(Request $request, array $legalCookie)
return new Cookie(
self::LEGAL_COOKIE_NAME,
json_encode($legalCookie),
time() + (10 * 365 * 24 * 60 * 60),
time() + ($this->cookieLifetime),
'/',
null,
$request->isSecure(),
Expand Down

0 comments on commit 1d79a00

Please sign in to comment.