Skip to content

Commit

Permalink
fix(cookie-consent): fix cookie-consent error when consent is disabled
Browse files Browse the repository at this point in the history
changes:
 - add active check
 - deactivated consent settings no longer throws an error
  - add a template trait to help with front-end formatting
  • Loading branch information
JoelAlphonso committed May 8, 2024
1 parent 5046b55 commit fc55492
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Charcoal/CookieConsent/CookieConsentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Charcoal\CookieConsent;

use Charcoal\CookieConsent\Exception\InvalidArgumentException;
use Charcoal\CookieConsent\Exception\ModelNotFoundException;
use Charcoal\CookieConsent\Model;
use Charcoal\CookieConsent\Model\Repository\DisclosureRepository;
use Charcoal\CookieConsent\Config\PluginConfig;
Expand Down Expand Up @@ -42,6 +43,14 @@ public function __construct(
$this->translator = $translator;
}

/**
* @return bool
*/
public function isCookieConsentActive(): bool
{
return !!$this->getPluginOptions();
}

/**
* @return string
*/
Expand All @@ -67,7 +76,12 @@ public function getPluginOptions(): array
*/
public function createPluginOptions(): array
{
$disclosureInstance = $this->disclosureRepository->getDisclosure();
try {
$disclosureInstance = $this->disclosureRepository->getDisclosure();
} catch (ModelNotFoundException $e) {
return [];
}

if (!$disclosureInstance) {
return [];
}
Expand Down
74 changes: 74 additions & 0 deletions src/Charcoal/CookieConsent/HasCookieConsentTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Charcoal\CookieConsent;

use Pimple\Container;

/**
* Provides templating tools for cookie consent integration
*/
trait HasCookieConsentTrait
{
protected CookieConsentManager $cookieConsent;

/**
* @return string
*/
public function cookieConsentConfigAsJson() : string
{
return $this->cookieConsent->getPluginOptionsAsJson();
}

public function hasCookieConsent(): bool
{
return $this->cookieConsent->isCookieConsentActive();
}

public function cookieConsentScriptTag(): string
{
if (!$this->hasCookieConsent()) {
return 'type=text/javascript';
}

return 'type=text/plain';
}

/**
* twig syntax
*
* @return string
*/
public function getCookieConsentConfigAsJson(): string
{
return $this->cookieConsentConfigAsJson();
}

/**
* twig syntax
*
* @return string
*/
public function getCookieConsentScriptTag(): string
{
return $this->cookieConsentScriptTag();
}

/**
* twig syntax
*
* @return string
*/
public function getHasCookieConsent(): bool
{
return $this->hasCookieConsent();
}

/**
* @param Container|CookieConsentManager $cookieConsent
* @return void
*/
public function setCookieConsent($cookieConsent)
{
$this->cookieConsent = $cookieConsent instanceof Container ? $cookieConsent['cookie-consent'] : $cookieConsent;
}
}

0 comments on commit fc55492

Please sign in to comment.