AuthorGetProfileAction   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 78
c 6
b 0
f 0
dl 0
loc 121
rs 10
wmc 2

1 Method

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