1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Finder\Http\Actions; |
4
|
|
|
|
5
|
|
|
use Finder\Http\Resources\PostResource; |
6
|
|
|
use Population\Manipule\Managers\PostManager; |
7
|
|
|
use Illuminate\Contracts\Cache\Factory as CacheManager; |
8
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
9
|
|
|
use Illuminate\Http\JsonResponse; |
10
|
|
|
use Illuminate\Http\Request; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class PostUpdateByIdAction. |
14
|
|
|
* |
15
|
|
|
* @package Finder\Http\Actions |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
class PostUpdateByIdAction |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ResponseFactory |
21
|
|
|
*/ |
22
|
|
|
private $responseFactory; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var PostManager |
26
|
|
|
*/ |
27
|
|
|
private $postManager; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var CacheManager |
31
|
|
|
*/ |
32
|
|
|
private $cacheManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* PostUpdateByIdAction constructor. |
36
|
|
|
* |
37
|
|
|
* @param ResponseFactory $responseFactory |
38
|
|
|
* @param PostManager $postManager |
39
|
|
|
* @param CacheManager $cacheManager |
40
|
|
|
*/ |
41
|
|
|
public function __construct(ResponseFactory $responseFactory, PostManager $postManager, CacheManager $cacheManager) |
42
|
|
|
{ |
43
|
|
|
$this->responseFactory = $responseFactory; |
44
|
|
|
$this->postManager = $postManager; |
45
|
|
|
$this->cacheManager = $cacheManager; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @apiVersion 1.0.0 |
50
|
|
|
* @api {put} /v1/posts/:post_id Update |
51
|
|
|
* @apiName Update |
52
|
|
|
* @apiGroup Posts |
53
|
|
|
* @apiHeader {String} Accept application/json |
54
|
|
|
* @apiHeader {String} Content-type application/json |
55
|
|
|
* @apiParam {Integer{1..N}} :post_id Unique resource ID. |
56
|
|
|
* @apiParamExample {json} Request-Body-Example: |
57
|
|
|
* { |
58
|
|
|
* "is_published": true, |
59
|
|
|
* "description": "The post description.", |
60
|
|
|
* "photo": { |
61
|
|
|
* "id": 1 |
62
|
|
|
* }, |
63
|
|
|
* "tags": [ |
64
|
|
|
* { |
65
|
|
|
* "value": "tag", |
66
|
|
|
* } |
67
|
|
|
* ] |
68
|
|
|
* } |
69
|
|
|
* @apiSuccessExample {json} Success-Response: |
70
|
|
|
* HTTP/1.1 201 Created |
71
|
|
|
* { |
72
|
|
|
* "id": 1, |
73
|
|
|
* "created_by_user_id": 1, |
74
|
|
|
* "is_published": true, |
75
|
|
|
* "description": "The post description.", |
76
|
|
|
* "published_at": "2099-12-31T23:59:59+00:00", |
77
|
|
|
* "created_at": "2099-12-31T23:59:59+00:00", |
78
|
|
|
* "updated_at": "2099-12-31T23:59:59+00:00", |
79
|
|
|
* "photo": { |
80
|
|
|
* "id": 1, |
81
|
|
|
* "created_by_user_id" 1, |
82
|
|
|
* "avg_color": "#000000", |
83
|
|
|
* "created_at": "2099-12-31T23:59:59+00:00", |
84
|
|
|
* "exif": { |
85
|
|
|
* "manufacturer": "Manufacturer Name", |
86
|
|
|
* "model": "Model Number", |
87
|
|
|
* "exposure_time": "1/160", |
88
|
|
|
* "aperture": "f/11.0", |
89
|
|
|
* "iso": 200, |
90
|
|
|
* "taken_at": "2099-12-31T23:59:59+00:00", |
91
|
|
|
* "software": "Software Name" |
92
|
|
|
* }, |
93
|
|
|
* "thumbnails": [ |
94
|
|
|
* "medium": { |
95
|
|
|
* "url": "http://path/to/photo/thumbnail/medium_file" |
96
|
|
|
* "width": 500, |
97
|
|
|
* "height": 500 |
98
|
|
|
* }, |
99
|
|
|
* "large": { |
100
|
|
|
* "url": "http://path/to/photo/thumbnail/large_file" |
101
|
|
|
* "width": 1000, |
102
|
|
|
* "height": 1000 |
103
|
|
|
* } |
104
|
|
|
* ] |
105
|
|
|
* }, |
106
|
|
|
* "tags": [ |
107
|
|
|
* { |
108
|
|
|
* "value": "tag", |
109
|
|
|
* } |
110
|
|
|
* ] |
111
|
|
|
* } |
112
|
|
|
*/ |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Update a post. |
116
|
|
|
* |
117
|
|
|
* @param mixed $id |
118
|
|
|
* @param Request $request |
119
|
|
|
* @return JsonResponse |
120
|
|
|
*/ |
121
|
|
|
public function __invoke($id, Request $request): JsonResponse |
122
|
|
|
{ |
123
|
|
|
$post = $this->postManager->updateById((int) $id, $request->all()); |
124
|
|
|
|
125
|
|
|
$this->cacheManager->tags(['posts', 'photos', 'tags'])->flush(); |
|
|
|
|
126
|
|
|
|
127
|
|
|
return $this->responseFactory->json(new PostResource($post), JsonResponse::HTTP_OK); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.