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

GetByTagAction   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 1
eloc 58
c 1
b 0
f 1
dl 0
loc 91
rs 10

1 Method

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