UserService::edit()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace OmnideskBundle\Service;
3
4
use GuzzleHttp\Exception\ClientException;
5
use OmnideskBundle\Exception\UserAlreadyExistException;
6
use OmnideskBundle\Exception\UserWrongEmailException;
7
use OmnideskBundle\Factory\User\UserConfigurationFactory;
8
use OmnideskBundle\Factory\User\UserDataTransformerFactory;
9
use OmnideskBundle\Request\User\AddUserRequest;
10
use OmnideskBundle\Request\User\EditUserRequest;
11
use OmnideskBundle\Request\User\ListUserRequest;
12
use OmnideskBundle\Request\User\ViewUserRequest;
13
use OmnideskBundle\Response\User\ListUserResponse;
14
use OmnideskBundle\Response\User\UserResponse;
15
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
16
17
/**
18
 * Class UserService
19
 * @package OmnideskBundle\Service
20
 */
21
class UserService extends AbstractService
22
{
23
    /**
24
     * @var RequestService
25
     */
26
    protected $requestService;
27
28
    /**
29
     * @var UserDataTransformerFactory
30
     */
31
    protected $transformerFactory;
32
33
    /**
34
     * @var UserConfigurationFactory
35
     */
36
    protected $configurationFactory;
37
38
    /**
39
     * UserService constructor.
40
     * @param RequestService              $requestService
41
     * @param UserDataTransformerFactory $transformerFactory
42
     * @param UserConfigurationFactory   $configurationFactory
43
     */
44
    public function __construct(
45
        RequestService $requestService,
46
        UserDataTransformerFactory $transformerFactory,
47
        UserConfigurationFactory $configurationFactory
48
    ) {
49
        $this->requestService = $requestService;
50
        $this->transformerFactory = $transformerFactory;
51
        $this->configurationFactory = $configurationFactory;
52
    }
53
54
    /**
55
     * @param AddUserRequest $request
56
     * @return UserResponse
57
     * @throws UserAlreadyExistException
58
     * @throws UserWrongEmailException
59
     */
60
    public function add(AddUserRequest $request)
61
    {
62
        $transformer = $this->transformerFactory->get(UserDataTransformerFactory::REQUEST_ADD);
63
        $configuration = $this->configurationFactory->get(UserConfigurationFactory::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
        try {
72
            $result = $this->requestService->post('users', ['user' => $params]);
73
        } catch (ClientException $exception) {
74
            $contents = json_decode($exception->getResponse()->getBody(), JSON_UNESCAPED_UNICODE);
75
76
            switch ($contents['error']) {
77
                case UserResponse::ERROR_EMAIL_ALREADY_EXIST:
78
                    throw new UserAlreadyExistException();
79
                case UserResponse::ERROR_PHONE_ALREADY_EXIST:
80
                    throw new UserAlreadyExistException();
81
                case UserResponse::ERROR_WRONG_EMAIL:
82
                    throw new UserWrongEmailException();
83
            }
84
85
            throw $exception;
86
        }
87
88
89
90
        return $this->transformerFactory->get(UserDataTransformerFactory::RESPONSE_VIEW)->transform($result);
91
    }
92
93
    /**
94
     * @param EditUserRequest $request
95
     * @return UserResponse
96
     */
97 View Code Duplication
    public function edit(EditUserRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
98
    {
99
        $transformer = $this->transformerFactory->get(UserDataTransformerFactory::REQUEST_EDIT);
100
        $configuration = $this->configurationFactory->get(UserConfigurationFactory::CONFIGURATION_EDIT);
101
102
        try {
103
            $params = $this->checkRequest($request, $transformer, $configuration);
104
        } catch (InvalidConfigurationException $exception) {
105
            throw new InvalidConfigurationException($exception->getMessage());
106
        }
107
108
        $result = $this->requestService->put("users/{$params['user_id']}", ['user' => $params]);
109
110
        return $this->transformerFactory->get(UserDataTransformerFactory::RESPONSE_VIEW)->transform($result);
111
    }
112
113
    /**
114
     * @param ListUserRequest $request
115
     * @return ListUserResponse
116
     */
117 View Code Duplication
    public function lists(ListUserRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
118
    {
119
        $transformer = $this->transformerFactory->get(UserDataTransformerFactory::REQUEST_LIST);
120
        $configuration = $this->configurationFactory->get(UserConfigurationFactory::CONFIGURATION_LIST);
121
122
        try {
123
            $params = $this->checkRequest($request, $transformer, $configuration);
124
        } catch (InvalidConfigurationException $exception) {
125
            throw new InvalidConfigurationException($exception->getMessage());
126
        }
127
128
        $result = $this->requestService->get('users', $params);
129
130
        return $this->transformerFactory->get(UserDataTransformerFactory::RESPONSE_LIST)->transform($result);
131
    }
132
133
    /**
134
     * @param ViewUserRequest $request
135
     * @return UserResponse
136
     */
137 View Code Duplication
    public function view(ViewUserRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
138
    {
139
        $transformer = $this->transformerFactory->get(UserDataTransformerFactory::REQUEST_VIEW);
140
        $configuration = $this->configurationFactory->get(UserConfigurationFactory::CONFIGURATION_VIEW);
141
142
        try {
143
            $params = $this->checkRequest($request, $transformer, $configuration);
144
        } catch (InvalidConfigurationException $exception) {
145
            throw new InvalidConfigurationException($exception->getMessage());
146
        }
147
148
        $result = $this->requestService->get("users/{$params['user_id']}");
149
150
        return $this->transformerFactory->get(UserDataTransformerFactory::RESPONSE_VIEW)->transform($result);
151
    }
152
153
    /**
154
     * @param ViewUserRequest $request
155
     * @return UserResponse
156
     */
157 View Code Duplication
    public function block(ViewUserRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
158
    {
159
        $transformer = $this->transformerFactory->get(UserDataTransformerFactory::REQUEST_VIEW);
160
        $configuration = $this->configurationFactory->get(UserConfigurationFactory::CONFIGURATION_VIEW);
161
162
        try {
163
            $params = $this->checkRequest($request, $transformer, $configuration);
164
        } catch (InvalidConfigurationException $exception) {
165
            throw new InvalidConfigurationException($exception->getMessage());
166
        }
167
168
        $result = $this->requestService->put("users/{$params['user_id']}/block");
169
170
        return $this->transformerFactory->get(UserDataTransformerFactory::RESPONSE_VIEW)->transform($result);
171
    }
172
173
    /**
174
     * @param ViewUserRequest $request
175
     * @return UserResponse
176
     */
177 View Code Duplication
    public function restore(ViewUserRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
178
    {
179
        $transformer = $this->transformerFactory->get(UserDataTransformerFactory::REQUEST_VIEW);
180
        $configuration = $this->configurationFactory->get(UserConfigurationFactory::CONFIGURATION_VIEW);
181
182
        try {
183
            $params = $this->checkRequest($request, $transformer, $configuration);
184
        } catch (InvalidConfigurationException $exception) {
185
            throw new InvalidConfigurationException($exception->getMessage());
186
        }
187
188
        $result = $this->requestService->put("users/{$params['user_id']}/restore");
189
190
        return $this->transformerFactory->get(UserDataTransformerFactory::RESPONSE_VIEW)->transform($result);
191
    }
192
193
    /**
194
     * @param ViewUserRequest $request
195
     * @return UserResponse
196
     */
197 View Code Duplication
    public function disable(ViewUserRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
198
    {
199
        $transformer = $this->transformerFactory->get(UserDataTransformerFactory::REQUEST_VIEW);
200
        $configuration = $this->configurationFactory->get(UserConfigurationFactory::CONFIGURATION_VIEW);
201
202
        try {
203
            $params = $this->checkRequest($request, $transformer, $configuration);
204
        } catch (InvalidConfigurationException $exception) {
205
            throw new InvalidConfigurationException($exception->getMessage());
206
        }
207
208
        $result = $this->requestService->put("users/{$params['user_id']}/disable");
209
210
        return $this->transformerFactory->get(UserDataTransformerFactory::RESPONSE_VIEW)->transform($result);
211
    }
212
213
    /**
214
     * @param ViewUserRequest $request
215
     */
216 View Code Duplication
    public function delete(ViewUserRequest $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
217
    {
218
        $transformer = $this->transformerFactory->get(UserDataTransformerFactory::REQUEST_VIEW);
219
        $configuration = $this->configurationFactory->get(UserConfigurationFactory::CONFIGURATION_VIEW);
220
221
        try {
222
            $params = $this->checkRequest($request, $transformer, $configuration);
223
        } catch (InvalidConfigurationException $exception) {
224
            throw new InvalidConfigurationException($exception->getMessage());
225
        }
226
227
        $this->requestService->delete("users/{$params['user_id']}");
228
    }
229
}
230