Completed
Push — master ( a1ce72...051ac9 )
by Tobias
33:52 queued 23:54
created

Cart::get()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.6333
c 0
b 0
f 0
cc 4
nc 4
nop 1
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\Cart\Cart as Model;
16
use FAPI\Sylius\Model\Cart\CartItem;
17
use Psr\Http\Message\ResponseInterface;
18
19
/**
20
 * @author Kasim Taskin <[email protected]>
21
 */
22
final class Cart extends HttpApi
23
{
24
    /**
25
     * @param int $id
26
     *
27
     * @throws Exception
28
     *
29
     * @return Cart|ResponseInterface
30
     */
31 View Code Duplication
    public function get(int $id)
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...
32
    {
33
        if (empty($id)) {
34
            throw new InvalidArgumentException('Id cannot be empty');
35
        }
36
37
        $response = $this->httpGet("/api/v1/carts/{$id}");
38
39
        if (!$this->hydrator) {
40
            return $response;
41
        }
42
43
        // Use any valid status code here
44
        if (200 !== $response->getStatusCode()) {
45
            $this->handleErrors($response);
46
        }
47
48
        return $this->hydrator->hydrate($response, Model::class);
49
    }
50
51
    /**
52
     * @param string $message
0 ignored issues
show
Bug introduced by
There is no parameter named $message. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
53
     * @param string $location
0 ignored issues
show
Bug introduced by
There is no parameter named $location. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
54
     * @param array  $hashtags
0 ignored issues
show
Bug introduced by
There is no parameter named $hashtags. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
55
     *
56
     * @throws Exception
57
     *
58
     * @return Cart|ResponseInterface
59
     */
60 View Code Duplication
    public function create(string $customer, string $channel, string $localeCode)
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...
61
    {
62
        if (empty($customer)) {
63
            throw new InvalidArgumentException('Customers field cannot be empty');
64
        }
65
66
        if (empty($channel)) {
67
            throw new InvalidArgumentException('Channel cannot be empty');
68
        }
69
70
        if (empty($localeCode)) {
71
            throw new InvalidArgumentException('Locale code cannot be empty');
72
        }
73
74
        $params = [
75
            'customer' => $customer,
76
            'channel' => $channel,
77
            'localeCode' => $localeCode,
78
        ];
79
80
        $response = $this->httpPost('/api/v1/carts/', $params);
81
82
        if (!$this->hydrator) {
83
            return $response;
84
        }
85
86
        // Use any valid status code here
87
        if (201 !== $response->getStatusCode()) {
88
            switch ($response->getStatusCode()) {
89
                case 400:
90
                    throw new DomainExceptions\ValidationException();
91
                    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...
92
                default:
93
                    $this->handleErrors($response);
94
95
                    break;
96
            }
97
        }
98
99
        return $this->hydrator->hydrate($response, Model::class);
100
    }
101
102
    /**
103
     * @param int    $cartId
104
     * @param string $variant
105
     * @param int    $quantity
106
     *
107
     * @throws Exception\DomainException
108
     * @throws Exception\Domain\ValidationException
109
     *
110
     * @return CartItem|ResponseInterface
111
     */
112 View Code Duplication
    public function addItem(int $cartId, string $variant, int $quantity)
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...
113
    {
114
        if (empty($cartId)) {
115
            throw new InvalidArgumentException('Cart id field cannot be empty');
116
        }
117
118
        if (empty($variant)) {
119
            throw new InvalidArgumentException('variant cannot be empty');
120
        }
121
122
        if (empty($quantity)) {
123
            throw new InvalidArgumentException('quantity cannot be empty');
124
        }
125
126
        $params = [
127
            'variant' => $variant,
128
            'quantity' => $quantity,
129
        ];
130
131
        $response = $this->httpPost("/api/v1/carts/{$cartId}/items/", $params);
132
133
        if (!$this->hydrator) {
134
            return $response;
135
        }
136
137
        // Use any valid status code here
138
        if (201 !== $response->getStatusCode()) {
139
            switch ($response->getStatusCode()) {
140
                case 400:
141
                    throw new DomainExceptions\ValidationException();
142
                    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...
143
                default:
144
                    $this->handleErrors($response);
145
146
                    break;
147
            }
148
        }
149
150
        return $this->hydrator->hydrate($response, CartItem::class);
151
    }
152
}
153