1
|
|
|
<?php |
2
|
|
|
namespace SafeCrow\Api; |
3
|
|
|
|
4
|
|
|
use SafeCrow\DataTransformer\OrderDataTransformer; |
5
|
|
|
use SafeCrow\Model\Order; |
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class OrderApi |
10
|
|
|
* @package SafeCrow\Api |
11
|
|
|
*/ |
12
|
|
|
class OrderApi extends AbstractApi |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param array $params |
16
|
|
|
* @return Order |
17
|
|
|
*/ |
18
|
|
|
public function add(array $params) : Order |
19
|
|
|
{ |
20
|
|
|
$resolver = new OptionsResolver(); |
21
|
|
|
$resolver |
22
|
|
|
->setRequired([ |
23
|
|
|
'consumer_id', |
24
|
|
|
'supplier_id', |
25
|
|
|
'price', |
26
|
|
|
'description', |
27
|
|
|
'service_cost_payer', |
28
|
|
|
'extra', |
29
|
|
|
]) |
30
|
|
|
->setDefaults([ |
31
|
|
|
'extra' => null, |
32
|
|
|
]) |
33
|
|
|
->setAllowedTypes('consumer_id', 'int') |
34
|
|
|
->setAllowedTypes('supplier_id', 'int') |
35
|
|
|
->setAllowedTypes('price', 'int') |
36
|
|
|
->setAllowedTypes('description', 'string') |
37
|
|
|
->setAllowedTypes('service_cost_payer', 'string') |
38
|
|
|
->setAllowedTypes('extra', ['array', 'null']) |
39
|
|
|
->setAllowedValues('service_cost_payer', [Order::PAYER_HALF, Order::PAYER_CONSUMER, Order::PAYER_SUPPLIER]); |
40
|
|
|
|
41
|
|
|
$params = $resolver->resolve($params); |
42
|
|
|
|
43
|
|
|
$response = $this->post('/orders', $params); |
44
|
|
|
|
45
|
|
|
return $this->getResult($response, new OrderDataTransformer()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function all(): array |
52
|
|
|
{ |
53
|
|
|
$response = $this->get('/orders'); |
54
|
|
|
|
55
|
|
|
return $this->getArrayResult($response, new OrderDataTransformer()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param int $id |
60
|
|
|
* @return Order |
61
|
|
|
*/ |
62
|
|
|
public function show(int $id) : Order |
63
|
|
|
{ |
64
|
|
|
$response = $this->get('/orders/'.$id); |
65
|
|
|
|
66
|
|
|
return $this->getResult($response, new OrderDataTransformer()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param int $id |
71
|
|
|
* @param array $params |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function pay(int $id, array $params) : string |
75
|
|
|
{ |
76
|
|
|
$resolver = new OptionsResolver(); |
77
|
|
|
$resolver |
78
|
|
|
->setRequired([ |
79
|
|
|
'redirect_url', |
80
|
|
|
]) |
81
|
|
|
->setAllowedTypes('redirect_url', 'string'); |
82
|
|
|
|
83
|
|
|
$params = $resolver->resolve($params); |
84
|
|
|
|
85
|
|
|
$response = $this->post('/orders/'.$id.'/pay', $params); |
86
|
|
|
|
87
|
|
|
return $this->getSingleResult($response, 'redirect_url'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param int $id |
92
|
|
|
* @param array $params |
93
|
|
|
* @return Order |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function annul(int $id, array $params) : Order |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$resolver = new OptionsResolver(); |
98
|
|
|
$resolver |
99
|
|
|
->setRequired([ |
100
|
|
|
'reason', |
101
|
|
|
]) |
102
|
|
|
->setAllowedTypes('reason', 'string'); |
103
|
|
|
|
104
|
|
|
$params = $resolver->resolve($params); |
105
|
|
|
|
106
|
|
|
$response = $this->post('/orders/'.$id.'/annul', $params); |
107
|
|
|
|
108
|
|
|
return $this->getResult($response, new OrderDataTransformer()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param int $id |
113
|
|
|
* @param array $params |
114
|
|
|
* @return Order |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function cancel(int $id, array $params): Order |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$resolver = new OptionsResolver(); |
119
|
|
|
$resolver |
120
|
|
|
->setRequired([ |
121
|
|
|
'reason', |
122
|
|
|
]) |
123
|
|
|
->setAllowedTypes('reason', 'string'); |
124
|
|
|
|
125
|
|
|
$params = $resolver->resolve($params); |
126
|
|
|
|
127
|
|
|
$response = $this->post('/orders/'.$id.'/cancel', $params); |
128
|
|
|
|
129
|
|
|
return $this->getResult($response, new OrderDataTransformer()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param int $id |
134
|
|
|
* @param array $params |
135
|
|
|
* @return Order |
136
|
|
|
*/ |
137
|
|
|
public function close(int $id, array $params) : Order |
138
|
|
|
{ |
139
|
|
|
$resolver = new OptionsResolver(); |
140
|
|
|
$resolver |
141
|
|
|
->setRequired([ |
142
|
|
|
'reason', |
143
|
|
|
]) |
144
|
|
|
->setAllowedTypes('reason', 'string'); |
145
|
|
|
|
146
|
|
|
$params = $resolver->resolve($params); |
147
|
|
|
|
148
|
|
|
$response = $this->post('/orders/'.$id.'/close', $params); |
149
|
|
|
|
150
|
|
|
return $this->getResult($response, new OrderDataTransformer()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param int $id |
155
|
|
|
* @param array $params |
156
|
|
|
* @return Order |
157
|
|
|
*/ |
158
|
|
|
public function escalate(int $id, array $params) : Order |
159
|
|
|
{ |
160
|
|
|
$resolver = new OptionsResolver(); |
161
|
|
|
$resolver |
162
|
|
|
->setRequired([ |
163
|
|
|
'reason', |
164
|
|
|
]) |
165
|
|
|
->setAllowedTypes('reason', 'string'); |
166
|
|
|
|
167
|
|
|
$params = $resolver->resolve($params); |
168
|
|
|
|
169
|
|
|
$response = $this->post('/orders/'.$id.'/escalate', $params); |
170
|
|
|
|
171
|
|
|
return $this->getResult($response, new OrderDataTransformer()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param int $userId |
176
|
|
|
* @param int $orderId |
177
|
|
|
* @param array $params |
178
|
|
|
* @return Order |
179
|
|
|
*/ |
180
|
|
View Code Duplication |
public function bind(int $userId, int $orderId, array $params) : Order |
|
|
|
|
181
|
|
|
{ |
182
|
|
|
$resolver = new OptionsResolver(); |
183
|
|
|
$resolver |
184
|
|
|
->setRequired([ |
185
|
|
|
'card_id', |
186
|
|
|
]) |
187
|
|
|
->setAllowedTypes('card_id', 'int'); |
188
|
|
|
|
189
|
|
|
$params = $resolver->resolve($params); |
190
|
|
|
|
191
|
|
|
$response = $this->post('/users/'.$userId.'/orders/'.$orderId.'/bind_card', $params); |
192
|
|
|
|
193
|
|
|
return $this->getResult($response, new OrderDataTransformer()); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
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.