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 = []) |
|
|
|
|
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) |
|
|
|
|
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 = []) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$response = $this->httpPatch(sprintf('/api/v1/products/%s/variants/%s', $productCode, $code), $params); |
|
|
|
|
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) |
|
|
|
|
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
|
|
|
|
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.