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