1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Finder\Http\Actions; |
4
|
|
|
|
5
|
|
|
use Finder\Http\Resources\UserPlainResource; |
6
|
|
|
use Population\Manipule\Managers\UserManager; |
7
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
8
|
|
|
use Illuminate\Http\JsonResponse; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
use Illuminate\Http\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class UserUpdateByIdAction. |
14
|
|
|
* |
15
|
|
|
* @package Finder\Http\Actions |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
class UserUpdateByIdAction |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ResponseFactory |
21
|
|
|
*/ |
22
|
|
|
private $responseFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var UserManager |
26
|
|
|
*/ |
27
|
|
|
private $userManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* UserUpdateByIdAction constructor. |
31
|
|
|
* |
32
|
|
|
* @param ResponseFactory $responseFactory |
33
|
|
|
* @param UserManager $userManager |
34
|
|
|
*/ |
35
|
|
|
public function __construct(ResponseFactory $responseFactory, UserManager $userManager) |
36
|
|
|
{ |
37
|
|
|
$this->responseFactory = $responseFactory; |
38
|
|
|
$this->userManager = $userManager; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @apiVersion 1.0.0 |
43
|
|
|
* @api {put} /v1/users/:user_id Update |
44
|
|
|
* @apiName Update |
45
|
|
|
* @apiGroup Users |
46
|
|
|
* @apiHeader {String} Accept application/json |
47
|
|
|
* @apiHeader {String} Content-type application/json |
48
|
|
|
* @apiParam {Integer{1...N}='me'} :user_id Unique resource ID. |
49
|
|
|
* @apiParamExample {json} Request-Body-Example: |
50
|
|
|
* { |
51
|
|
|
* "name": "username", |
52
|
|
|
* "email": "[email protected]", |
53
|
|
|
* "password": "password" |
54
|
|
|
* } |
55
|
|
|
* @apiSuccessExample {json} Success-Response: |
56
|
|
|
* HTTP/1.1 20O OK |
57
|
|
|
* { |
58
|
|
|
* "id": 1, |
59
|
|
|
* "name": "username", |
60
|
|
|
* "email": "[email protected]", |
61
|
|
|
* "role": "Customer", |
62
|
|
|
* "created_at": "2099-12-31T23:59:59+00:00", |
63
|
|
|
* "updated_at": "2099-12-31T23:59:59+00:00" |
64
|
|
|
* } |
65
|
|
|
*/ |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Update a user. |
69
|
|
|
* |
70
|
|
|
* @param mixed $id |
71
|
|
|
* @param Request $request |
72
|
|
|
* @return JsonResponse |
73
|
|
|
*/ |
74
|
|
|
public function __invoke($id, Request $request): JsonResponse |
75
|
|
|
{ |
76
|
|
|
$user = $this->userManager->updateById((int) $id, $request->all()); |
77
|
|
|
|
78
|
|
|
return $this->responseFactory->json(new UserPlainResource($user), Response::HTTP_OK); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.