Completed
Push — master ( f6940b...5339e4 )
by Tobias
01:46
created

Checkout::updatePaymentMethod()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 24
loc 24
ccs 0
cts 20
cp 0
rs 9.2248
c 0
b 0
f 0
cc 5
nc 5
nop 2
crap 30
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\Domain as DomainExceptions;
14
use FAPI\Sylius\Exception\InvalidArgumentException;
15
use FAPI\Sylius\Model\Checkout\PaymentCollection;
16
use FAPI\Sylius\Model\Checkout\ShipmentCollection;
17
use Psr\Http\Message\ResponseInterface;
18
19
/**
20
 * @author Kasim Taskin <[email protected]>
21
 */
22
final class Checkout extends HttpApi
23
{
24
    const SHIPPING_ADDRESS_FIELDS = [
25
        'firstName',
26
        'lastName',
27
        'city',
28
        'postcode',
29
        'street',
30
        'countryCode',
31
    ];
32
33
    /**
34
     * @throws Exception
35
     *
36
     * @return ResponseInterface|void
37
     */
38
    public function updateAddress(int $cartId, array $shippingAddress, bool $differentBillingAddress = false, array $billingAddress = [])
39
    {
40
        if (empty($cartId)) {
41
            throw new InvalidArgumentException('Cart id cannot be empty');
42
        }
43
44
        if (empty($shippingAddress)) {
45
            throw new InvalidArgumentException('Shipping address cannot be empty');
46
        }
47
48
        foreach (self::SHIPPING_ADDRESS_FIELDS as $field) {
49
            if (empty($shippingAddress[$field])) {
50
                throw new InvalidArgumentException(\sprintf('Field "%s" missing in shipping address', $field));
51
            }
52
        }
53
54
        $params = [
55
            'shippingAddress' => $shippingAddress,
56
            'differentBillingAddress' => $differentBillingAddress,
57
            'billingAddress' => $billingAddress,
58
        ];
59
60
        $response = $this->httpPut('/api/v1/checkouts/addressing/'.$cartId, $params);
61
        if (!$this->hydrator) {
62
            return $response;
63
        }
64
65
        // Use any valid status code here
66
        if (204 !== $response->getStatusCode()) {
67
            switch ($response->getStatusCode()) {
68
                case 400:
69
                    throw new DomainExceptions\ValidationException();
70
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
71
                default:
72
                    $this->handleErrors($response);
73
74
                    break;
75
            }
76
        }
77
    }
78
79
    /**
80
     * @throws Exception
81
     *
82
     * @return ResponseInterface|void
83
     */
84 View Code Duplication
    public function updatePaymentMethod(int $cartId, array $params = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        if (empty($cartId)) {
87
            throw new InvalidArgumentException('Cart id cannot be empty');
88
        }
89
90
        $response = $this->httpPut('/api/v1/checkouts/select-payment/'.$cartId, $params);
91
        if (!$this->hydrator) {
92
            return $response;
93
        }
94
95
        // Use any valid status code here
96
        if (204 !== $response->getStatusCode()) {
97
            switch ($response->getStatusCode()) {
98
                case 400:
99
                    throw new DomainExceptions\ValidationException();
100
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
101
                default:
102
                    $this->handleErrors($response);
103
104
                    break;
105
            }
106
        }
107
    }
108
109
    /**
110
     * @throws Exception
111
     *
112
     * @return ResponseInterface|void
113
     */
114 View Code Duplication
    public function complete(int $cartId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
115
    {
116
        if (empty($cartId)) {
117
            throw new InvalidArgumentException('Cart id cannot be empty');
118
        }
119
120
        $response = $this->httpPut('/api/v1/checkouts/complete/'.$cartId);
121
        if (!$this->hydrator) {
122
            return $response;
123
        }
124
125
        // Use any valid status code here
126
        if (204 !== $response->getStatusCode()) {
127
            switch ($response->getStatusCode()) {
128
                case 400:
129
                    throw new DomainExceptions\ValidationException();
130
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
131
                default:
132
                    $this->handleErrors($response);
133
134
                    break;
135
            }
136
        }
137
    }
138
139
    /**
140
     * @throws Exception
141
     *
142
     * @return ResponseInterface|ShipmentCollection
143
     */
144 View Code Duplication
    public function getShippingMethods(int $cartId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
    {
146
        if (empty($cartId)) {
147
            throw new InvalidArgumentException('Cart id cannot be empty');
148
        }
149
150
        $response = $this->httpGet('/api/v1/checkouts/select-shipping/'.$cartId);
151
        if (!$this->hydrator) {
152
            return $response;
153
        }
154
155
        // Use any valid status code here
156
        if (200 !== $response->getStatusCode()) {
157
            switch ($response->getStatusCode()) {
158
                case 400:
159
                    throw new DomainExceptions\ValidationException();
160
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
161
                default:
162
                    $this->handleErrors($response);
163
164
                    break;
165
            }
166
        }
167
168
        return $this->hydrator->hydrate($response, ShipmentCollection::class);
169
    }
170
171
    /**
172
     * @throws Exception
173
     *
174
     * @return PaymentCollection|ResponseInterface
175
     */
176 View Code Duplication
    public function getPaymentMethods(int $cartId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        if (empty($cartId)) {
179
            throw new InvalidArgumentException('Cart id cannot be empty');
180
        }
181
182
        $response = $this->httpGet('/api/v1/checkouts/select-payment/'.$cartId);
183
        if (!$this->hydrator) {
184
            return $response;
185
        }
186
187
        // Use any valid status code here
188
        if (200 !== $response->getStatusCode()) {
189
            switch ($response->getStatusCode()) {
190
                case 400:
191
                    throw new DomainExceptions\ValidationException();
192
                    break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
193
                default:
194
                    $this->handleErrors($response);
195
196
                    break;
197
            }
198
        }
199
200
        return $this->hydrator->hydrate($response, PaymentCollection::class);
201
    }
202
}
203