Passed
Pull Request — main (#62)
by Tan
03:01
created

AuthorAction::__invoke()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 104
Code Lines 77

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 77
c 1
b 0
f 1
dl 0
loc 104
rs 8.5018
cc 2
nc 2
nop 2

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\Actions\Author;
4
5
use Botble\ACL\Models\User;
0 ignored issues
show
Bug introduced by
The type Botble\ACL\Models\User 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\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...
7
use CSlant\Blog\Api\Http\Resources\Author\AuthorWithPostResource;
8
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Author\AuthorModelResourceSchema;
9
use CSlant\Blog\Core\Http\Controllers\Base\BasePostController;
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 ViewCountAction
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 AuthorAction extends BasePostController
34
{
35
    /**
36
     * @param  int  $authorId
37
     *
38
     * @group Blog
39
     *
40
     * @queryParam  Find by authorId of user.
41
     *
42
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
43
     */
44
    #[
45
        Get(
46
            path: "/authors/{authorId}",
47
            operationId: "profileAuthorByAuthorId",
48
            description: "Get profile and list post of the author by author id
49
            
50
    This API will get record from the database and return profile and list post of the author by author id.
51
            ",
52
            summary: "Get profile and list post of the author by author id",
53
            tags: ["Author"],
54
            parameters: [
55
                new Parameter(
56
                    name: 'authorId',
57
                    description: 'Author Id',
58
                    in: 'path',
59
                    required: true,
60
                    schema: new Schema(type: 'string', example: 'php')
61
                ),
62
                new Parameter(
63
                    name: 'order_by',
64
                    description: 'Can order by field: id, slug, created_at, ...',
65
                    in: 'query',
66
                    required: false,
67
                    schema: new Schema(type: 'string', default: 'created_at')
68
                ),
69
                new Parameter(
70
                    name: 'order',
71
                    description: 'Order direction: 
72
                        ASC for ascending
73
                        DESC for descending',
74
                    in: 'query',
75
                    required: false,
76
                    schema: new Schema(type: 'string', default: 'ASC', enum: ['ASC', 'DESC'])
77
                ),
78
                new Parameter(
79
                    name: 'per_page',
80
                    description: 'Number of items per page',
81
                    in: 'query',
82
                    required: false,
83
                    schema: new Schema(type: 'integer', default: 10)
84
                ),
85
                new Parameter(
86
                    name: 'page',
87
                    description: 'Page number',
88
                    in: 'query',
89
                    required: false,
90
                    schema: new Schema(type: 'integer', default: 1)
91
                ),
92
            ],
93
            responses: [
94
                new Response(
95
                    response: 200,
96
                    description: "Get author and list posts successfully",
97
                    content: new JsonContent(
98
                        properties: [
99
                            new Property(
100
                                property: 'error',
101
                                description: 'Error status',
102
                                type: 'boolean',
103
                                default: false
104
                            ),
105
                            new Property(
106
                                property: "data",
107
                                ref: AuthorModelResourceSchema::class,
108
                                description: "Data of model",
109
                                type: "object",
110
                            ),
111
                        ]
112
                    )
113
                ),
114
                new Response(
115
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
116
                    response: 400,
117
                ),
118
                new Response(
119
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
120
                    response: 404,
121
                ),
122
                new Response(
123
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
124
                    response: 500,
125
                ),
126
            ]
127
        )
128
    ]
129
    public function __invoke(int $authorId, Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
130
    {
131
        $user = User::query()
132
            ->with('posts')
133
            ->whereId($authorId)
134
            ->first();
135
136
        if (!$user) {
137
            return $this
138
                ->httpResponse()
139
                ->setError()
140
                ->setCode(404)
141
                ->setMessage('Not found');
142
        }
143
144
        return $this
145
            ->httpResponse()
146
            ->setData(new AuthorWithPostResource($user))
147
            ->toApiResponse();
148
    }
149
}
150