Completed
Push — master ( afd133...1df073 )
by Kamil
05:39 queued 11s
created

CartCollector   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 108
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasCart() 0 4 1
A getId() 0 4 1
A getTotal() 0 4 1
A getSubtotal() 0 4 1
A getCurrency() 0 4 1
A getLocale() 0 4 1
A getQuantity() 0 4 1
A getItems() 0 4 1
A getStates() 0 4 1
A collect() 0 41 2
A reset() 0 4 1
A getName() 0 4 1
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
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
44
    {
45
        return $this->data['id'];
46
    }
47
48
    public function getTotal(): ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
49
    {
50
        return $this->data['total'];
51
    }
52
53
    public function getSubtotal(): ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
54
    {
55
        return $this->data['subtotal'];
56
    }
57
58
    public function getCurrency(): ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
59
    {
60
        return $this->data['currency'];
61
    }
62
63
    public function getLocale(): ?string
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
64
    {
65
        return $this->data['locale'];
66
    }
67
68
    public function getQuantity(): ?int
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
69
    {
70
        return $this->data['quantity'];
71
    }
72
73
    public function getItems(): ?array
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
74
    {
75
        return $this->data['items'];
76
    }
77
78
    public function getStates(): ?array
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
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