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

Product::variant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\Api\Product\Variant;
13
use FAPI\Sylius\Exception;
14
use FAPI\Sylius\Model\Product\Product as Model;
15
use FAPI\Sylius\Model\Product\ProductCollection;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * @author Kasim Taskin <[email protected]>
20
 */
21
final class Product extends HttpApi
22
{
23
    public function variant(): Variant
24
    {
25
        return new Variant($this->getHttpClient(), $this->hydrator, $this->requestBuilder);
0 ignored issues
show
Bug introduced by
The method getHttpClient() does not seem to exist on object<FAPI\Sylius\Api\Product>.

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...
26
    }
27
28
    /**
29
     * @throws Exception
30
     *
31
     * @return Model|ResponseInterface
32
     */
33 View Code Duplication
    public function get(string $productCode)
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...
34
    {
35
        $response = $this->httpGet('/api/v1/products/'.$productCode);
36
        if (!$this->hydrator) {
37
            return $response;
38
        }
39
40
        // Use any valid status code here
41
        if (200 !== $response->getStatusCode()) {
42
            $this->handleErrors($response);
43
        }
44
45
        return $this->hydrator->hydrate($response, Model::class);
46
    }
47
48
    /**
49
     * {@link https://docs.sylius.com/en/1.3/api/products.html#creating-a-product}.
50
     *
51
     * @throws Exception
52
     *
53
     * @return Model|ResponseInterface
54
     */
55
    public function create(string $productCode, array $params = [])
56
    {
57
        $params['code'] = $productCode;
58
        $response = $this->httpPost('/api/v1/products/', $params);
59
        if (!$this->hydrator) {
60
            return $response;
61
        }
62
63
        // Use any valid status code here
64
        if (201 !== $response->getStatusCode()) {
65
            $this->handleErrors($response);
66
        }
67
68
        return $this->hydrator->hydrate($response, Model::class);
69
    }
70
71
    /**
72
     * Update a product partially.
73
     *
74
     * {@link https://docs.sylius.com/en/1.3/api/products.html#id14}
75
     *
76
     * @throws Exception
77
     *
78
     * @return void|ResponseInterface
79
     */
80 View Code Duplication
    public function update(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...
81
    {
82
        $response = $this->httpPatch('/api/v1/products/'.$productCode, $params);
0 ignored issues
show
Bug introduced by
The method httpPatch() does not seem to exist on object<FAPI\Sylius\Api\Product>.

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...
83
        if (!$this->hydrator) {
84
            return $response;
85
        }
86
87
        // Use any valid status code here
88
        if (204 !== $response->getStatusCode()) {
89
            $this->handleErrors($response);
90
        }
91
    }
92
93
    /**
94
     * @throws Exception\DomainException
95
     *
96
     * @return ProductCollection|ResponseInterface
97
     */
98 View Code Duplication
    public function getAll(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->httpGet('/api/v1/products/', $params);
101
        if (!$this->hydrator) {
102
            return $response;
103
        }
104
105
        // Use any valid status code here
106
        if (200 !== $response->getStatusCode()) {
107
            $this->handleErrors($response);
108
        }
109
110
        return $this->hydrator->hydrate($response, ProductCollection::class);
111
    }
112
}
113