Passed
Pull Request — main (#63)
by
unknown
02:31
created

GetByTagAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 61
c 3
b 0
f 1
dl 0
loc 98
rs 10
wmc 2

2 Methods

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