Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active April 21, 2020 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurozumi/169e0acf19a7190e600b6b6332542f24 to your computer and use it in GitHub Desktop.
Save kurozumi/169e0acf19a7190e600b6b6332542f24 to your computer and use it in GitHub Desktop.
商品IDをCookieに保存したり取得したり
<?php
namespace Customize\Service;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Eccube\Entity\Product;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
/**
* 商品IDをCookieに保存したり取得したり
*
* Class CheckedProduct
* @package Customize\Service
*/
class CheckedProduct
{
/**
* @var string
*/
private $cookie_name = 'cookie_name';
/**
* @var int
*/
private $limit = 5;
/**
* @var Request
*/
private $request;
/**
* @var ArrayCollection
*/
private $products;
public function __construct(
RequestStack $requestStack
) {
$this->request = $requestStack->getCurrentRequest();
$cookie = $this->getCookie();
$cookie = $cookie ? explode(',', $cookie) : [];
$this->products = new ArrayCollection(array_map('intval', $cookie));
}
/**
* Cookieから商品IDを取得
*
* @return Collection
*/
public function getProducts(): Collection
{
return $this->products;
}
/**
* 商品IDを追加
*
* @param Product $product
* @return $this
*/
public function addProduct(Product $product): self
{
if(!in_array($product->getId(), $this->products->getValues(), true)) {
$products = $this->products->toArray();
array_unshift($products, $product->getId());
$this->products = new ArrayCollection($products);
}
if($this->products->count() > $this->getLimit()) {
$this->products->removeElement($this->products->last());
}
return $this;
}
/**
* 保持上限をセット
*
* @param int $limit
* @return $this
*/
public function setLimit(int $limit): self
{
$this->limit = $limit;
return $this;
}
/**
* 保持上限を取得
*
* @return int
*/
public function getLimit(): int
{
return $this->limit;
}
/**
* Cookieを取得
*
* @return string|null
*/
public function getCookie(): ?string
{
if(!$this->request) {
return null;
}
return $this->request->cookies->get($this->getCookieName());
}
/**
* Cookieをセット
*
* @param Response $response
* @param Cookie $cookie
* @return Response
*/
public function setCookie(Response $response, Cookie $cookie): Response
{
$response->headers->setCookie($cookie);
return $response;
}
/**
* Cookie削除
*
* @param Response $response
* @param Cookie $cookie
* @return Response
*/
public function remove(Response $response, Cookie $cookie): Response
{
$response->headers->removeCookie($cookie->getName(), $cookie->getPath(), $cookie->getDomain());
return $response;
}
/**
* Cookie名をセット
*
* @param string $cookie_name
* @return $this
*/
public function setCookieName(string $cookie_name): self
{
$this->cookie_name = $cookie_name;
return $this;
}
/**
* Cookie名を取得
*
* @return string
*/
public function getCookieName(): string
{
return $this->cookie_name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment