Completed
Push — master ( bba068...3760af )
by Tobias
02:03
created

Taxon::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.8333
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
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\Product;
11
12
use FAPI\Sylius\Api\HttpApi;
13
use FAPI\Sylius\Exception;
14
use FAPI\Sylius\Model\Product\Taxon as Model;
15
use FAPI\Sylius\Model\Product\TaxonCollection as ModelCollection;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * @author Radoje Albijanic <[email protected]>
20
 */
21
final class Taxon extends HttpApi
22
{
23
    /**
24
     * {@link https://docs.sylius.com/en/1.4/api/taxons.html#collection-of-taxons}.
25
     *
26
     * @throws Exception\DomainException
27
     *
28
     * @return ResponseInterface|ModelCollection
29
     */
30
    public function getAll(array $params = [])
31
    {
32
        $response = $this->httpGet('/api/v1/taxons', $params);
33
        if (!$this->hydrator) {
34
            return $response;
35
        }
36
37
        // Use any valid status code here
38
        if (200 !== $response->getStatusCode()) {
39
            $this->handleErrors($response);
40
        }
41
42
        return $this->hydrator->hydrate($response, ModelCollection::class);
43
    }
44
45
    /**
46
     * {@link https://docs.sylius.com/en/1.4/api/taxons.html#getting-a-single-taxon}.
47
     *
48
     * @throws Exception
49
     *
50
     * @return Model|ResponseInterface
51
     */
52
    public function get(string $code)
53
    {
54
        $response = $this->httpGet(\sprintf('/api/v1/taxons/%s', $code));
55
        if (!$this->hydrator) {
56
            return $response;
57
        }
58
59
        // Use any valid status code here
60
        if (200 !== $response->getStatusCode()) {
61
            $this->handleErrors($response);
62
        }
63
64
        return $this->hydrator->hydrate($response, Model::class);
65
    }
66
67
    /**
68
     * {@link https://docs.sylius.com/en/1.4/api/taxons.html#creating-a-taxon}.
69
     *
70
     * @throws Exception
71
     *
72
     * @return Model|ResponseInterface
73
     */
74 View Code Duplication
    public function create(string $code, 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...
75
    {
76
        $params = $this->validateAndGetParams($code, $params);
77
        $response = $this->httpPost('/api/v1/taxons/', $params);
78
        if (!$this->hydrator) {
79
            return $response;
80
        }
81
82
        // Use any valid status code here
83
        if (201 !== $response->getStatusCode()) {
84
            $this->handleErrors($response);
85
        }
86
87
        return $this->hydrator->hydrate($response, Model::class);
88
    }
89
90
    /**
91
     * Update a taxon partially.
92
     *
93
     * {@link https://docs.sylius.com/en/1.4/api/taxons.html#updating-taxon}
94
     *
95
     * @throws Exception
96
     *
97
     * @return ResponseInterface|void
98
     */
99
    public function update(string $code, array $params = [])
100
    {
101
        $params = $this->validateAndGetParams($code, $params);
102
        $response = $this->httpPatch(\sprintf('/api/v1/taxons/%s', $code), $params);
103
        if (!$this->hydrator) {
104
            return $response;
105
        }
106
107
        // Use any valid status code here
108
        if (204 !== $response->getStatusCode()) {
109
            $this->handleErrors($response);
110
        }
111
    }
112
113
    /**
114
     * {@link https://docs.sylius.com/en/1.4/api/taxons.html#deleting-a-taxon}.
115
     *
116
     * @throws Exception
117
     *
118
     * @return ResponseInterface|void
119
     */
120
    public function delete(string $code)
121
    {
122
        $response = $this->httpDelete(\sprintf('/api/v1/taxons/%s', $code));
123
        if (!$this->hydrator) {
124
            return $response;
125
        }
126
127
        // Use any valid status code here
128
        if (204 !== $response->getStatusCode()) {
129
            $this->handleErrors($response);
130
        }
131
    }
132
133
    private function validateAndGetParams(string $code, array $optionalParams): array
134
    {
135
        if (empty($code)) {
136
            throw new InvalidArgumentException('Code cannot be empty');
137
        }
138
139
        $params = \array_merge([
140
            'code' => $code,
141
        ], $optionalParams);
142
143
        return $params;
144
    }
145
}
146