-
Notifications
You must be signed in to change notification settings - Fork 91
/
CartWatcherFacade.php
139 lines (117 loc) · 4.22 KB
/
CartWatcherFacade.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Shopsys\FrameworkBundle\Model\Cart\Watcher;
use Doctrine\ORM\EntityManagerInterface;
use Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider;
use Shopsys\FrameworkBundle\Component\FlashMessage\FlashMessage;
use Shopsys\FrameworkBundle\Model\Cart\Cart;
use Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser;
use Shopsys\FrameworkBundle\Model\Product\Exception\ProductNotFoundException;
use Twig\Environment;
abstract class CartWatcherFacade
{
/**
* @var \Doctrine\ORM\EntityManagerInterface
*/
protected $em;
/**
* @var \Shopsys\FrameworkBundle\Model\Cart\Watcher\CartWatcher
*/
protected $cartWatcher;
/**
* @var \Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser
*/
protected $currentCustomerUser;
/**
* @var \Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider
*/
protected FlashBagProvider $flashBagProvider;
/**
* @var \Twig\Environment
*/
protected $twigEnvironment;
/**
* @param \Shopsys\FrameworkBundle\Component\FlashMessage\FlashBagProvider $flashBagProvider
* @param \Doctrine\ORM\EntityManagerInterface $em
* @param \Shopsys\FrameworkBundle\Model\Cart\Watcher\CartWatcher $cartWatcher
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CurrentCustomerUser $currentCustomerUser
* @param \Twig\Environment $twigEnvironment
*/
public function __construct(
FlashBagProvider $flashBagProvider,
EntityManagerInterface $em,
CartWatcher $cartWatcher,
CurrentCustomerUser $currentCustomerUser,
Environment $twigEnvironment
) {
$this->flashBagProvider = $flashBagProvider;
$this->em = $em;
$this->cartWatcher = $cartWatcher;
$this->currentCustomerUser = $currentCustomerUser;
$this->twigEnvironment = $twigEnvironment;
}
/**
* @param \Shopsys\FrameworkBundle\Model\Cart\Cart $cart
*/
public function checkCartModifications(Cart $cart)
{
$this->checkNotListableItems($cart);
$this->checkModifiedPrices($cart);
}
/**
* @param \Shopsys\FrameworkBundle\Model\Cart\Cart $cart
*/
protected function checkModifiedPrices(Cart $cart)
{
$modifiedItems = $this->cartWatcher->getModifiedPriceItemsAndUpdatePrices($cart);
$messageTemplate = $this->twigEnvironment->createTemplate(
$this->getMessageForChangedProduct()
);
foreach ($modifiedItems as $cartItem) {
$this->flashBagProvider->getFlashBag()?->add(FlashMessage::KEY_INFO, $messageTemplate->render(['name' => $cartItem->getName()]));
}
if (count($modifiedItems) > 0) {
$this->em->flush();
}
}
/**
* @param \Shopsys\FrameworkBundle\Model\Cart\Cart $cart
*/
protected function checkNotListableItems(Cart $cart)
{
$notVisibleItems = $this->cartWatcher->getNotListableItems($cart, $this->currentCustomerUser);
$toFlush = [];
$messageTemplate = $this->twigEnvironment->createTemplate(
$this->getMessageForNoLongerAvailableExistingProduct()
);
$flashBag = $this->flashBagProvider->getFlashBag();
foreach ($notVisibleItems as $cartItem) {
try {
$productName = $cartItem->getName();
$flashBag?->add(FlashMessage::KEY_ERROR, $messageTemplate->render(['name' => $productName]));
} catch (ProductNotFoundException $e) {
$flashBag?->add(
FlashMessage::KEY_ERROR,
$this->getMessageForNoLongerAvailableProduct()
);
}
$cart->removeItemById($cartItem->getId());
$this->em->remove($cartItem);
$toFlush[] = $cartItem;
}
if (count($toFlush) > 0) {
$this->em->flush();
}
}
/**
* @return string
*/
abstract protected function getMessageForNoLongerAvailableExistingProduct(): string;
/**
* @return string
*/
abstract protected function getMessageForNoLongerAvailableProduct(): string;
/**
* @return string
*/
abstract protected function getMessageForChangedProduct(): string;
}