| Total Complexity | 45 |
| Total Lines | 289 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Cart often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Cart, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 20 | #[Package('checkout')] |
||
| 21 | class Cart extends Struct |
||
| 22 | { |
||
| 23 | use StateAwareTrait; |
||
| 24 | |||
| 25 | protected CartPrice $price; |
||
| 26 | |||
| 27 | protected LineItemCollection $lineItems; |
||
| 28 | |||
| 29 | protected ErrorCollection $errors; |
||
| 30 | |||
| 31 | protected DeliveryCollection $deliveries; |
||
| 32 | |||
| 33 | protected TransactionCollection $transactions; |
||
| 34 | |||
| 35 | protected bool $modified = false; |
||
| 36 | |||
| 37 | protected ?string $customerComment = null; |
||
| 38 | |||
| 39 | protected ?string $affiliateCode = null; |
||
| 40 | |||
| 41 | protected ?string $campaignCode = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * This can be used to identify carts, that are used for different purposes. |
||
| 45 | * Setting this will call different hook names for respective cart sources. |
||
| 46 | * This may be used for multi-cart or subscription applications, |
||
| 47 | * where the regular calculation process is not desired and separate calculation processes are used as an opt-in usage. |
||
| 48 | */ |
||
| 49 | protected ?string $source = null; |
||
| 50 | |||
| 51 | private ?CartDataCollection $data = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array<string> |
||
| 55 | */ |
||
| 56 | private array $ruleIds = []; |
||
| 57 | |||
| 58 | private ?CartBehavior $behavior = null; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @internal |
||
| 62 | */ |
||
| 63 | public function __construct(protected string $token) |
||
| 64 | { |
||
| 65 | $this->lineItems = new LineItemCollection(); |
||
| 66 | $this->transactions = new TransactionCollection(); |
||
| 67 | $this->errors = new ErrorCollection(); |
||
| 68 | $this->deliveries = new DeliveryCollection(); |
||
| 69 | $this->price = new CartPrice(0, 0, 0, new CalculatedTaxCollection(), new TaxRuleCollection(), CartPrice::TAX_STATE_GROSS); |
||
| 70 | } |
||
| 71 | |||
| 72 | public function getToken(): string |
||
| 73 | { |
||
| 74 | return $this->token; |
||
| 75 | } |
||
| 76 | |||
| 77 | public function setToken(string $token): void |
||
| 78 | { |
||
| 79 | $this->token = $token; |
||
| 80 | } |
||
| 81 | |||
| 82 | public function getLineItems(): LineItemCollection |
||
| 83 | { |
||
| 84 | return $this->lineItems; |
||
| 85 | } |
||
| 86 | |||
| 87 | public function setLineItems(LineItemCollection $lineItems): void |
||
| 88 | { |
||
| 89 | $this->lineItems = $lineItems; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function getErrors(): ErrorCollection |
||
| 93 | { |
||
| 94 | return $this->errors; |
||
| 95 | } |
||
| 96 | |||
| 97 | public function setErrors(ErrorCollection $errors): void |
||
| 98 | { |
||
| 99 | $this->errors = $errors; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function getDeliveries(): DeliveryCollection |
||
| 103 | { |
||
| 104 | return $this->deliveries; |
||
| 105 | } |
||
| 106 | |||
| 107 | public function setDeliveries(DeliveryCollection $deliveries): void |
||
| 108 | { |
||
| 109 | $this->deliveries = $deliveries; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @throws CartException |
||
| 114 | */ |
||
| 115 | public function addLineItems(LineItemCollection $lineItems): void |
||
| 116 | { |
||
| 117 | foreach ($lineItems as $lineItem) { |
||
| 118 | $this->add($lineItem); |
||
|
|
|||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function addDeliveries(DeliveryCollection $deliveries): void |
||
| 123 | { |
||
| 124 | foreach ($deliveries as $delivery) { |
||
| 125 | $this->deliveries->add($delivery); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | public function addErrors(Error ...$errors): void |
||
| 130 | { |
||
| 131 | foreach ($errors as $error) { |
||
| 132 | $this->errors->add($error); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | public function getPrice(): CartPrice |
||
| 137 | { |
||
| 138 | return $this->price; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function setPrice(CartPrice $price): void |
||
| 142 | { |
||
| 143 | $this->price = $price; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @throws CartException |
||
| 148 | */ |
||
| 149 | public function add(LineItem $lineItem): self |
||
| 150 | { |
||
| 151 | $this->lineItems->add($lineItem); |
||
| 152 | |||
| 153 | return $this; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return LineItem|null |
||
| 158 | */ |
||
| 159 | public function get(string $lineItemKey) |
||
| 160 | { |
||
| 161 | return $this->lineItems->get($lineItemKey); |
||
| 162 | } |
||
| 163 | |||
| 164 | public function has(string $lineItemKey): bool |
||
| 165 | { |
||
| 166 | return $this->lineItems->has($lineItemKey); |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @throws CartException |
||
| 171 | */ |
||
| 172 | public function remove(string $key): void |
||
| 173 | { |
||
| 174 | $item = $this->get($key); |
||
| 175 | |||
| 176 | if (!$item) { |
||
| 177 | throw CartException::lineItemNotFound($key); |
||
| 178 | } |
||
| 179 | |||
| 180 | if (!$item->isRemovable()) { |
||
| 181 | throw CartException::lineItemNotRemovable($key); |
||
| 182 | } |
||
| 183 | |||
| 184 | $this->lineItems->remove($key); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function getTransactions(): TransactionCollection |
||
| 188 | { |
||
| 189 | return $this->transactions; |
||
| 190 | } |
||
| 191 | |||
| 192 | public function setTransactions(TransactionCollection $transactions): self |
||
| 197 | } |
||
| 198 | |||
| 199 | public function getShippingCosts(): CalculatedPrice |
||
| 200 | { |
||
| 201 | return $this->deliveries->getShippingCosts()->sum(); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getData(): CartDataCollection |
||
| 205 | { |
||
| 206 | if (!$this->data) { |
||
| 207 | $this->data = new CartDataCollection(); |
||
| 208 | } |
||
| 209 | |||
| 210 | return $this->data; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function setData(?CartDataCollection $data): void |
||
| 214 | { |
||
| 215 | $this->data = $data; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function isModified(): bool |
||
| 219 | { |
||
| 220 | return $this->modified; |
||
| 221 | } |
||
| 222 | |||
| 223 | public function markModified(): void |
||
| 224 | { |
||
| 225 | $this->modified = true; |
||
| 226 | } |
||
| 227 | |||
| 228 | public function markUnmodified(): void |
||
| 229 | { |
||
| 230 | $this->modified = false; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function getCustomerComment(): ?string |
||
| 234 | { |
||
| 235 | return $this->customerComment; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function setCustomerComment(?string $customerComment): void |
||
| 239 | { |
||
| 240 | $this->customerComment = $customerComment; |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getAffiliateCode(): ?string |
||
| 244 | { |
||
| 245 | return $this->affiliateCode; |
||
| 246 | } |
||
| 247 | |||
| 248 | public function setAffiliateCode(?string $affiliateCode): void |
||
| 249 | { |
||
| 250 | $this->affiliateCode = $affiliateCode; |
||
| 251 | } |
||
| 252 | |||
| 253 | public function getCampaignCode(): ?string |
||
| 254 | { |
||
| 255 | return $this->campaignCode; |
||
| 256 | } |
||
| 257 | |||
| 258 | public function setCampaignCode(?string $campaignCode): void |
||
| 259 | { |
||
| 260 | $this->campaignCode = $campaignCode; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function getApiAlias(): string |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param array<string> $ruleIds |
||
| 270 | */ |
||
| 271 | public function setRuleIds(array $ruleIds): void |
||
| 272 | { |
||
| 273 | $this->ruleIds = $ruleIds; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @return array<string> |
||
| 278 | */ |
||
| 279 | public function getRuleIds(): array |
||
| 280 | { |
||
| 281 | return $this->ruleIds; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Will be available after the cart gets calculated |
||
| 286 | * The `\Shopware\Core\Checkout\Cart\Processor::process` will set this |
||
| 287 | */ |
||
| 288 | public function getBehavior(): ?CartBehavior |
||
| 289 | { |
||
| 290 | return $this->behavior; |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @internal These function is reserved for the `\Shopware\Core\Checkout\Cart\Processor::process` |
||
| 295 | */ |
||
| 296 | public function setBehavior(?CartBehavior $behavior): void |
||
| 297 | { |
||
| 298 | $this->behavior = $behavior; |
||
| 299 | } |
||
| 300 | |||
| 301 | public function getSource(): ?string |
||
| 304 | } |
||
| 305 | |||
| 306 | public function setSource(?string $source): void |
||
| 307 | { |
||
| 308 | $this->source = $source; |
||
| 309 | } |
||
| 310 | } |
||
| 311 |