|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of PHP CS Fixer. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* Dariusz Rumiński <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* This source file is subject to the MIT license that is bundled |
|
10
|
|
|
* with this source code in the file LICENSE. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Etrias\EwarehousingConnector\Services; |
|
14
|
|
|
|
|
15
|
|
|
use DateTime; |
|
16
|
|
|
use Etrias\EwarehousingConnector\Client\EwarehousingClient; |
|
17
|
|
|
use Etrias\EwarehousingConnector\Response\GetTrackingCodeResponse; |
|
18
|
|
|
use Etrias\EwarehousingConnector\Response\OrderResponse; |
|
19
|
|
|
use Etrias\EwarehousingConnector\Response\SuccessResponse; |
|
20
|
|
|
use Etrias\EwarehousingConnector\Serializer\ServiceTrait; |
|
21
|
|
|
use Etrias\EwarehousingConnector\Types\Address; |
|
22
|
|
|
use Etrias\EwarehousingConnector\Types\Order; |
|
23
|
|
|
use GuzzleHttp\RequestOptions; |
|
24
|
|
|
use JMS\Serializer\SerializerInterface; |
|
25
|
|
|
|
|
26
|
|
|
class OrderService implements OrderServiceInterface |
|
27
|
|
|
{ |
|
28
|
|
|
use ServiceTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var EwarehousingClient |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $client; |
|
34
|
|
|
/** |
|
35
|
|
|
* @var SerializerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $serializer; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* OrderService constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param EwarehousingClient $client |
|
43
|
|
|
* @param SerializerInterface $serializer |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(EwarehousingClient $client, SerializerInterface $serializer) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->client = $client; |
|
48
|
|
|
$this->serializer = $serializer; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
* |
|
54
|
|
|
* @return OrderResponse[] |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getListing( |
|
57
|
|
|
DateTime $from, |
|
58
|
|
|
DateTime $to, |
|
59
|
|
|
$page = 1, |
|
60
|
|
|
$status = null, |
|
61
|
|
|
$sort = null, |
|
62
|
|
|
$direction = null |
|
63
|
|
|
) { |
|
64
|
|
|
$data = [ |
|
65
|
|
|
'from' => $from->format('Y-m-d'), |
|
66
|
|
|
'to' => $to->format('Y-m-d'), |
|
67
|
|
|
'page' => $page, |
|
68
|
|
|
'status' => $status, |
|
69
|
|
|
'sort' => $sort, |
|
70
|
|
|
'direction' => $direction, |
|
71
|
|
|
]; |
|
72
|
|
|
|
|
73
|
|
|
$guzzleResponse = $this->client->get('3/orders', [RequestOptions::QUERY => $data]); |
|
74
|
|
|
|
|
75
|
|
|
return $this->deserializeResponse($guzzleResponse, 'array<'.OrderResponse::class.'>'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritdoc} |
|
80
|
|
|
* |
|
81
|
|
|
* @return OrderResponse |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getOrder($reference) |
|
84
|
|
|
{ |
|
85
|
|
|
$guzzleResponse = $this->client->get('2/orders/order', [RequestOptions::QUERY => ['reference' => $reference]]); |
|
86
|
|
|
|
|
87
|
|
|
return $this->deserializeResponse($guzzleResponse, OrderResponse::class); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
* |
|
93
|
|
|
* @return SuccessResponse; |
|
94
|
|
|
*/ |
|
95
|
|
|
public function addOrder(Order $order) |
|
96
|
|
|
{ |
|
97
|
|
|
$json = $this->serializer->serialize($order, 'json'); |
|
98
|
|
|
$guzzleResponse = $this->client->post('/3/orders/create', [RequestOptions::FORM_PARAMS => json_decode($json, true)]); |
|
99
|
|
|
|
|
100
|
|
|
return $this->deserializeResponse($guzzleResponse, SuccessResponse::class); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
* |
|
106
|
|
|
* @return SuccessResponse; |
|
107
|
|
|
*/ |
|
108
|
|
|
public function updateOrder( |
|
109
|
|
|
$reference, |
|
110
|
|
|
DateTime $date, |
|
111
|
|
|
Address $address = null, |
|
112
|
|
|
array $orderLines = null |
|
113
|
|
|
) { |
|
114
|
|
|
$data = [ |
|
115
|
|
|
'reference' => $reference, |
|
116
|
|
|
'date' => $date->format('Y-m-d'), |
|
117
|
|
|
'address' => $this->serializer->serialize($address, 'array'), |
|
118
|
|
|
'order_lines' => $this->serializer->serialize($orderLines, 'array'), |
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
$guzzleResponse = $this->client->post('/1/orders/update', [RequestOptions::FORM_PARAMS => $data]); |
|
122
|
|
|
|
|
123
|
|
|
return $this->deserializeResponse($guzzleResponse, SuccessResponse::class); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
* |
|
129
|
|
|
* @return SuccessResponse; |
|
130
|
|
|
*/ |
|
131
|
|
View Code Duplication |
public function cancelOrder( |
|
|
|
|
|
|
132
|
|
|
$reference, |
|
133
|
|
|
array $orderLines = [] |
|
134
|
|
|
) { |
|
135
|
|
|
$data = [ |
|
136
|
|
|
'order_lines' => $this->serializer->serialize($orderLines, 'array'), |
|
137
|
|
|
]; |
|
138
|
|
|
|
|
139
|
|
|
$guzzleResponse = $this->client->post('/2/orders/cancel/'.$reference, [RequestOptions::FORM_PARAMS => $data]); |
|
140
|
|
|
|
|
141
|
|
|
return $this->deserializeResponse($guzzleResponse, SuccessResponse::class); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* {@inheritdoc} |
|
146
|
|
|
* |
|
147
|
|
|
* @return GetTrackingCodeResponse[] |
|
148
|
|
|
*/ |
|
149
|
|
|
public function getTrackingCode(array $references) |
|
150
|
|
|
{ |
|
151
|
|
|
$guzzleResponse = $this->client->get('5/orders/tracking', [RequestOptions::QUERY => ['reference' => $references]]); |
|
152
|
|
|
|
|
153
|
|
|
return $this->deserializeResponse($guzzleResponse, 'array<string, '.GetTrackingCodeResponse::class.'>'); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* {@inheritdoc} |
|
158
|
|
|
* |
|
159
|
|
|
* @return SuccessResponse; |
|
160
|
|
|
*/ |
|
161
|
|
|
public function addDocumentToOrder( |
|
162
|
|
|
$reference, |
|
163
|
|
|
$fileContent, |
|
164
|
|
|
$fileName = null, |
|
165
|
|
|
$quantityToPrint = null, |
|
166
|
|
|
$isShippingLabel = false |
|
167
|
|
|
) { |
|
168
|
|
|
|
|
169
|
|
|
if ($fileName === null) { |
|
170
|
|
|
$fileName = $reference . '.pdf'; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$data = [ |
|
174
|
|
|
[ |
|
175
|
|
|
'name' => 'shippingLabel', |
|
176
|
|
|
'contents' => (int) $isShippingLabel |
|
177
|
|
|
], |
|
178
|
|
|
[ |
|
179
|
|
|
'name' => 'file', |
|
180
|
|
|
'contents' => $fileContent, |
|
181
|
|
|
'filename' => $fileName |
|
182
|
|
|
] |
|
183
|
|
|
]; |
|
184
|
|
|
|
|
185
|
|
|
if ($quantityToPrint) { |
|
186
|
|
|
$data[] = [ |
|
187
|
|
|
'name' => 'quantity', |
|
188
|
|
|
'contents' => $quantityToPrint |
|
189
|
|
|
]; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$guzzleResponse = $this->client->post( |
|
193
|
|
|
'/1/orders/document/'.$reference, |
|
194
|
|
|
[ |
|
195
|
|
|
RequestOptions::MULTIPART => $data |
|
196
|
|
|
] |
|
197
|
|
|
); |
|
198
|
|
|
|
|
199
|
|
|
return $this->deserializeResponse($guzzleResponse, SuccessResponse::class); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
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.