Passed
Push — main ( df6838...327ac0 )
by Tan
03:15 queued 11s
created

CategoryController   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 179
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 5
eloc 119
c 4
b 1
f 0
dl 0
loc 179
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B index() 0 76 2
B findBySlug() 0 89 3
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\ListCategoryResource;
8
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Category\CategoryListResourceSchema;
9
use CSlant\Blog\Core\Facades\Base\SlugHelper;
10
use CSlant\Blog\Core\Http\Controllers\Base\BaseCategoryController;
11
use CSlant\Blog\Core\Models\Category;
12
use CSlant\Blog\Core\Models\Slug;
13
use Illuminate\Http\JsonResponse;
14
use Illuminate\Http\RedirectResponse;
15
use Illuminate\Http\Request;
16
use Illuminate\Http\Resources\Json\JsonResource;
17
use OpenApi\Attributes\Get;
18
use OpenApi\Attributes\Items;
19
use OpenApi\Attributes\JsonContent;
20
use OpenApi\Attributes\Parameter;
21
use OpenApi\Attributes\Property;
22
use OpenApi\Attributes\Response;
23
use OpenApi\Attributes\Schema;
24
25
/**
26
 * Class CategoryController
27
 *
28
 * @package CSlant\Blog\Api\Http\Controllers
29
 *
30
 * @group Blog API
31
 *
32
 * @authenticated
33
 *
34
 * @method BaseHttpResponse httpResponse()
35
 * @method BaseHttpResponse setData(mixed $data)
36
 * @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse()
37
 */
38
class CategoryController extends BaseCategoryController
39
{
40
    #[
41
        Get(
42
            path: "/categories",
43
            operationId: "categoryGetAllWithFilter",
44
            description: "Get all categories with pagination (10 items per page by default, page 1 by default)
45
            
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 categories with pagination",
50
            security: [['sanctum' => []]],
51
            tags: ["Category"],
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 categories 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: CategoryListResourceSchema::class)
85
                            ),
86
                        ]
87
                    )
88
                ),
89
                new Response(
90
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
91
                    response: 400,
92
                ),
93
                new Response(
94
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
95
                    response: 404,
96
                ),
97
                new Response(
98
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
99
                    response: 500,
100
                ),
101
            ]
102
        )
103
    ]
104
    public function index(Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
105
    {
106
        $data = Category::query()
107
            ->wherePublished()
108
            ->orderByDesc('created_at')
109
            ->with(['slugable'])
110
            ->paginate($request->integer('per_page', 10) ?: 10);
111
112
        return $this
113
            ->httpResponse()
114
            ->setData(ListCategoryResource::collection($data))
115
            ->toApiResponse();
116
    }
117
118
    /**
119
     *  Get category by slug
120
     *
121
     * @group Blog
122
     * @queryParam slug Find by slug of category.
123
     *
124
     * @param  string  $slug
125
     *
126
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
127
     */
128
    #[
129
        Get(
130
            path: "/categories/{slug}",
131
            operationId: "categoryFilterBySlug",
132
            description: "Get the category by slug
133
            
134
    This API will get records from the database and return the category by slug.
135
            ",
136
            summary: "Get category by slug",
137
            security: [['sanctum' => []]],
138
            tags: ["Category"],
139
            parameters: [
140
                new Parameter(
141
                    name: 'slug',
142
                    description: 'Category slug',
143
                    in: 'path',
144
                    required: true,
145
                    schema: new Schema(type: 'string', example: 'php')
146
                ),
147
            ],
148
            responses: [
149
                new Response(
150
                    response: 200,
151
                    description: "Get category successfully",
152
                    content: new JsonContent(
153
                        properties: [
154
                            new Property(
155
                                property: 'error',
156
                                description: 'Error status',
157
                                type: 'boolean',
158
                                default: false
159
                            ),
160
                            new Property(
161
                                property: "data",
162
                                ref: CategoryListResourceSchema::class,
163
                                description: "Data of model",
164
                                type: "object",
165
                            ),
166
                        ]
167
                    )
168
                ),
169
                new Response(
170
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
171
                    response: 400,
172
                ),
173
                new Response(
174
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
175
                    response: 404,
176
                ),
177
                new Response(
178
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
179
                    response: 500,
180
                ),
181
            ]
182
        )
183
    ]
184
    public function findBySlug(string $slug): JsonResponse|RedirectResponse|JsonResource|BaseHttpResponse
185
    {
186
        /** @var Slug $slug */
187
        $slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Category::getBaseModel()));
188
189
        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...
190
            return $this
191
                ->httpResponse()
192
                ->setError()
193
                ->setCode(404)
194
                ->setMessage('Not found');
195
        }
196
197
        $category = Category::query()
198
            ->with(['slugable'])
199
            ->where([
200
                'id' => $slug->reference_id,
201
                'status' => StatusEnum::PUBLISHED,
202
            ])
203
            ->first();
204
205
        if (!$category) {
206
            return $this
207
                ->httpResponse()
208
                ->setError()
209
                ->setCode(404)
210
                ->setMessage('Not found');
211
        }
212
213
        return $this
214
            ->httpResponse()
215
            ->setData(ListCategoryResource::make($category))
216
            ->toApiResponse();
217
    }
218
}
219