-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductDetailListener.php
62 lines (50 loc) · 1.94 KB
/
ProductDetailListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
// /app/Customize/EventListener/ProductDetailListener.php
namespace Customize\EventListener;
use Eccube\Repository\ProductRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class ProductDetailListener implements EventSubscriberInterface
{
private $session;
protected $productRepository;
public function __construct(
SessionInterface $session,
ProductRepository $productRepository)
{
$this->session = $session;
$this->productRepository = $productRepository;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
];
}
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
if ($request->attributes->get('_route') === 'product_detail') {
$productId = $request->attributes->get('id');
$Product = $this->productRepository->findOneBy(['id' => $productId ]);
// セッションから閲覧履歴を取得
$history = $this->session->get('product_history', []);
// 閲覧履歴に同じ商品が存在するか確認
foreach ($history as $key => $storedProduct) {
if ($storedProduct->getId() === $Product->getId()) {
// 同じ商品を見つけたら削除
unset($history[$key]);
break;
}
}
// 商品を履歴の先頭に追加
array_unshift($history, $Product);
// 履歴を最大10件に制限
$history = array_slice($history, 0, 10);
// セッションに履歴を保存
$this->session->set('product_history', $history);
}
}
}