Checkout   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 23
lcom 1
cbo 4
dl 0
loc 141
ccs 0
cts 80
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B updateAddress() 0 32 7
A getShippingMethods() 0 18 4
A getPaymentMethods() 0 18 4
A updatePaymentMethod() 0 16 4
A complete() 0 16 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license. See the LICENSE file for details.
8
 */
9
10
namespace FAPI\Sylius\Api;
11
12
use FAPI\Sylius\Exception;
13
use FAPI\Sylius\Exception\InvalidArgumentException;
14
use FAPI\Sylius\Model\Checkout\PaymentCollection;
15
use FAPI\Sylius\Model\Checkout\ShipmentCollection;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * @author Kasim Taskin <[email protected]>
20
 */
21
final class Checkout extends HttpApi
22
{
23
    const SHIPPING_ADDRESS_FIELDS = [
24
        'firstName',
25
        'lastName',
26
        'city',
27
        'postcode',
28
        'street',
29
        'countryCode',
30
    ];
31
32
    /**
33
     * @throws Exception
34
     *
35
     * @return ResponseInterface|void
36
     */
37
    public function updateAddress(int $cartId, array $shippingAddress, bool $differentBillingAddress = false, array $billingAddress = [])
38
    {
39
        if (empty($cartId)) {
40
            throw new InvalidArgumentException('Cart id cannot be empty');
41
        }
42
43
        if (empty($shippingAddress)) {
44
            throw new InvalidArgumentException('Shipping address cannot be empty');
45
        }
46
47
        foreach (self::SHIPPING_ADDRESS_FIELDS as $field) {
48
            if (empty($shippingAddress[$field])) {
49
                throw new InvalidArgumentException(\sprintf('Field "%s" missing in shipping address', $field));
50
            }
51
        }
52
53
        $params = [
54
            'shippingAddress' => $shippingAddress,
55
            'differentBillingAddress' => $differentBillingAddress,
56
            'billingAddress' => $billingAddress,
57
        ];
58
59
        $response = $this->httpPut('/api/v1/checkouts/addressing/'.$cartId, $params);
60
        if (!$this->hydrator) {
61
            return $response;
62
        }
63
64
        // Use any valid status code here
65
        if (204 !== $response->getStatusCode()) {
66
            $this->handleErrors($response);
67
        }
68
    }
69
70
    /**
71
     * @throws Exception
72
     *
73
     * @return ResponseInterface|void
74
     */
75
    public function updatePaymentMethod(int $cartId, array $params = [])
76
    {
77
        if (empty($cartId)) {
78
            throw new InvalidArgumentException('Cart id cannot be empty');
79
        }
80
81
        $response = $this->httpPut('/api/v1/checkouts/select-payment/'.$cartId, $params);
82
        if (!$this->hydrator) {
83
            return $response;
84
        }
85
86
        // Use any valid status code here
87
        if (204 !== $response->getStatusCode()) {
88
            $this->handleErrors($response);
89
        }
90
    }
91
92
    /**
93
     * @throws Exception
94
     *
95
     * @return ResponseInterface|void
96
     */
97
    public function complete(int $cartId)
98
    {
99
        if (empty($cartId)) {
100
            throw new InvalidArgumentException('Cart id cannot be empty');
101
        }
102
103
        $response = $this->httpPut('/api/v1/checkouts/complete/'.$cartId);
104
        if (!$this->hydrator) {
105
            return $response;
106
        }
107
108
        // Use any valid status code here
109
        if (204 !== $response->getStatusCode()) {
110
            $this->handleErrors($response);
111
        }
112
    }
113
114
    /**
115
     * @throws Exception
116
     *
117
     * @return ResponseInterface|ShipmentCollection
118
     */
119
    public function getShippingMethods(int $cartId)
120
    {
121
        if (empty($cartId)) {
122
            throw new InvalidArgumentException('Cart id cannot be empty');
123
        }
124
125
        $response = $this->httpGet('/api/v1/checkouts/select-shipping/'.$cartId);
126
        if (!$this->hydrator) {
127
            return $response;
128
        }
129
130
        // Use any valid status code here
131
        if (200 !== $response->getStatusCode()) {
132
            $this->handleErrors($response);
133
        }
134
135
        return $this->hydrator->hydrate($response, ShipmentCollection::class);
136
    }
137
138
    /**
139
     * @throws Exception
140
     *
141
     * @return PaymentCollection|ResponseInterface
142
     */
143
    public function getPaymentMethods(int $cartId)
144
    {
145
        if (empty($cartId)) {
146
            throw new InvalidArgumentException('Cart id cannot be empty');
147
        }
148
149
        $response = $this->httpGet('/api/v1/checkouts/select-payment/'.$cartId);
150
        if (!$this->hydrator) {
151
            return $response;
152
        }
153
154
        // Use any valid status code here
155
        if (200 !== $response->getStatusCode()) {
156
            $this->handleErrors($response);
157
        }
158
159
        return $this->hydrator->hydrate($response, PaymentCollection::class);
160
    }
161
}
162