Passed
Push — main ( b4775f...6b1a6c )
by Tan
07:12 queued 03:33
created

PostGetCustomFiltersAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 115
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 99 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
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
49
     */
50
    #[
51
        Get(
52
            path: "/posts/custom-filters",
53
            operationId: "postGetWithCustomFilters",
54
            description: "Get all posts with pagination (10 items per page by default, page 1 by default)
55
            
56
    This API will get records from the database and return them as a paginated list. 
57
    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.
58
    
59
    Additional query parameters:
60
    - `custom-filters`: Apply custom filters such as tags, categories, etc and custom order by multiple.  
61
    - `tags`: Filter posts that are associated with specific tag IDs. Supports passing multiple tag IDs.
62
            ",
63
            summary: "Get posts by filter with pagination",
64
            tags: ["Post"],
65
            parameters: [
66
                new Parameter(
67
                    name: 'categories',
68
                    description: 'Filter posts by categories IDs',
69
                    in: 'query',
70
                    required: false,
71
                    schema: new Schema(
72
                        type: 'array',
73
                        items: new Items(description: 'Input the category ID', type: 'integer'),
74
                        default: null,
75
                    )
76
                ),
77
                new Parameter(
78
                    name: 'tags',
79
                    description: 'Filter posts by tag specific tag IDs.',
80
                    in: 'query',
81
                    required: false,
82
                    schema: new Schema(
83
                        type: 'array',
84
                        items: new Items(description: 'Input the exclude tag ID', type: 'integer'),
85
                        default: null
86
                    )
87
                ),
88
                new Parameter(
89
                    name: 'per_page',
90
                    description: 'Number of items per page',
91
                    in: 'query',
92
                    required: false,
93
                    schema: new Schema(type: 'integer', default: 10)
94
                ),
95
                new Parameter(
96
                    name: 'page',
97
                    description: 'Page number',
98
                    in: 'query',
99
                    required: false,
100
                    schema: new Schema(type: 'integer', default: 1)
101
                ),
102
            ],
103
            responses: [
104
                new Response(
105
                    response: 200,
106
                    description: "Get list posts successfully",
107
                    content: new JsonContent(
108
                        properties: [
109
                            new Property(
110
                                property: 'error',
111
                                description: 'Error status',
112
                                type: 'boolean',
113
                                default: false
114
                            ),
115
                            new Property(
116
                                property: "data",
117
                                ref: PostListResourceSchema::class,
118
                                description: "Data of model",
119
                                type: "object",
120
                            ),
121
                        ]
122
                    )
123
                ),
124
                new Response(
125
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
126
                    response: 400,
127
                ),
128
                new Response(
129
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
130
                    response: 404,
131
                ),
132
                new Response(
133
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
134
                    response: 500,
135
                ),
136
            ]
137
        )
138
    ]
139
    public function __invoke(Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
140
    {
141
        $filters = FilterPost::setFilters($request->input());
142
143
        $data = $this->postService->getCustomFilters((array) $filters);
144
145
        return $this
146
            ->httpResponse()
147
            ->setData(ListPostResource::collection($data))
148
            ->toApiResponse();
149
    }
150
}
151