1
|
|
|
<?php |
2
|
|
|
namespace OmnideskBundle\Service; |
3
|
|
|
|
4
|
|
|
use GuzzleHttp\Exception\ClientException; |
5
|
|
|
use OmnideskBundle\Exception\CasesNotFoundException; |
6
|
|
|
use OmnideskBundle\Exception\IncorrectUserEmailException; |
7
|
|
|
use OmnideskBundle\Exception\StaffNotActiveException; |
8
|
|
|
use OmnideskBundle\Factory\Cases\CasesConfigurationFactory; |
9
|
|
|
use OmnideskBundle\Factory\Cases\CasesDataTransformerFactory; |
10
|
|
|
use OmnideskBundle\Request\Cases\AddCasesRequest; |
11
|
|
|
use OmnideskBundle\Request\Cases\EditCasesRequest; |
12
|
|
|
use OmnideskBundle\Request\Cases\ListCasesRequest; |
13
|
|
|
use OmnideskBundle\Request\Cases\ViewCasesRequest; |
14
|
|
|
use OmnideskBundle\Response\Cases\CasesResponse; |
15
|
|
|
use OmnideskBundle\Response\Cases\GetCasesResponse; |
16
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class CasesService |
20
|
|
|
* @package OmnideskBundle\Service |
21
|
|
|
*/ |
22
|
|
|
class CasesService extends AbstractService |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var RequestService |
26
|
|
|
*/ |
27
|
|
|
protected $requestService; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CasesDataTransformerFactory |
31
|
|
|
*/ |
32
|
|
|
protected $transformerFactory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var CasesConfigurationFactory |
36
|
|
|
*/ |
37
|
|
|
protected $configurationFactory; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* CasesService constructor. |
41
|
|
|
* @param RequestService $requestService |
42
|
|
|
* @param CasesDataTransformerFactory $transformerFactory |
43
|
|
|
* @param CasesConfigurationFactory $configurationFactory |
44
|
|
|
*/ |
45
|
|
|
public function __construct( |
46
|
|
|
RequestService $requestService, |
47
|
|
|
CasesDataTransformerFactory $transformerFactory, |
48
|
|
|
CasesConfigurationFactory $configurationFactory |
49
|
|
|
) { |
50
|
|
|
$this->requestService = $requestService; |
51
|
|
|
$this->transformerFactory = $transformerFactory; |
52
|
|
|
$this->configurationFactory = $configurationFactory; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param AddCasesRequest $request |
57
|
|
|
* @return CasesResponse |
58
|
|
|
* @throws IncorrectUserEmailException |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function add(AddCasesRequest $request) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$transformer = $this->transformerFactory->get(CasesDataTransformerFactory::REQUEST_ADD); |
63
|
|
|
$configuration = $this->configurationFactory->get(CasesConfigurationFactory::CONFIGURATION_ADD); |
64
|
|
|
|
65
|
|
|
try { |
66
|
|
|
$params = $this->checkRequest($request, $transformer, $configuration); |
67
|
|
|
} catch (InvalidConfigurationException $exception) { |
68
|
|
|
throw new InvalidConfigurationException($exception->getMessage()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$result = $this->requestService->postMultipart('cases', 'case', $params); |
72
|
|
|
|
73
|
|
|
return $this->transformerFactory->get(CasesDataTransformerFactory::RESPONSE_VIEW)->transform($result); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param EditCasesRequest $request |
78
|
|
|
* @return CasesResponse |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function edit(EditCasesRequest $request) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
$transformer = $this->transformerFactory->get(CasesDataTransformerFactory::REQUEST_EDIT); |
83
|
|
|
$configuration = $this->configurationFactory->get(CasesConfigurationFactory::CONFIGURATION_EDIT); |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$params = $this->checkRequest($request, $transformer, $configuration); |
87
|
|
|
} catch (InvalidConfigurationException $exception) { |
88
|
|
|
throw new InvalidConfigurationException($exception->getMessage()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
try { |
92
|
|
|
$result = $this->requestService->put("cases/{$params['case_id']}", ['case' => $params]); |
93
|
|
|
} catch (ClientException $exception) { |
94
|
|
|
$contents = json_decode($exception->getResponse()->getBody(), JSON_UNESCAPED_UNICODE); |
95
|
|
|
|
96
|
|
|
if ($contents['error'] === CasesResponse::ERROR_STAFF_HAS_NOT_ACCESS) { |
97
|
|
|
throw new StaffNotActiveException(CasesResponse::ERROR_STAFF_HAS_NOT_ACCESS); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
throw $exception; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->transformerFactory->get(CasesDataTransformerFactory::RESPONSE_VIEW)->transform($result); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param ListCasesRequest $request |
108
|
|
|
* @return GetCasesResponse |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
public function lists(ListCasesRequest $request) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$transformer = $this->transformerFactory->get(CasesDataTransformerFactory::REQUEST_LIST); |
113
|
|
|
$configuration = $this->configurationFactory->get(CasesConfigurationFactory::CONFIGURATION_LIST); |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
$params = $this->checkRequest($request, $transformer, $configuration); |
117
|
|
|
} catch (InvalidConfigurationException $exception) { |
118
|
|
|
throw new InvalidConfigurationException($exception->getMessage()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$result = $this->requestService->get('cases', $params); |
122
|
|
|
|
123
|
|
|
return $this->transformerFactory->get(CasesDataTransformerFactory::RESPONSE_LIST)->transform($result); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param ViewCasesRequest $request |
128
|
|
|
* @return CasesResponse |
129
|
|
|
*/ |
130
|
|
View Code Duplication |
public function view(ViewCasesRequest $request) |
|
|
|
|
131
|
|
|
{ |
132
|
|
|
$transformer = $this->transformerFactory->get(CasesDataTransformerFactory::REQUEST_VIEW); |
133
|
|
|
$configuration = $this->configurationFactory->get(CasesConfigurationFactory::CONFIGURATION_VIEW); |
134
|
|
|
|
135
|
|
|
try { |
136
|
|
|
$params = $this->checkRequest($request, $transformer, $configuration); |
137
|
|
|
} catch (InvalidConfigurationException $exception) { |
138
|
|
|
throw new InvalidConfigurationException($exception->getMessage()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$result = $this->requestService->get("cases/{$params['case_id']}"); |
142
|
|
|
|
143
|
|
|
return $this->transformerFactory->get(CasesDataTransformerFactory::RESPONSE_VIEW)->transform($result); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param ViewCasesRequest $request |
148
|
|
|
* @return CasesResponse |
149
|
|
|
* @throws CasesNotFoundException |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function trash(ViewCasesRequest $request) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$transformer = $this->transformerFactory->get(CasesDataTransformerFactory::REQUEST_VIEW); |
154
|
|
|
$configuration = $this->configurationFactory->get(CasesConfigurationFactory::CONFIGURATION_VIEW); |
155
|
|
|
|
156
|
|
|
try { |
157
|
|
|
$params = $this->checkRequest($request, $transformer, $configuration); |
158
|
|
|
} catch (InvalidConfigurationException $exception) { |
159
|
|
|
throw new InvalidConfigurationException($exception->getMessage()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$result = $this->requestService->put("cases/{$params['case_id']}/trash"); |
163
|
|
|
|
164
|
|
|
return $this->transformerFactory->get(CasesDataTransformerFactory::RESPONSE_VIEW)->transform($result); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param ViewCasesRequest $request |
169
|
|
|
* @return CasesResponse |
170
|
|
|
* @throws CasesNotFoundException |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
public function spam(ViewCasesRequest $request) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
$transformer = $this->transformerFactory->get(CasesDataTransformerFactory::REQUEST_VIEW); |
175
|
|
|
$configuration = $this->configurationFactory->get(CasesConfigurationFactory::CONFIGURATION_VIEW); |
176
|
|
|
|
177
|
|
|
try { |
178
|
|
|
$params = $this->checkRequest($request, $transformer, $configuration); |
179
|
|
|
} catch (InvalidConfigurationException $exception) { |
180
|
|
|
throw new InvalidConfigurationException($exception->getMessage()); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$result = $this->requestService->put("cases/{$params['case_id']}/spam"); |
184
|
|
|
|
185
|
|
|
return $this->transformerFactory->get(CasesDataTransformerFactory::RESPONSE_VIEW)->transform($result); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param ViewCasesRequest $request |
190
|
|
|
* @return CasesResponse |
191
|
|
|
* @throws CasesNotFoundException |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
public function restore(ViewCasesRequest $request) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$transformer = $this->transformerFactory->get(CasesDataTransformerFactory::REQUEST_VIEW); |
196
|
|
|
$configuration = $this->configurationFactory->get(CasesConfigurationFactory::CONFIGURATION_VIEW); |
197
|
|
|
|
198
|
|
|
try { |
199
|
|
|
$params = $this->checkRequest($request, $transformer, $configuration); |
200
|
|
|
} catch (InvalidConfigurationException $exception) { |
201
|
|
|
throw new InvalidConfigurationException($exception->getMessage()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$result = $this->requestService->put("cases/{$params['case_id']}/restore"); |
205
|
|
|
|
206
|
|
|
return $this->transformerFactory->get(CasesDataTransformerFactory::RESPONSE_VIEW)->transform($result); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param ViewCasesRequest $request |
211
|
|
|
* @throws CasesNotFoundException |
212
|
|
|
*/ |
213
|
|
View Code Duplication |
public function delete(ViewCasesRequest $request) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$transformer = $this->transformerFactory->get(CasesDataTransformerFactory::REQUEST_VIEW); |
216
|
|
|
$configuration = $this->configurationFactory->get(CasesConfigurationFactory::CONFIGURATION_VIEW); |
217
|
|
|
|
218
|
|
|
try { |
219
|
|
|
$params = $this->checkRequest($request, $transformer, $configuration); |
220
|
|
|
} catch (InvalidConfigurationException $exception) { |
221
|
|
|
throw new InvalidConfigurationException($exception->getMessage()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
try { |
225
|
|
|
$this->requestService->delete("cases/{$params['case_id']}"); |
226
|
|
|
} catch (ClientException $exception) { |
227
|
|
|
throw $exception; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
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.