Completed
Push — main ( be9081...572954 )
by Tan
19s queued 14s
created

AuthorGetProfileAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 78
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 104 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  $authorId
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/{authorId}",
48
            operationId: "profileAuthorByAuthorId",
49
            description: "Get profile and list post of the author by author id
50
            
51
    This API will get record from the database and return profile and list post of the author by author id.
52
            ",
53
            summary: "Get profile and list post of the author by author id",
54
            tags: ["Author"],
55
            parameters: [
56
                new Parameter(
57
                    name: 'authorId',
58
                    description: 'Author Id',
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',
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',
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(int $authorId, Request $request): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
131
    {
132
        $user = User::query()
133
            ->with('posts')
134
            ->whereId($authorId)
135
            ->first();
136
137
        if (!$user) {
138
            return $this
139
                ->httpResponse()
140
                ->setError()
141
                ->setCode(404)
142
                ->setMessage('Not found');
143
        }
144
145
        return $this
146
            ->httpResponse()
147
            ->setData(AuthorWithPostResource::make($user))
148
            ->toApiResponse();
149
    }
150
}
151