TagController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 8
Bugs 5 Features 0
Metric Value
wmc 5
eloc 116
c 8
b 5
f 0
dl 0
loc 175
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B findBySlug() 0 88 3
A index() 0 73 2
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\Http\Resources\Tag\TagResource;
7
use CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema;
8
use CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema;
9
use CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema;
10
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Tag\TagModelResourceSchema;
11
use CSlant\Blog\Core\Enums\StatusEnum;
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\Items;
22
use OpenApi\Attributes\JsonContent;
23
use OpenApi\Attributes\Parameter;
24
use OpenApi\Attributes\Property;
25
use OpenApi\Attributes\Response;
26
use OpenApi\Attributes\Schema;
27
28
/**
29
 * Class TagController
30
 *
31
 * @package CSlant\Blog\Api\Http\Controllers
32
 *
33
 * @group Blog
34
 *
35
 * @authenticated
36
 *
37
 * @method BaseHttpResponse httpResponse()
38
 * @method BaseHttpResponse setData(mixed $data)
39
 */
40
class TagController extends BaseTagController
41
{
42
    #[
43
        Get(
44
            path: "/tags",
45
            operationId: "tagGetAllWithFilter",
46
            description: "Get all tags with pagination (10 items per page by default, page 1 by default, page 1 by default)
47
48
    This API will get records from the database and return them as a paginated list. 
49
    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.",
50
            summary: "Get all tags with pagination",
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 of model",
83
                                type: "array",
84
                                items: new Items(ref: TagModelResourceSchema::class)
85
                            ),
86
                        ]
87
                    )
88
                ),
89
                new Response(
90
                    ref: BadRequestResponseSchema::class,
91
                    response: 400,
92
                ),
93
                new Response(
94
                    ref: ErrorNotFoundResponseSchema::class,
95
                    response: 404,
96
                ),
97
                new Response(
98
                    ref: InternalServerResponseSchema::class,
99
                    response: 500,
100
                ),
101
            ]
102
        )
103
    ]
104
    public function index(Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
105
    {
106
        $data = Tag::query()
107
            ->wherePublished()
108
            ->with('slugable')
109
            ->paginate($request->integer('per_page', 10) ?: 10);
110
111
        return $this
112
            ->httpResponse()
113
            ->setData(TagResource::collection($data))
114
            ->toApiResponse();
115
    }
116
117
    /**
118
     * Get tag by slug
119
     *
120
     * @group Blog
121
     * @queryParam slug Find by slug of tag.
122
     *
123
     * @param  string  $slug
124
     *
125
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
126
     */
127
    #[
128
        Get(
129
            path: "/tags/{slug}",
130
            operationId: "tagFilterBySlug",
131
            description: "Get the tag by slug
132
            
133
    This API will get records from the database and return the tag by slug.
134
            ",
135
            summary: "Get tag by slug",
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