| Conditions | 2 |
| Paths | 2 |
| Total Lines | 109 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |
||
| 156 |