|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sylius\Bundle\CoreBundle\Collector; |
|
15
|
|
|
|
|
16
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
|
17
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
|
18
|
|
|
use Sylius\Component\Order\Context\CartContextInterface; |
|
19
|
|
|
use Sylius\Component\Order\Context\CartNotFoundException; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
22
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @internal |
|
26
|
|
|
*/ |
|
27
|
|
|
final class CartCollector extends DataCollector |
|
28
|
|
|
{ |
|
29
|
|
|
/** @var CartContextInterface */ |
|
30
|
|
|
private $cartContext; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(CartContextInterface $cartContext) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->cartContext = $cartContext; |
|
35
|
|
|
$this->data = []; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function hasCart(): bool |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->data !== []; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getId(): ?int |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
|
|
return $this->data['id']; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function getTotal(): ?int |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
return $this->data['total']; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getSubtotal(): ?int |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
return $this->data['subtotal']; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getCurrency(): ?string |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
return $this->data['currency']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getLocale(): ?string |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
return $this->data['locale']; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getQuantity(): ?int |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
return $this->data['quantity']; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getItems(): ?array |
|
|
|
|
|
|
74
|
|
|
{ |
|
75
|
|
|
return $this->data['items']; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getStates(): ?array |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
return $this->data['states']; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null): void |
|
84
|
|
|
{ |
|
85
|
|
|
try { |
|
86
|
|
|
/** @var OrderInterface $cart */ |
|
87
|
|
|
$cart = $this->cartContext->getCart(); |
|
88
|
|
|
|
|
89
|
|
|
$itemsData = $cart->getItems()->map(static function (OrderItemInterface $item): array { |
|
90
|
|
|
$variant = $item->getVariant(); |
|
91
|
|
|
$product = $variant->getProduct(); |
|
92
|
|
|
|
|
93
|
|
|
return [ |
|
94
|
|
|
'id' => $item->getId(), |
|
95
|
|
|
'variantName' => $variant->getName(), |
|
96
|
|
|
'variantId' => $variant->getId(), |
|
97
|
|
|
'variantCode' => $variant->getCode(), |
|
98
|
|
|
'quantity' => $item->getQuantity(), |
|
99
|
|
|
'productName' => $product->getName(), |
|
100
|
|
|
'productCode' => $product->getCode(), |
|
101
|
|
|
'productId' => $product->getId(), |
|
102
|
|
|
]; |
|
103
|
|
|
})->toArray(); |
|
104
|
|
|
|
|
105
|
|
|
$this->data = [ |
|
106
|
|
|
'id' => $cart->getId(), |
|
107
|
|
|
'total' => $cart->getTotal(), |
|
108
|
|
|
'subtotal' => $cart->getItemsTotal(), |
|
109
|
|
|
'currency' => $cart->getCurrencyCode(), |
|
110
|
|
|
'locale' => $cart->getLocaleCode(), |
|
111
|
|
|
'quantity' => count($cart->getItems()), |
|
112
|
|
|
'items' => $itemsData, |
|
113
|
|
|
'states' => [ |
|
114
|
|
|
'main' => $cart->getState(), |
|
115
|
|
|
'checkout' => $cart->getCheckoutState(), |
|
116
|
|
|
'shipping' => $cart->getShippingState(), |
|
117
|
|
|
'payment' => $cart->getPaymentState(), |
|
118
|
|
|
] |
|
119
|
|
|
]; |
|
120
|
|
|
} catch (CartNotFoundException $exception) { |
|
121
|
|
|
$this->data = []; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function reset(): void |
|
126
|
|
|
{ |
|
127
|
|
|
$this->data = []; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function getName(): string |
|
131
|
|
|
{ |
|
132
|
|
|
return 'sylius_cart'; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.