Closed xorgxx closed 4 years ago
Isn't it sufficient to be able to change the prefix of the cookie names? In that case you only have to add the cookie_prefix option and that would leave the code much cleaner. Right now the cookiePrefix isn't used anywhere. Also I think only in the Enum class the custom prefix should be injected
Yes cookie_prefix did not see where to use, in first analyse. As you, maybe it's better to use only cookie_prefix and inject in Enum class but are making injection ? we cant modifie const, i see only one way : to create service CookieNameEnum with gutters and setters and inject this service where it needs. But this solution is almost the identical to me.
class CookieNameEnum
{
const COOKIE_CONSENT_NAME = 'cookie_Consent';
const COOKIE_CONSENT_KEY_NAME = 'cookie_Consent_Key';
const COOKIE_CATEGORY_NAME_PREFIX = 'cookie_Category_';
private $cookiePrefix;
/**
* @var array
*/
public static $prefixCookie = [
"COOKIE_CONSENT_NAME" => "",
"COOKIE_CONSENT_KEY_NAME" => "",
"COOKIE_CATEGORY_NAME_PREFIX" => "",
];
private static $Prefix;
public function __construct(string $cookiePrefix)
{
self::$Prefix = $cookiePrefix;
self::$prefixCookie = [
"COOKIE_CONSENT_NAME" => self::$Prefix . self::COOKIE_CONSENT_NAME ,
"COOKIE_CONSENT_KEY_NAME" => self::$Prefix . self::COOKIE_CONSENT_KEY_NAME ,
"COOKIE_CATEGORY_NAME_PREFIX" => self::$Prefix . self::COOKIE_CONSENT_KEY_NAME ,
];
}
/**
* Get all cookie consent prefix (this was only for test).
*/
public static function getAvailableCookie ( ){
self::$prefixCookie = [
"COOKIE_CONSENT_NAME" => self::$Prefix . self::COOKIE_CONSENT_NAME ,
"COOKIE_CONSENT_KEY_NAME" => self::$Prefix . self::COOKIE_CONSENT_KEY_NAME ,
"COOKIE_CATEGORY_NAME_PREFIX" => self::$Prefix . self::COOKIE_CONSENT_KEY_NAME ,
];
return self::$prefixCookie;
}
/**
* Get cookie category name.
*/
public static function getCookieCategoryName(string $category): string
{
return self::$Prefix . self::COOKIE_CATEGORY_NAME_PREFIX.$category;
}
}
and for exemple CookieChecker :
/**
* CookieChecker constructor.
*
* @param Request $request
*/
public function __construct(Request $request, CookieNameEnum $cookieNameEnum)
{
$this->request = $request;
$this->cookiePrefix = $cookieNameEnum;
}
.....
/**
* @return bool
*/
public function isCookieConsentSavedByUser( ): bool
{
return $this->request->cookies->has( CookieNameEnum::$prefixCookie["COOKIE_CONSENT_NAME"]);
}
Also:
./bin/php-cs-fixer fix
before committing your contributionin the last version i have include :
class CookieNameEnum
{
const COOKIE_PREFIX = 'cookie_';
const COOKIE_CONSENT_NAME = 'Consent';
const COOKIE_CONSENT_KEY_NAME = '_Key';
const COOKIE_CATEGORY_NAME_PREFIX = 'Category_';
private $cookiePrefix;
/**
* @var array
*/
public static $prefixCookie = [
"COOKIE_CONSENT_NAME" => "",
"COOKIE_CONSENT_KEY_NAME" => "",
"COOKIE_CATEGORY_NAME_PREFIX" => "",
];
private static $Prefix;
private static $Consent_name;
private static $Consent_key;
private static $Consent_category;
public function __construct(string $cookiePrefix, string $cookieKeyName, string $cookieConsentName, string $cookieConsentCategory) //, string $cookiePrefix
{
self::$Prefix = $cookiePrefix;
self::$Consent_name = $cookieConsentName;
self::$Consent_key = $cookieKeyName;
self::$Consent_category = $cookieConsentCategory;
self::$prefixCookie = [
"COOKIE_CONSENT_NAME" => self::$Prefix . self::$Consent_name ,
"COOKIE_CONSENT_KEY_NAME" => self::$Prefix . self::$Consent_name . self::$Consent_key ,
"COOKIE_CATEGORY_NAME_PREFIX" => self::$Prefix . self::$Consent_category ,
];
}
/**
* Get all cookie consent prefix (this was only for test).
*/
public static function getAvailableCookie ( ){
self::$prefixCookie = [
"COOKIE_CONSENT_NAME" => self::$Prefix . self::$Consent_name ,
"COOKIE_CONSENT_KEY_NAME" => self::$Prefix . self::$Consent_name . self::$Consent_key ,
"COOKIE_CATEGORY_NAME_PREFIX" => self::$Prefix . self::$Consent_category ,
];
return self::$prefixCookie;
}
/**
* Get cookie category name.
*/
public static function getCookieCategoryName(string $category): string
{
return self::$Prefix . self::$Consent_category.$category;
}
}
i think that it can be gainful and it's include. it das not take must.
To be possible to modify "Cookie_Consent" and "Cookie_Category" by a variable in the config.yml
27