diff --git a/src/Charcoal/CookieConsent/CookieConsentManager.php b/src/Charcoal/CookieConsent/CookieConsentManager.php index a03d85a..4052c56 100644 --- a/src/Charcoal/CookieConsent/CookieConsentManager.php +++ b/src/Charcoal/CookieConsent/CookieConsentManager.php @@ -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; @@ -42,6 +43,14 @@ public function __construct( $this->translator = $translator; } + /** + * @return bool + */ + public function isCookieConsentActive(): bool + { + return !!$this->getPluginOptions(); + } + /** * @return string */ @@ -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 []; } diff --git a/src/Charcoal/CookieConsent/HasCookieConsentTrait.php b/src/Charcoal/CookieConsent/HasCookieConsentTrait.php new file mode 100644 index 0000000..0bc04b2 --- /dev/null +++ b/src/Charcoal/CookieConsent/HasCookieConsentTrait.php @@ -0,0 +1,74 @@ +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; + } +}