Passed
Push — trunk ( fdce1b...33e524 )
by Christian
09:59 queued 13s
created

Cart::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Cart;
4
5
use Shopware\Core\Checkout\Cart\Delivery\Struct\DeliveryCollection;
6
use Shopware\Core\Checkout\Cart\Error\Error;
7
use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
8
use Shopware\Core\Checkout\Cart\LineItem\CartDataCollection;
9
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
10
use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
11
use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
12
use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
13
use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
14
use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
15
use Shopware\Core\Checkout\Cart\Transaction\Struct\TransactionCollection;
16
use Shopware\Core\Framework\Struct\StateAwareTrait;
17
use Shopware\Core\Framework\Struct\Struct;
18
19
/**
20
 * @package checkout
21
 */
22
class Cart extends Struct
23
{
24
    use StateAwareTrait;
25
26
    protected string $token;
27
28
    protected CartPrice $price;
29
30
    protected LineItemCollection $lineItems;
31
32
    protected ErrorCollection $errors;
33
34
    protected DeliveryCollection $deliveries;
35
36
    protected TransactionCollection $transactions;
37
38
    protected bool $modified = false;
39
40
    protected ?string $customerComment = null;
41
42
    protected ?string $affiliateCode = null;
43
44
    protected ?string $campaignCode = null;
45
46
    private ?CartDataCollection $data = null;
47
48
    /**
49
     * @var array<string>
50
     */
51
    private array $ruleIds = [];
52
53
    private ?CartBehavior $behavior = null;
54
55
    /**
56
     * @internal
57
     */
58
    public function __construct(string $token)
59
    {
60
        $this->token = $token;
61
        $this->lineItems = new LineItemCollection();
62
        $this->transactions = new TransactionCollection();
63
        $this->errors = new ErrorCollection();
64
        $this->deliveries = new DeliveryCollection();
65
        $this->price = new CartPrice(0, 0, 0, new CalculatedTaxCollection(), new TaxRuleCollection(), CartPrice::TAX_STATE_GROSS);
66
    }
67
68
    public function getToken(): string
69
    {
70
        return $this->token;
71
    }
72
73
    public function setToken(string $token): void
74
    {
75
        $this->token = $token;
76
    }
77
78
    public function getLineItems(): LineItemCollection
79
    {
80
        return $this->lineItems;
81
    }
82
83
    public function setLineItems(LineItemCollection $lineItems): void
84
    {
85
        $this->lineItems = $lineItems;
86
    }
87
88
    public function getErrors(): ErrorCollection
89
    {
90
        return $this->errors;
91
    }
92
93
    public function setErrors(ErrorCollection $errors): void
94
    {
95
        $this->errors = $errors;
96
    }
97
98
    public function getDeliveries(): DeliveryCollection
99
    {
100
        return $this->deliveries;
101
    }
102
103
    public function setDeliveries(DeliveryCollection $deliveries): void
104
    {
105
        $this->deliveries = $deliveries;
106
    }
107
108
    /**
109
     * @throws CartException
110
     */
111
    public function addLineItems(LineItemCollection $lineItems): void
112
    {
113
        foreach ($lineItems as $lineItem) {
114
            $this->add($lineItem);
0 ignored issues
show
Bug introduced by
$lineItem of type array is incompatible with the type Shopware\Core\Checkout\Cart\LineItem\LineItem expected by parameter $lineItem of Shopware\Core\Checkout\Cart\Cart::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
            $this->add(/** @scrutinizer ignore-type */ $lineItem);
Loading history...
115
        }
116
    }
117
118
    public function addDeliveries(DeliveryCollection $deliveries): void
119
    {
120
        foreach ($deliveries as $delivery) {
121
            $this->deliveries->add($delivery);
0 ignored issues
show
Bug introduced by
$delivery of type array is incompatible with the type Shopware\Core\Framework\Struct\TElement expected by parameter $element of Shopware\Core\Framework\Struct\Collection::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
            $this->deliveries->add(/** @scrutinizer ignore-type */ $delivery);
Loading history...
122
        }
123
    }
124
125
    public function addErrors(Error ...$errors): void
126
    {
127
        foreach ($errors as $error) {
128
            $this->errors->add($error);
129
        }
130
    }
131
132
    public function getPrice(): CartPrice
133
    {
134
        return $this->price;
135
    }
136
137
    public function setPrice(CartPrice $price): void
138
    {
139
        $this->price = $price;
140
    }
141
142
    /**
143
     * @throws CartException
144
     */
145
    public function add(LineItem $lineItem): self
146
    {
147
        $this->lineItems->add($lineItem);
148
149
        return $this;
150
    }
151
152
    /**
153
     * @return LineItem|null
154
     */
155
    public function get(string $lineItemKey)
156
    {
157
        return $this->lineItems->get($lineItemKey);
158
    }
159
160
    public function has(string $lineItemKey): bool
161
    {
162
        return $this->lineItems->has($lineItemKey);
163
    }
164
165
    /**
166
     * @throws CartException
167
     */
168
    public function remove(string $key): void
169
    {
170
        $item = $this->get($key);
171
172
        if (!$item) {
173
            throw CartException::lineItemNotFound($key);
174
        }
175
176
        if (!$item->isRemovable()) {
177
            throw CartException::lineItemNotRemovable($key);
178
        }
179
180
        $this->lineItems->remove($key);
181
    }
182
183
    public function getTransactions(): TransactionCollection
184
    {
185
        return $this->transactions;
186
    }
187
188
    public function setTransactions(TransactionCollection $transactions): self
189
    {
190
        $this->transactions = $transactions;
191
192
        return $this;
193
    }
194
195
    public function getShippingCosts(): CalculatedPrice
196
    {
197
        return $this->deliveries->getShippingCosts()->sum();
198
    }
199
200
    public function getData(): CartDataCollection
201
    {
202
        if (!$this->data) {
203
            $this->data = new CartDataCollection();
204
        }
205
206
        return $this->data;
207
    }
208
209
    public function setData(?CartDataCollection $data): void
210
    {
211
        $this->data = $data;
212
    }
213
214
    public function isModified(): bool
215
    {
216
        return $this->modified;
217
    }
218
219
    public function markModified(): void
220
    {
221
        $this->modified = true;
222
    }
223
224
    public function markUnmodified(): void
225
    {
226
        $this->modified = false;
227
    }
228
229
    public function getCustomerComment(): ?string
230
    {
231
        return $this->customerComment;
232
    }
233
234
    public function setCustomerComment(?string $customerComment): void
235
    {
236
        $this->customerComment = $customerComment;
237
    }
238
239
    public function getAffiliateCode(): ?string
240
    {
241
        return $this->affiliateCode;
242
    }
243
244
    public function setAffiliateCode(?string $affiliateCode): void
245
    {
246
        $this->affiliateCode = $affiliateCode;
247
    }
248
249
    public function getCampaignCode(): ?string
250
    {
251
        return $this->campaignCode;
252
    }
253
254
    public function setCampaignCode(?string $campaignCode): void
255
    {
256
        $this->campaignCode = $campaignCode;
257
    }
258
259
    public function getApiAlias(): string
260
    {
261
        return 'cart';
262
    }
263
264
    /**
265
     * @param array<string> $ruleIds
266
     */
267
    public function setRuleIds(array $ruleIds): void
268
    {
269
        $this->ruleIds = $ruleIds;
270
    }
271
272
    /**
273
     * @return array<string>
274
     */
275
    public function getRuleIds(): array
276
    {
277
        return $this->ruleIds;
278
    }
279
280
    /**
281
     * Will be available after the cart gets calculated
282
     * The `\Shopware\Core\Checkout\Cart\Processor::process` will set this
283
     */
284
    public function getBehavior(): ?CartBehavior
285
    {
286
        return $this->behavior;
287
    }
288
289
    /**
290
     * @internal These function is reserved for the `\Shopware\Core\Checkout\Cart\Processor::process`
291
     */
292
    public function setBehavior(?CartBehavior $behavior): void
293
    {
294
        $this->behavior = $behavior;
295
    }
296
}
297