Skip to content
Snippets Groups Projects

Issue #3325602: Make the cookie set/get a service

Open Hardik Pandya requested to merge issue/like-3325602:3325602-make-the-cookie into 1.x
4 files
+ 98
26
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 48
0
<?php
namespace Drupal\like;
use Drupal\Component\Serialization\Json;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Like Cookie helper class.
*/
class LikeCookieHelper implements LikeCookieHelperInterface {
/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* Constructs a LikeCookieHelper object.
*
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
*/
public function __construct(RequestStack $request_stack) {
$this->requestStack = $request_stack;
}
/**
* {@inheritdoc}
*/
public function getLikeCookieData():array {
$cookie = $this->requestStack->getCurrentRequest()->cookies->get('Drupal_visitor_like');
return $cookie ? Json::decode($cookie) : [];
}
/**
* {@inheritdoc}
*/
public function setLikeCookieData($cookie_data):bool {
if (isset($this->requestStack->getCurrentRequest()->cookies)) {
$this->requestStack->getCurrentRequest()->cookies->set('Drupal_visitor_like', $cookie_data);
}
setrawcookie('Drupal.visitor.like', rawurlencode($cookie_data), $expire, '/');
}
}
Loading