Skip to content

Commit

Permalink
Merge pull request ezsystems#3 from clash82/better
Browse files Browse the repository at this point in the history
EZP-24231: As a Developer I would like to have more abstract structure...
  • Loading branch information
andrerom committed Apr 30, 2015
2 parents e93f0af + 91f67f2 commit 6f794ee
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 51 deletions.
44 changes: 44 additions & 0 deletions Banner/Banner.php
@@ -0,0 +1,44 @@
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\PrivacyCookieBundle\Banner;

class Banner
{
/**
* Banner caption.
*
* @var string
*/
public $caption;

/**
* Text linked to the policy page.
*
* @var string
*/
public $learnMoreText;

/**
* URL to the detailled cookies & privacy legacy page.
*
* @var string
*/
public $policyPageUrl;

/**
* Name of the cookie used to store the user's choice.
*
* @var string
*/
public $cookieName;

/**
* Cookie validity duration, in days.
*
* @var int
*/
public $cookieValidity;
}
16 changes: 16 additions & 0 deletions Banner/BannerFactory.php
@@ -0,0 +1,16 @@
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\PrivacyCookieBundle\Banner;

interface BannerFactory
{
/**
* Builds the privacy cookie banner object, using any kind of data source.
*
* @return \EzSystems\PrivacyCookieBundle\Banner\Banner
*/
public static function build();
}
31 changes: 31 additions & 0 deletions Banner/BannerOptions.php
@@ -0,0 +1,31 @@
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\PrivacyCookieBundle\Banner;

use \EzSystems\PrivacyCookieBundle\Banner\Banner;

class BannerOptions
{
/**
* Map banner options.
*
* @param array $options
* @param \EzSystems\PrivacyCookieBundle\Banner\Banner $banner
* @return array
*/
public function map(array $options, Banner $banner)
{
$bannerOptions = get_object_vars($banner);

$validatedOptions = array();
foreach ($bannerOptions as $name => $value)
{
$validatedOptions[$name] = empty($options[$name]) ? $value : $options[$name];
}

return $validatedOptions;
}
}
13 changes: 13 additions & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -26,6 +26,19 @@ public function getConfigTreeBuilder()
->defaultValue('365')
->info('how many days banner should be hidden when user accepts policy?')
->isRequired()
->end()
->scalarNode('banner_caption')
->defaultValue('Cookies help us create a good experience. By using our website, you agree to our use of cookies.')
->info('banner description')
->isRequired()
->end()
->scalarNode('banner_link_text')
->defaultValue('Learn more')
->info('banner policy link caption')
->isRequired()
->end()
->scalarNode('banner_link_url')
->info('banner policy address')
->end();

return $treeBuilder;
Expand Down
25 changes: 25 additions & 0 deletions Factory/ConfigurationBasedBannerFactory.php
@@ -0,0 +1,25 @@
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/

namespace EzSystems\PrivacyCookieBundle\Factory;

use EzSystems\PrivacyCookieBundle\Banner\Banner;
use EzSystems\PrivacyCookieBundle\Banner\BannerFactory;

class ConfigurationBasedBannerFactory implements BannerFactory
{
public static function build(array $configuration = array())
{
$banner = new Banner();

$banner->cookieName = $configuration['cookie_name'];
$banner->cookieValidity = $configuration['cookie_validity'];
$banner->caption = $configuration['banner_caption'];
$banner->learnMoreText = $configuration['banner_link_text'];
$banner->policyPageUrl = $configuration['banner_link_url'];

return $banner;
}
}
7 changes: 6 additions & 1 deletion Resources/config/default_settings.yml
@@ -1,6 +1,11 @@
parameters:
# how many days banner should be hidden when user accepts policy?
ez_privacy_cookie.days: 365

# name to be used to store cookie status
ez_privacy_cookie.cookie_name: "privacyCookieAccepted"
# banner description
ez_privacy_cookie.banner_caption: "Cookies help us create a good experience. By using our website, you agree to our use of cookies."
# banner policy link caption
ez_privacy_cookie.banner_link_text: "Learn more"
# banner policy address
ez_privacy_cookie.banner_link_url: ""
25 changes: 22 additions & 3 deletions Resources/config/services.yml
@@ -1,12 +1,31 @@
parameters:
ez_privacy_cookie_twig_extension: EzSystems\PrivacyCookieBundle\Twig\PrivacyCookieTwigExtension
ez_privacy_cookie.banner_factory.class: \EzSystems\PrivacyCookieBundle\Banner\BannerFactory
ez_privacy_cookie.configuration_banner_factory.class: EzSystems\PrivacyCookieBundle\Factory\ConfigurationBasedBannerFactory
ez_privacy_cookie.banner_options.class: EzSystems\PrivacyCookieBundle\Banner\BannerOptions

services:
ez_privacy_cookie.banner_options:
class: %ez_privacy_cookie.banner_options.class%

ez_privacy_cookie.twig.extension:
class: %ez_privacy_cookie_twig_extension%
arguments:
- @service_container
- %ez_privacy_cookie.cookie_name%
- %ez_privacy_cookie.days%
- @ez_privacy_cookie.banner_options
- @ez_privacy_cookie.banner_factory
tags:
- { name: twig.extension }

ez_privacy_cookie.banner_factory:
class: %ez_privacy_cookie.banner_factory.class%
alias: ez_privacy_cookie.configuration_banner_factory

ez_privacy_cookie.configuration_banner_factory:
class: %ez_privacy_cookie.configuration_banner_factory.class%
factory: ["%ez_privacy_cookie.configuration_banner_factory.class%", build]
arguments:
- cookie_name: %ez_privacy_cookie.cookie_name%
cookie_validity: %ez_privacy_cookie.days%
banner_caption: %ez_privacy_cookie.banner_caption%
banner_link_text: %ez_privacy_cookie.banner_link_text%
banner_link_url: %ez_privacy_cookie.banner_link_url%
14 changes: 5 additions & 9 deletions Resources/views/privacycookie.html.twig
@@ -1,11 +1,7 @@
<div class="privacy-cookie-banner" id="privacy-cookie-banner">
<div class="col-md-1"></div>
<div class="col-md-7 privacy-cookie-banner__message">
<p>{% if bannerCaption is empty %}
{{ 'Cookies help us create a good experience. By using our website, you agree to our use of cookies.'|trans }}
{% else %}
{{ bannerCaption }}
{% endif %}</p>
<p>{{ caption }}</p>
</div>
<div class="col-md-4 privacy-cookie-banner__action">
<div class="attribute-link pull-left">
Expand All @@ -14,9 +10,9 @@
</a>
</div>
<div class="attribute-link pull-left">
{% if policyUrl is not empty %}
<a class="privacy-cookie-banner__learn-more" href="{{ policyUrl }}" title="{{ 'Learn more'|trans }}">
{{ 'Learn more'|trans }}
{% if policyPageUrl is not empty %}
<a class="privacy-cookie-banner__learn-more" href="{{ policyPageUrl }}" title="{{ learnMoreText }}">
{{ learnMoreText }}
</a>
{% endif %}
</div>
Expand All @@ -33,7 +29,7 @@
(function() {
var privacyCookieBanner = new eZ.PrivacyCookieBanner({
cookieName: '{{ cookieName }}',
days: {{ days }}
days: {{ cookieValidity }}
});
setTimeout(function () {
privacyCookieBanner.show()
Expand Down
61 changes: 23 additions & 38 deletions Twig/PrivacyCookieTwigExtension.php
Expand Up @@ -5,42 +5,31 @@

namespace EzSystems\PrivacyCookieBundle\Twig;

use \Symfony\Component\DependencyInjection\ContainerInterface;
use \Twig_Extension;
use \Twig_Function_Method;
use EzSystems\PrivacyCookieBundle\Banner\Banner;
use EzSystems\PrivacyCookieBundle\Banner\BannerOptions;
use Twig_Extension;
use Twig_SimpleFunction;
use Twig_Environment;

/**
* PrivacyCookie Twig helper which renders necessary snippet code.
*/
class PrivacyCookieTwigExtension extends Twig_Extension
{
/**
* we must inject service_container this way
* @link https://github.com/symfony/symfony/issues/2347
*
* @var $container \Symfony\Component\DependencyInjection\ContainerInterface
* @var \EzSystems\PrivacyCookieBundle\Banner\Banner
*/
protected $container;
protected $banner;

/**
* default cookie name
*
* @var $defaultCookieName string
* @var \EzSystems\PrivacyCookieBundle\Banner\BannerOptions
*/
protected $defaultCookieName;
protected $bannerOptions;

/**
* default cookie validity days
*
* @var $defaultValidityDays int
*/
protected $defaultValidityDays;

public function __construct(ContainerInterface $container, $defaultCookieName, $defaultValidityDays)
public function __construct(BannerOptions $bannerOptions, Banner $banner)
{
$this->container = $container;
$this->defaultCookieName = $defaultCookieName;
$this->defaultValidityDays = $defaultValidityDays;
$this->bannerOptions = $bannerOptions;
$this->banner = $banner;
}

public function getName()
Expand All @@ -51,8 +40,9 @@ public function getName()
public function getFunctions()
{
return array(
'show_privacy_cookie_banner' => new Twig_Function_Method($this, 'showPrivacyCookieBanner', array(
'is_safe' => array( 'html' )
new Twig_SimpleFunction('show_privacy_cookie_banner', array($this, 'showPrivacyCookieBanner'), array(
'is_safe' => array('html'),
'needs_environment' => true
)),
);
}
Expand All @@ -61,23 +51,18 @@ public function getFunctions()
* Render cookie privacy banner snippet code
* - should be included at the end of template before the body ending tag
*
* @param string $policyUrl cookie policy page address (not required, no policy link will be shown)
* @param array $options optional parameters:
* cookieName - name to be used to store cookie status
* days - for how many days this banner should be hidden when user accepts policy?
* bannerCaption - replace default banner message caption
* @param Twig_Environment $twigEnvironment
* @param string $policyPageUrl cookie policy page address (not required, no policy link will be shown)
* @param array $options override default options
* @return string
*/
public function showPrivacyCookieBanner($policyUrl = null, array $options = array())
public function showPrivacyCookieBanner(Twig_Environment $twigEnvironment, $policyPageUrl = null, $options = array())
{
return $this->container->get("templating")->render(
$options['policyPageUrl'] = $policyPageUrl;

return $twigEnvironment->render(
'@EzSystemsPrivacyCookieBundle/Resources/views/privacycookie.html.twig',
array(
'policyUrl' => $policyUrl,
'cookieName' => empty($options[ 'cookieName' ]) ? $this->defaultCookieName : $options[ 'cookieName' ],
'days' => empty($options[ 'days' ]) ? $this->defaultValidityDays : $options[ 'days' ],
'bannerCaption' => empty($options[ 'bannerCaption' ]) ? null : $options[ 'bannerCaption' ]
)
$this->bannerOptions->map($options, $this->banner)
);
}
}

0 comments on commit 6f794ee

Please sign in to comment.