forked from SymfonyCasts/symfony-ux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-turbo-fix-deprecations.diff
64 lines (58 loc) · 2.05 KB
/
pre-turbo-fix-deprecations.diff
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
63
64
diff --git a/src/Service/CartStorage.php b/src/Service/CartStorage.php
index 217fced..c1f323a 100644
--- a/src/Service/CartStorage.php
+++ b/src/Service/CartStorage.php
@@ -6,17 +6,18 @@ use App\Entity\Cart;
use App\Entity\CartItem;
use App\Repository\ColorRepository;
use App\Repository\ProductRepository;
+use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CartStorage
{
- private $session;
+ private $requestStack;
private $productRepository;
private $colorRepository;
- public function __construct(SessionInterface $session, ProductRepository $productRepository, ColorRepository $colorRepository)
+ public function __construct(RequestStack $requestStack, ProductRepository $productRepository, ColorRepository $colorRepository)
{
- $this->session = $session;
+ $this->requestStack = $requestStack;
$this->productRepository = $productRepository;
$this->colorRepository = $colorRepository;
}
@@ -24,10 +25,10 @@ class CartStorage
public function getCart(): ?Cart
{
$key = self::getKey();
- if (!$this->session->has($key)) {
+ if (!$this->getSession()->has($key)) {
return null;
}
- $cart = $this->session->get($key);
+ $cart = $this->getSession()->get($key);
if (!$cart instanceof Cart) {
throw new \InvalidArgumentException('Wrong cart type');
@@ -55,16 +56,22 @@ class CartStorage
public function save(Cart $cart)
{
- $this->session->set(self::getKey(), $cart);
+ $this->getSession()->set(self::getKey(), $cart);
}
public function clearCart()
{
- $this->session->remove(self::getKey());
+ $this->getSession()->remove(self::getKey());
}
private static function getKey(): string
{
return sprintf('_cart_storage');
}
+
+ private function getSession(): SessionInterface
+ {
+ return $this->requestStack->getCurrentRequest()
+ ->getSession();
+ }
}