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

GetByTagAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 79 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\JsonContent;
17
use OpenApi\Attributes\Parameter;
18
use OpenApi\Attributes\Property;
19
use OpenApi\Attributes\Response;
20
use OpenApi\Attributes\Schema;
21
22
/**
23
 * Class GetByTagAction
24
 *
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 GetByTagAction extends Action
35
{
36
    public function __construct(protected PostInterface $postRepository)
37
    {
38
    }
39
40
    /**
41
     * @param  int  $tagId
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/{tagId}/by-tag",
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: 'tagId',
63
                    description: 'Tag Id',
64
                    in: 'path',
65
                    required: true,
66
                    schema: new Schema(type: 'string', example: 'php')
67
                ),
68
                new Parameter(
69
                    name: 'per_page',
70
                    description: 'Number of items per page',
71
                    in: 'query',
72
                    required: false,
73
                    schema: new Schema(type: 'integer', default: 10)
74
                ),
75
                new Parameter(
76
                    name: 'page',
77
                    description: 'Page number',
78
                    in: 'query',
79
                    required: false,
80
                    schema: new Schema(type: 'integer', default: 1)
81
                ),
82
            ],
83
            responses: [
84
                new Response(
85
                    response: 200,
86
                    description: "Get list posts by tag successfully",
87
                    content: new JsonContent(
88
                        properties: [
89
                            new Property(
90
                                property: 'error',
91
                                description: 'Error status',
92
                                type: 'boolean',
93
                                default: false
94
                            ),
95
                            new Property(
96
                                property: "data",
97
                                ref: PostModelResourceSchema::class,
98
                                description: "Data of model",
99
                                type: "object",
100
                            ),
101
                        ]
102
                    )
103
                ),
104
                new Response(
105
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
106
                    response: 400,
107
                ),
108
                new Response(
109
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
110
                    response: 404,
111
                ),
112
                new Response(
113
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
114
                    response: 500,
115
                ),
116
            ]
117
        )
118
    ]
119
    public function __invoke(int $tagId, Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
120
    {
121
        $filters = FilterPost::setFilters($request->input());
122
123
        $data = $this->postRepository->getByTag($tagId, (int) $filters['per_page']);
124
125
        return $this
126
            ->httpResponse()
127
            ->setData(PostResource::collection($data))
128
            ->toApiResponse();
129
    }
130
}
131