|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Shopware\Core\Checkout\Cart; |
|
4
|
|
|
|
|
5
|
|
|
use Shopware\Core\Checkout\Cart\Error\ErrorCollection; |
|
6
|
|
|
use Shopware\Core\Checkout\Cart\Exception\CartTokenNotFoundException; |
|
7
|
|
|
use Shopware\Core\Checkout\Cart\Exception\CustomerNotLoggedInException; |
|
8
|
|
|
use Shopware\Core\Checkout\Cart\Exception\InvalidCartException; |
|
9
|
|
|
use Shopware\Core\Framework\HttpException; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @package checkout |
|
14
|
|
|
*/ |
|
15
|
|
|
class CartException extends HttpException |
|
16
|
|
|
{ |
|
17
|
|
|
public const DESERIALIZE_FAILED_CODE = 'CHECKOUT__CART_DESERIALIZE_FAILED'; |
|
18
|
|
|
public const TOKEN_NOT_FOUND_CODE = 'CHECKOUT__CART_TOKEN_NOT_FOUND'; |
|
19
|
|
|
public const CUSTOMER_NOT_LOGGED_IN_CODE = 'CHECKOUT__CUSTOMER_NOT_LOGGED_IN'; |
|
20
|
|
|
public const INSUFFICIENT_PERMISSION_CODE = 'CHECKOUT__INSUFFICIENT_PERMISSION'; |
|
21
|
|
|
public const CART_DELIVERY_NOT_FOUND_CODE = 'CHECKOUT__CART_DELIVERY_POSITION_NOT_FOUND'; |
|
22
|
|
|
public const CART_INVALID_CODE = 'CHECKOUT__CART_INVALID'; |
|
23
|
|
|
public const CART_INVALID_LINE_ITEM_PAYLOAD_CODE = 'CHECKOUT__CART_INVALID_LINE_ITEM_PAYLOAD'; |
|
24
|
|
|
public const CART_INVALID_LINE_ITEM_QUANTITY_CODE = 'CHECKOUT__CART_INVALID_LINE_ITEM_QUANTITY'; |
|
25
|
|
|
public const CART_LINE_ITEM_NOT_FOUND_CODE = 'CHECKOUT__CART_LINE_ITEM_NOT_FOUND'; |
|
26
|
|
|
public const CART_LINE_ITEM_NOT_REMOVABLE_CODE = 'CHECKOUT__CART_LINE_ITEM_NOT_REMOVABLE'; |
|
27
|
|
|
public const CART_LINE_ITEM_NOT_STACKABLE_CODE = 'CHECKOUT__CART_LINE_ITEM_NOT_STACKABLE'; |
|
28
|
|
|
public const CART_LINE_ITEM_TYPE_NOT_SUPPORTED_CODE = 'CHECKOUT__CART_LINE_ITEM_TYPE_NOT_SUPPORTED'; |
|
29
|
|
|
public const CART_MISSING_LINE_ITEM_PRICE_CODE = 'CHECKOUT__CART_MISSING_LINE_ITEM_PRICE'; |
|
30
|
|
|
public const CART_INVALID_PRICE_DEFINITION_CODE = 'CHECKOUT__CART_MISSING_PRICE_DEFINITION'; |
|
31
|
|
|
public const CART_MIXED_LINE_ITEM_TYPE_CODE = 'CHECKOUT__CART_MIXED_LINE_ITEM_TYPE'; |
|
32
|
|
|
public const CART_PAYLOAD_KEY_NOT_FOUND_CODE = 'CHECKOUT__CART_PAYLOAD_KEY_NOT_FOUND'; |
|
33
|
|
|
|
|
34
|
|
|
public static function deserializeFailed(): self |
|
35
|
|
|
{ |
|
36
|
|
|
return new self( |
|
37
|
|
|
Response::HTTP_BAD_REQUEST, |
|
38
|
|
|
self::DESERIALIZE_FAILED_CODE, |
|
39
|
|
|
'Failed to deserialize cart.' |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public static function tokenNotFound(string $token): self |
|
44
|
|
|
{ |
|
45
|
|
|
return new CartTokenNotFoundException(Response::HTTP_NOT_FOUND, self::TOKEN_NOT_FOUND_CODE, 'Cart with token {{ token }} not found.', ['token' => $token]); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function customerNotLoggedIn(): self |
|
49
|
|
|
{ |
|
50
|
|
|
return new CustomerNotLoggedInException( |
|
51
|
|
|
Response::HTTP_FORBIDDEN, |
|
52
|
|
|
self::CUSTOMER_NOT_LOGGED_IN_CODE, |
|
53
|
|
|
'Customer is not logged in.' |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public static function insufficientPermission(): self |
|
58
|
|
|
{ |
|
59
|
|
|
return new self( |
|
60
|
|
|
Response::HTTP_FORBIDDEN, |
|
61
|
|
|
self::INSUFFICIENT_PERMISSION_CODE, |
|
62
|
|
|
'Insufficient permission.' |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return CartException|InvalidCartException |
|
68
|
|
|
*/ |
|
69
|
|
|
public static function invalidCart(ErrorCollection $errors) |
|
70
|
|
|
{ |
|
71
|
|
|
$message = []; |
|
72
|
|
|
foreach ($errors as $error) { |
|
73
|
|
|
$message[] = $error->getId() . ': ' . $error->getMessage(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return new InvalidCartException( |
|
77
|
|
|
Response::HTTP_INTERNAL_SERVER_ERROR, |
|
78
|
|
|
self::CART_INVALID_CODE, |
|
79
|
|
|
'The cart is invalid, got {{ errorCount }} error(s): {{ errors }}', |
|
80
|
|
|
['errorCount' => $errors->count(), 'errors' => implode(\PHP_EOL, $message)] |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public static function invalidChildQuantity(int $childQuantity, int $parentQuantity): self |
|
85
|
|
|
{ |
|
86
|
|
|
return new self( |
|
87
|
|
|
Response::HTTP_BAD_REQUEST, |
|
88
|
|
|
self::CART_INVALID_LINE_ITEM_QUANTITY_CODE, |
|
89
|
|
|
'The quantity of a child "{{ childQuantity }}" must be a multiple of the parent quantity "{{ parentQuantity }}"', |
|
90
|
|
|
['childQuantity' => $childQuantity, 'parentQuantity' => $parentQuantity] |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public static function invalidPayload(string $key, string $id): CartException |
|
95
|
|
|
{ |
|
96
|
|
|
return new self( |
|
97
|
|
|
Response::HTTP_BAD_REQUEST, |
|
98
|
|
|
self::CART_INVALID_LINE_ITEM_PAYLOAD_CODE, |
|
99
|
|
|
'Unable to save payload with key `{{ key }}` on line item `{{ id }}`. Only scalar data types are allowed.', |
|
100
|
|
|
['key' => $key, 'id' => $id] |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public static function invalidQuantity(int $quantity): self |
|
105
|
|
|
{ |
|
106
|
|
|
return new self( |
|
107
|
|
|
Response::HTTP_BAD_REQUEST, |
|
108
|
|
|
self::CART_INVALID_LINE_ITEM_QUANTITY_CODE, |
|
109
|
|
|
'The quantity must be a positive integer. Given: "{{ quantity }}"', |
|
110
|
|
|
['quantity' => $quantity] |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function deliveryNotFound(string $id): self |
|
115
|
|
|
{ |
|
116
|
|
|
return new self( |
|
117
|
|
|
Response::HTTP_NOT_FOUND, |
|
118
|
|
|
self::CART_DELIVERY_NOT_FOUND_CODE, |
|
119
|
|
|
'Delivery with identifier {{ id }} not found.', |
|
120
|
|
|
['id' => $id] |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
public static function lineItemNotFound(string $id): self |
|
125
|
|
|
{ |
|
126
|
|
|
return new self( |
|
127
|
|
|
Response::HTTP_NOT_FOUND, |
|
128
|
|
|
self::CART_LINE_ITEM_NOT_FOUND_CODE, |
|
129
|
|
|
'Line item with identifier {{ id }} not found.', |
|
130
|
|
|
['id' => $id] |
|
131
|
|
|
); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public static function lineItemNotRemovable(string $id): self |
|
135
|
|
|
{ |
|
136
|
|
|
return new self( |
|
137
|
|
|
Response::HTTP_BAD_REQUEST, |
|
138
|
|
|
self::CART_LINE_ITEM_NOT_REMOVABLE_CODE, |
|
139
|
|
|
'Line item with identifier {{ id }} is not removable.', |
|
140
|
|
|
['id' => $id] |
|
141
|
|
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public static function lineItemNotStackable(string $id): self |
|
145
|
|
|
{ |
|
146
|
|
|
return new self( |
|
147
|
|
|
Response::HTTP_BAD_REQUEST, |
|
148
|
|
|
self::CART_LINE_ITEM_NOT_STACKABLE_CODE, |
|
149
|
|
|
'Line item with identifier "{{ id }}" is not stackable and the quantity cannot be changed.', |
|
150
|
|
|
['id' => $id] |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
public static function lineItemTypeNotSupported(string $type): self |
|
155
|
|
|
{ |
|
156
|
|
|
return new self( |
|
157
|
|
|
Response::HTTP_BAD_REQUEST, |
|
158
|
|
|
self::CART_LINE_ITEM_TYPE_NOT_SUPPORTED_CODE, |
|
159
|
|
|
'Line item type "{{ type }}" is not supported.', |
|
160
|
|
|
['type' => $type] |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public static function missingLineItemPrice(string $id): self |
|
165
|
|
|
{ |
|
166
|
|
|
return new self( |
|
167
|
|
|
Response::HTTP_BAD_REQUEST, |
|
168
|
|
|
self::CART_MISSING_LINE_ITEM_PRICE_CODE, |
|
169
|
|
|
'Line item with identifier {{ id }} has no price.', |
|
170
|
|
|
['id' => $id] |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public static function invalidPriceDefinition(): self |
|
175
|
|
|
{ |
|
176
|
|
|
return new self( |
|
177
|
|
|
Response::HTTP_CONFLICT, |
|
178
|
|
|
self::CART_INVALID_PRICE_DEFINITION_CODE, |
|
179
|
|
|
'Provided price definition is invalid.' |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
public static function mixedLineItemType(string $id, string $type): self |
|
184
|
|
|
{ |
|
185
|
|
|
return new self( |
|
186
|
|
|
Response::HTTP_CONFLICT, |
|
187
|
|
|
self::CART_MIXED_LINE_ITEM_TYPE_CODE, |
|
188
|
|
|
'Line item with id {{ id }} already exists with different type {{ type }}.', |
|
189
|
|
|
['id' => $id, 'type' => $type] |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
public static function payloadKeyNotFound(string $key, string $lineItemId): self |
|
194
|
|
|
{ |
|
195
|
|
|
return new self( |
|
196
|
|
|
Response::HTTP_BAD_REQUEST, |
|
197
|
|
|
self::CART_PAYLOAD_KEY_NOT_FOUND_CODE, |
|
198
|
|
|
'Payload key "{{ key }}" in line item "{{ id }}" not found.', |
|
199
|
|
|
['key' => $key, 'id' => $lineItemId] |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|