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