Completed
Push — master ( decb47...853263 )
by Tobias
02:04
created

Variant::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 14
loc 14
ccs 0
cts 11
cp 0
rs 9.7998
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\Variant as Model;
15
use FAPI\Sylius\Model\Product\VariantCollection;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * @author Kasim Taskin <[email protected]>
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
final class Variant extends HttpApi
23
{
24
    /**
25
     * @throws Exception\DomainException
26
     *
27
     * @return ResponseInterface|VariantCollection
28
     */
29 View Code Duplication
    public function getAll(string $productCode, 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...
30
    {
31
        $response = $this->httpGet(\sprintf('/api/v1/products/%s/variants/', $productCode), $params);
32
        if (!$this->hydrator) {
33
            return $response;
34
        }
35
36
        // Use any valid status code here
37
        if (200 !== $response->getStatusCode()) {
38
            $this->handleErrors($response);
39
        }
40
41
        return $this->hydrator->hydrate($response, VariantCollection::class);
42
    }
43
44
    /**
45
     * {@link https://docs.sylius.com/en/1.3/api/product_variants.html#getting-a-single-product-variant}.
46
     *
47
     * @throws Exception
48
     *
49
     * @return Model|ResponseInterface
50
     */
51 View Code Duplication
    public function get(string $productCode, string $code)
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...
52
    {
53
        $response = $this->httpGet(sprintf('/api/v1/products/%s/variants/%s', $productCode, $code));
54
        if (!$this->hydrator) {
55
            return $response;
56
        }
57
58
        // Use any valid status code here
59
        if (200 !== $response->getStatusCode()) {
60
            $this->handleErrors($response);
61
        }
62
63
        return $this->hydrator->hydrate($response, Model::class);
64
    }
65
66
    /**
67
     * {@link https://docs.sylius.com/en/1.3/api/product_variants.html#creating-a-product-variant}.
68
     *
69
     * @throws Exception
70
     *
71
     * @return Model|ResponseInterface
72
     */
73
    public function create(string $productCode, string $code, array $params = [])
74
    {
75
        $params['code'] = $code;
76
        $response = $this->httpPost(sprintf('/api/v1/products/%s/variants/', $productCode), $params);
77
        if (!$this->hydrator) {
78
            return $response;
79
        }
80
81
        // Use any valid status code here
82
        if (201 !== $response->getStatusCode()) {
83
            $this->handleErrors($response);
84
        }
85
86
        return $this->hydrator->hydrate($response, Model::class);
87
    }
88
89
    /**
90
     * Update a product partially.
91
     *
92
     * {@link https://docs.sylius.com/en/1.3/api/product_variants.html#updating-product-variant}
93
     *
94
     * @throws Exception
95
     *
96
     * @return void|ResponseInterface
97
     */
98 View Code Duplication
    public function update(string $productCode, 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...
99
    {
100
        $response = $this->httpPatch(sprintf('/api/v1/products/%s/variants/%s', $productCode, $code), $params);
0 ignored issues
show
Bug introduced by
The method httpPatch() does not seem to exist on object<FAPI\Sylius\Api\Product\Variant>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
101
        if (!$this->hydrator) {
102
            return $response;
103
        }
104
105
        // Use any valid status code here
106
        if (204 !== $response->getStatusCode()) {
107
            $this->handleErrors($response);
108
        }
109
    }
110
111
    /**
112
     * {@link https://docs.sylius.com/en/1.3/api/product_variants.html#deleting-a-product-variant}.
113
     *
114
     * @throws Exception
115
     *
116
     * @return void|ResponseInterface
117
     */
118 View Code Duplication
    public function delete(string $productCode, string $code)
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...
119
    {
120
        $response = $this->httpDelete(sprintf('/api/v1/products/%s/variants/%s', $productCode, $code));
121
        if (!$this->hydrator) {
122
            return $response;
123
        }
124
125
        // Use any valid status code here
126
        if (204 !== $response->getStatusCode()) {
127
            $this->handleErrors($response);
128
        }
129
    }
130
}
131