Passed
Push — main ( 36d8dc...df6838 )
by Tan
03:46 queued 21s
created

TagController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 73
Code Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 52
c 2
b 0
f 0
dl 0
loc 73
rs 9.0472
cc 1
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CSlant\Blog\Api\Http\Controllers;
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 CSlant\Blog\Api\Enums\StatusEnum;
7
use CSlant\Blog\Api\Http\Resources\TagResource;
8
use CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema;
9
use CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema;
10
use CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema;
11
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Tag\TagModelResourceSchema;
12
use CSlant\Blog\Core\Facades\Base\SlugHelper;
13
use CSlant\Blog\Core\Http\Controllers\Base\BaseTagController;
14
use CSlant\Blog\Core\Models\Slug;
15
use CSlant\Blog\Core\Models\Tag;
16
use Illuminate\Http\JsonResponse;
17
use Illuminate\Http\RedirectResponse;
18
use Illuminate\Http\Request;
19
use Illuminate\Http\Resources\Json\JsonResource;
20
use OpenApi\Attributes\Get;
21
use OpenApi\Attributes\JsonContent;
22
use OpenApi\Attributes\Parameter;
23
use OpenApi\Attributes\Property;
24
use OpenApi\Attributes\Response;
25
use OpenApi\Attributes\Schema;
26
27
/**
28
 * Class TagController
29
 *
30
 * @package CSlant\Blog\Api\Http\Controllers
31
 *
32
 * @group Blog
33
 *
34
 * @authenticated
35
 *
36
 * @method BaseHttpResponse httpResponse()
37
 * @method BaseHttpResponse setData(mixed $data)
38
 */
39
class TagController extends BaseTagController
40
{
41
    #[
42
        Get(
43
            path: "/tags",
44
            operationId: "tagGetAllWithFilter",
45
            description: "Get all tags with pagination (10 items per page by default, page 1 by default, page 1 by default)
46
    This API will get records from the database and return them as a paginated list. 
47
    The default number of items per page is 10 and the default page number is 1. You can change these values by passing the `per_page` and `page` query parameters.
48
          ",
49
            summary: "Get all tags with pagination",
50
            security: [['sanctum' => []]],
51
            tags: ["Tag"],
52
            parameters: [
53
                new Parameter(
54
                    name: 'per_page',
55
                    description: 'Number of items per page',
56
                    in: 'query',
57
                    required: false,
58
                    schema: new Schema(type: 'integer', default: 10)
59
                ),
60
                new Parameter(
61
                    name: 'page',
62
                    description: 'Page number',
63
                    in: 'query',
64
                    required: false,
65
                    schema: new Schema(type: 'integer', default: 1)
66
                ),
67
            ],
68
            responses: [
69
                new Response(
70
                    response: 200,
71
                    description: "Get tags successfully",
72
                    content: new JsonContent(
73
                        properties: [
74
                            new Property(
75
                                property: 'error',
76
                                description: 'Error status',
77
                                type: 'boolean',
78
                                default: false
79
                            ),
80
                            new Property(
81
                                property: 'data',
82
                                description: 'Data',
83
                                properties: [
84
                                    new Property(
85
                                        property: 'tag',
86
                                        ref: TagModelResourceSchema::class,
87
                                        description: 'Tag',
88
                                        type: 'object'
89
                                    ),
90
                                ],
91
                                type: 'object'
92
                            ),
93
                        ]
94
                    )
95
                ),
96
                new Response(
97
                    ref: BadRequestResponseSchema::class,
98
                    response: 400,
99
                ),
100
                new Response(
101
                    ref: ErrorNotFoundResponseSchema::class,
102
                    response: 404,
103
                ),
104
                new Response(
105
                    ref: InternalServerResponseSchema::class,
106
                    response: 500,
107
                ),
108
            ]
109
        )
110
    ]
111
    public function index(Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
112
    {
113
        return parent::index($request);
114
    }
115
116
    /**
117
     * Get tag by slug
118
     *
119
     * @group Blog
120
     * @queryParam slug Find by slug of tag.
121
     *
122
     * @param  string  $slug
123
     *
124
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
125
     */
126
    #[
127
        Get(
128
            path: "/tags/{slug}",
129
            operationId: "tagFilterBySlug",
130
            description: "Get the tag by slug
131
            
132
    This API will get records from the database and return the tag by slug.
133
            ",
134
            summary: "Get tag by slug",
135
            security: [['sanctum' => []]],
136
            tags: ["Tag"],
137
            parameters: [
138
                new Parameter(
139
                    name: 'slug',
140
                    description: 'Tag slug',
141
                    in: 'path',
142
                    required: true,
143
                    schema: new Schema(type: 'string', example: 'php')
144
                ),
145
            ],
146
            responses: [
147
                new Response(
148
                    response: 200,
149
                    description: "Get tag successfully",
150
                    content: new JsonContent(
151
                        properties: [
152
                            new Property(
153
                                property: 'error',
154
                                description: 'Error status',
155
                                type: 'boolean',
156
                                default: false
157
                            ),
158
                            new Property(
159
                                property: "data",
160
                                ref: TagModelResourceSchema::class,
161
                                description: "Data of model",
162
                                type: "object",
163
                            ),
164
                        ]
165
                    )
166
                ),
167
                new Response(
168
                    ref: BadRequestResponseSchema::class,
169
                    response: 400,
170
                ),
171
                new Response(
172
                    ref: ErrorNotFoundResponseSchema::class,
173
                    response: 404,
174
                ),
175
                new Response(
176
                    ref: InternalServerResponseSchema::class,
177
                    response: 500,
178
                ),
179
            ]
180
        )
181
    ]
182
    public function findBySlug(string $slug): JsonResponse|RedirectResponse|JsonResource|BaseHttpResponse
183
    {
184
        /** @var Slug $slug */
185
        $slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Tag::getBaseModel()));
186
187
        if (!$slug) {
0 ignored issues
show
introduced by
$slug is of type CSlant\Blog\Core\Models\Slug, thus it always evaluated to true.
Loading history...
188
            return $this
189
                ->httpResponse()
190
                ->setError()
191
                ->setCode(404)
192
                ->setMessage('Not found');
193
        }
194
195
        $tag = Tag::query()
196
            ->with('slugable')
197
            ->where([
198
                'id' => $slug->reference_id,
199
                'status' => StatusEnum::PUBLISHED,
200
            ])
201
            ->first();
202
203
        if (!$tag) {
204
            return $this
205
                ->httpResponse()
206
                ->setError()
207
                ->setCode(404)
208
                ->setMessage('Not found');
209
        }
210
211
        return $this
212
            ->httpResponse()
213
            ->setData(new TagResource($tag))
214
            ->toApiResponse();
215
    }
216
}
217