|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Finder\Http\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use Finder\Http\Requests\PaginatedRequest; |
|
6
|
|
|
use Finder\Http\Resources\PaginatedResource; |
|
7
|
|
|
use Finder\Http\Resources\TagPlainResource; |
|
8
|
|
|
use Population\Manipule\Managers\TagManager; |
|
9
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
|
10
|
|
|
use Illuminate\Http\JsonResponse; |
|
11
|
|
|
use Illuminate\Http\Response; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class TagPaginateAction. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Finder\Http\Actions |
|
17
|
|
|
*/ |
|
18
|
|
View Code Duplication |
class TagPaginateAction |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var ResponseFactory |
|
22
|
|
|
*/ |
|
23
|
|
|
private $responseFactory; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var TagManager |
|
27
|
|
|
*/ |
|
28
|
|
|
private $tagManager; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* TagPaginateAction constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param ResponseFactory $responseFactory |
|
34
|
|
|
* @param TagManager $tagManager |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(ResponseFactory $responseFactory, TagManager $tagManager) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->responseFactory = $responseFactory; |
|
39
|
|
|
$this->tagManager = $tagManager; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @apiVersion 1.0.0 |
|
44
|
|
|
* @api {get} /v1/tags?page=:page&per_page=:per_page Paginate |
|
45
|
|
|
* @apiName Paginate |
|
46
|
|
|
* @apiGroup Tags |
|
47
|
|
|
* @apiHeader {String} Accept application/json |
|
48
|
|
|
* @apiParam {Integer{1..N}} [page=1] |
|
49
|
|
|
* @apiParam {Integer{1..100}} [per_page=20] |
|
50
|
|
|
* @apiSuccessExample {json} Success-Response: |
|
51
|
|
|
* HTTP/1.1 200 OK |
|
52
|
|
|
* { |
|
53
|
|
|
* "total": 100, |
|
54
|
|
|
* "per_page": 10, |
|
55
|
|
|
* "current_page": 2, |
|
56
|
|
|
* "last_page": 10, |
|
57
|
|
|
* "next_page_url": "http://path/to/api/resource?page=3", |
|
58
|
|
|
* "prev_page_url": "http://path/to/api/resource?page=1", |
|
59
|
|
|
* "from": 10, |
|
60
|
|
|
* "to": 20, |
|
61
|
|
|
* "data": [ |
|
62
|
|
|
* { |
|
63
|
|
|
* "value": "tag" |
|
64
|
|
|
* } |
|
65
|
|
|
* ] |
|
66
|
|
|
* } |
|
67
|
|
|
*/ |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Paginate over tags. |
|
71
|
|
|
* |
|
72
|
|
|
* @param PaginatedRequest $request |
|
73
|
|
|
* @return JsonResponse |
|
74
|
|
|
*/ |
|
75
|
|
|
public function __invoke(PaginatedRequest $request): JsonResponse |
|
76
|
|
|
{ |
|
77
|
|
|
$paginator = $this->tagManager->paginate( |
|
78
|
|
|
$request->get('page', 1), |
|
79
|
|
|
$request->get('per_page', 20), |
|
80
|
|
|
$request->query() |
|
81
|
|
|
); |
|
82
|
|
|
|
|
83
|
|
|
return $this->responseFactory->json(new PaginatedResource($paginator, TagPlainResource::class), Response::HTTP_OK); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
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.