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 PostCreateAction. |
14
|
|
|
* |
15
|
|
|
* @package Finder\Http\Actions |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
class PostCreateAction |
|
|
|
|
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
|
|
|
* PostCreateAction 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 {post} /v1/posts Create |
51
|
|
|
* @apiName Create |
52
|
|
|
* @apiGroup Posts |
53
|
|
|
* @apiHeader {String} Accept application/json |
54
|
|
|
* @apiHeader {String} Content-type application/json |
55
|
|
|
* @apiParamExample {json} Request-Body-Example: |
56
|
|
|
* { |
57
|
|
|
* "is_published": true, |
58
|
|
|
* "description": "The post description.", |
59
|
|
|
* "photo": { |
60
|
|
|
* "id": 1 |
61
|
|
|
* }, |
62
|
|
|
* "tags": [ |
63
|
|
|
* { |
64
|
|
|
* "value": "tag", |
65
|
|
|
* } |
66
|
|
|
* ] |
67
|
|
|
* } |
68
|
|
|
* @apiSuccessExample {json} Success-Response: |
69
|
|
|
* HTTP/1.1 201 Created |
70
|
|
|
* { |
71
|
|
|
* "id": 1, |
72
|
|
|
* "created_by_user_id": 1, |
73
|
|
|
* "is_published": true, |
74
|
|
|
* "description": "The post description.", |
75
|
|
|
* "published_at": "2099-12-31T23:59:59+00:00", |
76
|
|
|
* "created_at": "2099-12-31T23:59:59+00:00", |
77
|
|
|
* "updated_at": "2099-12-31T23:59:59+00:00", |
78
|
|
|
* "photo": { |
79
|
|
|
* "id": 1, |
80
|
|
|
* "created_by_user_id" 1, |
81
|
|
|
* "avg_color": "#000000", |
82
|
|
|
* "created_at": "2099-12-31T23:59:59+00:00", |
83
|
|
|
* "exif": { |
84
|
|
|
* "manufacturer": "Manufacturer Name", |
85
|
|
|
* "model": "Model Number", |
86
|
|
|
* "exposure_time": "1/160", |
87
|
|
|
* "aperture": "f/11.0", |
88
|
|
|
* "iso": 200, |
89
|
|
|
* "taken_at": "2099-12-31T23:59:59+00:00", |
90
|
|
|
* "software": "Software Name" |
91
|
|
|
* }, |
92
|
|
|
* "thumbnails": [ |
93
|
|
|
* "medium": { |
94
|
|
|
* "url": "http://path/to/photo/thumbnail/medium_file" |
95
|
|
|
* "width": 500, |
96
|
|
|
* "height": 500 |
97
|
|
|
* }, |
98
|
|
|
* "large": { |
99
|
|
|
* "url": "http://path/to/photo/thumbnail/large_file" |
100
|
|
|
* "width": 1000, |
101
|
|
|
* "height": 1000 |
102
|
|
|
* } |
103
|
|
|
* ] |
104
|
|
|
* }, |
105
|
|
|
* "tags": [ |
106
|
|
|
* { |
107
|
|
|
* "value": "tag", |
108
|
|
|
* } |
109
|
|
|
* ] |
110
|
|
|
* } |
111
|
|
|
*/ |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create a post. |
115
|
|
|
* |
116
|
|
|
* @param Request $request |
117
|
|
|
* @return JsonResponse |
118
|
|
|
*/ |
119
|
|
|
public function __invoke(Request $request): JsonResponse |
120
|
|
|
{ |
121
|
|
|
$post = $this->postManager->create($request->all()); |
122
|
|
|
|
123
|
|
|
$this->cacheManager->tags(['posts', 'photos', 'tags'])->flush(); |
|
|
|
|
124
|
|
|
|
125
|
|
|
return $this->responseFactory->json(new PostResource($post), JsonResponse::HTTP_CREATED); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
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.