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