Passed
Pull Request — main (#63)
by
unknown
03:10
created

PostGetByTagAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 101
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 83 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 Botble\Blog\Http\Resources\PostResource;
0 ignored issues
show
Bug introduced by
The type Botble\Blog\Http\Resources\PostResource 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...
7
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Post\PostModelResourceSchema;
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 GetByTagAction
25
 *
26
 *
27
 * @group Blog API
28
 *
29
 * @authenticated
30
 *
31
 * @method BaseHttpResponse httpResponse()
32
 * @method BaseHttpResponse setData(mixed $data)
33
 * @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse()
34
 */
35
class PostGetByTagAction extends Action
36
{
37
    protected PostService $postService;
38
39
    public function __construct(PostService $postService)
40
    {
41
        $this->postService = $postService;
42
    }
43
44
    /**
45
     * @param  Request  $request
46
     *
47
     * @group Blog
48
     *
49
     * @queryParam  Find by tagId of post.
50
     *
51
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
52
     */
53
    #[
54
        Get(
55
            path: "/posts/get-by-tags",
56
            operationId: "postGetByTag",
57
            description: "Get list post of the tag by tag id
58
            
59
    This API will get record from the database and return list post of the tag by tag id.
60
            ",
61
            summary: "Get list post of the tag by tag id",
62
            tags: ["Post"],
63
            parameters: [
64
                new Parameter(
65
                    name: 'tags',
66
                    description: 'Filter posts by tag specific tag IDs.',
67
                    in: 'query',
68
                    required: false,
69
                    schema: new Schema(
70
                        type: 'array',
71
                        items: new Items(description: 'Input the exclude tag ID', type: 'integer'),
72
                        default: null
73
                    )
74
                ),
75
                new Parameter(
76
                    name: 'per_page',
77
                    description: 'Number of items per page',
78
                    in: 'query',
79
                    required: false,
80
                    schema: new Schema(type: 'integer', default: 10)
81
                ),
82
                new Parameter(
83
                    name: 'page',
84
                    description: 'Page number',
85
                    in: 'query',
86
                    required: false,
87
                    schema: new Schema(type: 'integer', default: 1)
88
                ),
89
            ],
90
            responses: [
91
                new Response(
92
                    response: 200,
93
                    description: "Get list posts by tag successfully",
94
                    content: new JsonContent(
95
                        properties: [
96
                            new Property(
97
                                property: 'error',
98
                                description: 'Error status',
99
                                type: 'boolean',
100
                                default: false
101
                            ),
102
                            new Property(
103
                                property: "data",
104
                                ref: PostModelResourceSchema::class,
105
                                description: "Data of model",
106
                                type: "object",
107
                            ),
108
                        ]
109
                    )
110
                ),
111
                new Response(
112
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
113
                    response: 400,
114
                ),
115
                new Response(
116
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
117
                    response: 404,
118
                ),
119
                new Response(
120
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
121
                    response: 500,
122
                ),
123
            ]
124
        )
125
    ]
126
    public function __invoke(Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
127
    {
128
        $filters = FilterPost::setFilters($request->input());
129
130
        $data = $this->postService->getPostByTags((array) $filters);
131
132
        return $this
133
            ->httpResponse()
134
            ->setData(PostResource::collection($data))
135
            ->toApiResponse();
136
    }
137
}
138