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->httpClient, $this->hydrator, $this->requestBuilder); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws Exception |
30
|
|
|
* |
31
|
|
|
* @return Model|ResponseInterface |
32
|
|
|
*/ |
33
|
|
|
public function get(string $productCode) |
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
|
|
View Code Duplication |
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 ResponseInterface|void |
79
|
|
|
*/ |
80
|
|
|
public function update(string $productCode, array $params = []) |
81
|
|
|
{ |
82
|
|
|
$response = $this->httpPatch('/api/v1/products/'.$productCode, $params); |
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
|
|
|
public function getAll(array $params = []) |
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
|
|
|
|
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.