Passed
Push — trunk ( f001eb...5b7d3d )
by Christian
16:17 queued 05:13
created

CartService::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Core\Checkout\Cart\SalesChannel;
4
5
use Shopware\Core\Checkout\Cart\AbstractCartPersister;
6
use Shopware\Core\Checkout\Cart\Cart;
7
use Shopware\Core\Checkout\Cart\CartCalculator;
8
use Shopware\Core\Checkout\Cart\CartException;
9
use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
10
use Shopware\Core\Checkout\Cart\Event\CartCreatedEvent;
11
use Shopware\Core\Checkout\Cart\LineItem\LineItem;
12
use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
13
use Shopware\Core\Framework\Log\Package;
14
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
15
use Shopware\Core\System\SalesChannel\SalesChannelContext;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Contracts\Service\ResetInterface;
19
20
/**
21
 * @deprecated tag:v6.6.0 - reason:becomes-final - Should not be extended and is only intended as cache
22
 */
23
#[Package('checkout')]
24
class CartService implements ResetInterface
25
{
26
    /**
27
     * @var Cart[]
28
     */
29
    private array $cart = [];
30
31
    /**
32
     * @internal
33
     */
34
    public function __construct(
35
        private readonly AbstractCartPersister $persister,
36
        private readonly EventDispatcherInterface $eventDispatcher,
37
        private readonly CartCalculator $calculator,
38
        private readonly AbstractCartLoadRoute $loadRoute,
39
        private readonly AbstractCartDeleteRoute $deleteRoute,
40
        private readonly AbstractCartItemAddRoute $itemAddRoute,
41
        private readonly AbstractCartItemUpdateRoute $itemUpdateRoute,
42
        private readonly AbstractCartItemRemoveRoute $itemRemoveRoute,
43
        private readonly AbstractCartOrderRoute $orderRoute
44
    ) {
45
    }
46
47
    public function setCart(Cart $cart): void
48
    {
49
        $this->cart[$cart->getToken()] = $cart;
50
    }
51
52
    public function createNew(string $token): Cart
53
    {
54
        $cart = new Cart($token);
55
56
        $this->eventDispatcher->dispatch(new CartCreatedEvent($cart));
57
58
        return $this->cart[$cart->getToken()] = $cart;
59
    }
60
61
    public function getCart(
62
        string $token,
63
        SalesChannelContext $context,
64
        bool $caching = true,
65
        bool $taxed = false
66
    ): Cart {
67
        if ($caching && isset($this->cart[$token])) {
68
            return $this->cart[$token];
69
        }
70
71
        $request = new Request();
72
        $request->query->set('token', $token);
73
        $request->query->set('taxed', $taxed);
74
75
        $cart = $this->loadRoute->load($request, $context)->getCart();
76
77
        return $this->cart[$cart->getToken()] = $cart;
78
    }
79
80
    /**
81
     * @param LineItem|LineItem[] $items
82
     *
83
     * @throws CartException
84
     */
85
    public function add(Cart $cart, LineItem|array $items, SalesChannelContext $context): Cart
86
    {
87
        if ($items instanceof LineItem) {
0 ignored issues
show
introduced by
$items is never a sub-type of Shopware\Core\Checkout\Cart\LineItem\LineItem.
Loading history...
88
            $items = [$items];
89
        }
90
91
        $cart = $this->itemAddRoute->add(new Request(), $cart, $context, $items)->getCart();
92
93
        return $this->cart[$cart->getToken()] = $cart;
94
    }
95
96
    /**
97
     * @throws CartException
98
     */
99
    public function changeQuantity(Cart $cart, string $identifier, int $quantity, SalesChannelContext $context): Cart
100
    {
101
        return $this->update($cart, [
102
            [
103
                'id' => $identifier,
104
                'quantity' => $quantity,
105
            ],
106
        ], $context);
107
    }
108
109
    /**
110
     * @param array<string|int, mixed>[] $items
111
     *
112
     * @throws CartException
113
     */
114
    public function update(Cart $cart, array $items, SalesChannelContext $context): Cart
115
    {
116
        $request = new Request();
117
        $request->request->set('items', $items);
118
119
        $cart = $this->itemUpdateRoute->change($request, $cart, $context)->getCart();
120
121
        return $this->cart[$cart->getToken()] = $cart;
122
    }
123
124
    /**
125
     * @throws CartException
126
     */
127
    public function remove(Cart $cart, string $identifier, SalesChannelContext $context): Cart
128
    {
129
        return $this->removeItems($cart, [$identifier], $context);
130
    }
131
132
    /**
133
     * @param string[] $ids
134
     *
135
     * @throws CartException
136
     */
137
    public function removeItems(Cart $cart, array $ids, SalesChannelContext $context): Cart
138
    {
139
        $request = new Request();
140
        $request->request->set('ids', $ids);
141
142
        $cart = $this->itemRemoveRoute->remove($request, $cart, $context)->getCart();
143
144
        return $this->cart[$cart->getToken()] = $cart;
145
    }
146
147
    /**
148
     * @throws InconsistentCriteriaIdsException
149
     */
150
    public function order(Cart $cart, SalesChannelContext $context, RequestDataBag $data): string
151
    {
152
        $orderId = $this->orderRoute->order($cart, $context, $data)->getOrder()->getId();
153
154
        if (isset($this->cart[$cart->getToken()])) {
155
            unset($this->cart[$cart->getToken()]);
156
        }
157
158
        $cart = $this->createNew($context->getToken());
159
        $this->eventDispatcher->dispatch(new CartChangedEvent($cart, $context));
160
161
        return $orderId;
162
    }
163
164
    public function recalculate(Cart $cart, SalesChannelContext $context): Cart
165
    {
166
        $cart = $this->calculator->calculate($cart, $context);
167
        $this->persister->save($cart, $context);
168
169
        return $cart;
170
    }
171
172
    public function deleteCart(SalesChannelContext $context): void
173
    {
174
        $this->deleteRoute->delete($context);
175
    }
176
177
    public function reset(): void
178
    {
179
        $this->cart = [];
180
    }
181
}
182