Passed
Pull Request — main (#67)
by
unknown
02:40
created

PostGetCustomFiltersAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B __invoke() 0 95 1
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Actions\Post;
4
5
use Botble\Base\Http\Responses\BaseHttpResponse;
0 ignored issues
show
Bug introduced by
The type Botble\Base\Http\Responses\BaseHttpResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use CSlant\Blog\Api\Http\Resources\Post\ListPostResource;
7
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Post\PostListResourceSchema;
8
use CSlant\Blog\Api\Services\PostService;
9
use CSlant\Blog\Core\Http\Actions\Action;
10
use CSlant\Blog\Core\Supports\Base\FilterPost;
11
use Illuminate\Http\JsonResponse;
12
use Illuminate\Http\RedirectResponse;
13
use Illuminate\Http\Request;
14
use Illuminate\Http\Resources\Json\JsonResource;
15
use OpenApi\Attributes\Get;
16
use OpenApi\Attributes\Items;
17
use OpenApi\Attributes\JsonContent;
18
use OpenApi\Attributes\Parameter;
19
use OpenApi\Attributes\Property;
20
use OpenApi\Attributes\Response;
21
use OpenApi\Attributes\Schema;
22
23
/**
24
 * Class PostGetCustomFiltersAction
25
 *
26
 * @group Blog API
27
 *
28
 * @authenticated
29
 *
30
 * @method BaseHttpResponse httpResponse()
31
 * @method BaseHttpResponse setData(mixed $data)
32
 * @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse()
33
 */
34
class PostGetCustomFiltersAction extends Action
35
{
36
    protected PostService $postService;
37
38
    public function __construct(PostService $postService)
39
    {
40
        $this->postService = $postService;
41
    }
42
43
    /**
44
     * @param  Request  $request
45
     *
46
     * @group Blog
47
     *
48
     * @queryParam  Find by list tag id of post.
49
     *
50
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
51
     */
52
    #[
53
        Get(
54
            path: "/posts/filtered",
55
            operationId: "postGetWithFiltered",
56
            description: "Get all posts with pagination (10 items per page by default, page 1 by default)
57
            
58
    This API will get records from the database and return them as a paginated list. 
59
    The default number of items per page is 10 and the default page number is 1. You can change these values by passing the `per_page` and `page` query parameters.
60
            ",
61
            summary: "Get posts by filter with pagination",
62
            tags: ["Post"],
63
            parameters: [
64
                new Parameter(
65
                    name: 'categories',
66
                    description: 'Filter posts by categories IDs',
67
                    in: 'query',
68
                    required: false,
69
                    schema: new Schema(
70
                        type: 'array',
71
                        items: new Items(description: 'Input the category ID', type: 'integer'),
72
                        default: null,
73
                    )
74
                ),
75
                new Parameter(
76
                    name: 'tags',
77
                    description: 'Filter posts by tag specific tag IDs.',
78
                    in: 'query',
79
                    required: false,
80
                    schema: new Schema(
81
                        type: 'array',
82
                        items: new Items(description: 'Input the exclude tag ID', type: 'integer'),
83
                        default: null
84
                    )
85
                ),
86
                new Parameter(
87
                    name: 'per_page',
88
                    description: 'Number of items per page',
89
                    in: 'query',
90
                    required: false,
91
                    schema: new Schema(type: 'integer', default: 10)
92
                ),
93
                new Parameter(
94
                    name: 'page',
95
                    description: 'Page number',
96
                    in: 'query',
97
                    required: false,
98
                    schema: new Schema(type: 'integer', default: 1)
99
                ),
100
            ],
101
            responses: [
102
                new Response(
103
                    response: 200,
104
                    description: "Get list posts successfully",
105
                    content: new JsonContent(
106
                        properties: [
107
                            new Property(
108
                                property: 'error',
109
                                description: 'Error status',
110
                                type: 'boolean',
111
                                default: false
112
                            ),
113
                            new Property(
114
                                property: "data",
115
                                ref: PostListResourceSchema::class,
116
                                description: "Data of model",
117
                                type: "object",
118
                            ),
119
                        ]
120
                    )
121
                ),
122
                new Response(
123
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
124
                    response: 400,
125
                ),
126
                new Response(
127
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
128
                    response: 404,
129
                ),
130
                new Response(
131
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
132
                    response: 500,
133
                ),
134
            ]
135
        )
136
    ]
137
    public function __invoke(Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
138
    {
139
        $filters = FilterPost::setFilters($request->input());
140
141
        $data = $this->postService->getPostByTags((array) $filters);
142
143
        return $this
144
            ->httpResponse()
145
            ->setData(ListPostResource::collection($data))
146
            ->toApiResponse();
147
    }
148
}
149