-
Notifications
You must be signed in to change notification settings - Fork 967
/
CartRuleLoader.php
179 lines (138 loc) · 5.14 KB
/
CartRuleLoader.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php declare(strict_types=1);
namespace Shopware\Core\Checkout\Cart;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Cart\Exception\CartTokenNotFoundException;
use Shopware\Core\Content\Rule\RuleCollection;
use Shopware\Core\Framework\Context;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
class CartRuleLoader
{
public const CHECKOUT_RULE_LOADER_CACHE_KEY = 'all-rules';
private const MAX_ITERATION = 7;
/**
* @var CartPersisterInterface
*/
private $cartPersister;
/**
* @var RuleCollection|null
*/
private $rules;
/**
* @var Processor
*/
private $processor;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var TagAwareAdapterInterface
*/
private $cache;
/**
* @var RuleLoader
*/
private $ruleLoader;
public function __construct(
CartPersisterInterface $cartPersister,
Processor $processor,
LoggerInterface $logger,
TagAwareAdapterInterface $cache,
RuleLoader $loader
) {
$this->cartPersister = $cartPersister;
$this->processor = $processor;
$this->logger = $logger;
$this->cache = $cache;
$this->ruleLoader = $loader;
}
public function loadByToken(SalesChannelContext $context, string $cartToken): RuleLoaderResult
{
try {
$cart = $this->cartPersister->load($cartToken, $context);
} catch (CartTokenNotFoundException $e) {
$cart = new Cart($context->getSalesChannel()->getTypeId(), $cartToken);
}
return $this->loadByCart($context, $cart, new CartBehavior($context->getPermissions()));
}
public function loadByCart(SalesChannelContext $context, Cart $cart, CartBehavior $behaviorContext): RuleLoaderResult
{
return $this->load($context, $cart, $behaviorContext);
}
public function reset(): void
{
$this->rules = null;
$this->cache->deleteItem(self::CHECKOUT_RULE_LOADER_CACHE_KEY);
}
private function load(SalesChannelContext $context, Cart $cart, CartBehavior $behaviorContext): RuleLoaderResult
{
$rules = $this->loadRules($context->getContext());
// save all rules for later usage
$all = $rules;
// update rules in current context
$context->setRuleIds($rules->getIds());
$iteration = 1;
// start first cart calculation to have all objects enriched
$cart = $this->processor->process($cart, $context, $behaviorContext);
do {
$compare = $cart;
if ($iteration > self::MAX_ITERATION) {
break;
}
// filter rules which matches to current scope
$rules = $rules->filterMatchingRules($cart, $context);
// update matching rules in context
$context->setRuleIds($rules->getIds());
// calculate cart again
$cart = $this->processor->process($cart, $context, $behaviorContext);
// check if the cart changed, in this case we have to recalculate the cart again
$recalculate = $this->cartChanged($cart, $compare);
// check if rules changed for the last calculated cart, in this case we have to recalculate
$ruleCompare = $all->filterMatchingRules($cart, $context);
if (!$rules->equals($ruleCompare)) {
$recalculate = true;
$rules = $ruleCompare;
}
++$iteration;
} while ($recalculate);
$index = 0;
foreach ($rules as $rule) {
++$index;
$this->logger->info(
sprintf('#%s Rule detection: %s with priority %s (id: %s)', $index, $rule->getName(), $rule->getPriority(), $rule->getId())
);
}
$context->setRuleIds($rules->getIds());
// save the cart if errors exist, so the errors get persisted
if ($cart->getErrors()->count() > 0) {
$this->cartPersister->save($cart, $context);
}
return new RuleLoaderResult($cart, $rules);
}
private function loadRules(Context $context): RuleCollection
{
if ($this->rules !== null) {
return $this->rules;
}
$item = $this->cache->getItem(self::CHECKOUT_RULE_LOADER_CACHE_KEY);
$rules = $item->get();
if ($item->isHit() && $rules) {
return $this->rules = $rules;
}
$rules = $this->ruleLoader->load($context);
$item->set($rules);
$this->cache->save($item);
return $this->rules = $rules;
}
private function cartChanged(Cart $previous, Cart $current): bool
{
$previousLineItems = $previous->getLineItems();
$currentLineItems = $current->getLineItems();
return $previousLineItems->count() !== $currentLineItems->count()
|| $previous->getPrice()->getTotalPrice() !== $current->getPrice()->getTotalPrice()
|| $previousLineItems->getKeys() !== $currentLineItems->getKeys()
|| $previousLineItems->getTypes() !== $currentLineItems->getTypes()
;
}
}