| Conditions | 2 |
| Paths | 4 |
| Total Lines | 80 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 31 | #[ |
||
| 32 | Post( |
||
| 33 | path: "/posts/{id}/increment-views", |
||
| 34 | operationId: "incrementViewCountPostById", |
||
| 35 | description: "Increment views count of the post by ID. Only adds 1 view per IP within a time period specified in the environment configuration.", |
||
| 36 | summary: "Increment views count of the post by ID", |
||
| 37 | tags: ["Post"], |
||
| 38 | parameters: [ |
||
| 39 | new Parameter( |
||
| 40 | name: 'id', |
||
| 41 | description: 'Post Id', |
||
| 42 | in: 'path', |
||
| 43 | required: true, |
||
| 44 | schema: new Schema(type: 'integer', example: 1) |
||
| 45 | ), |
||
| 46 | ], |
||
| 47 | responses: [ |
||
| 48 | new Response( |
||
| 49 | response: 200, |
||
| 50 | description: "Success", |
||
| 51 | content: new JsonContent( |
||
| 52 | properties: [ |
||
| 53 | new Property( |
||
| 54 | property: 'error', |
||
| 55 | description: 'Error status', |
||
| 56 | type: 'boolean', |
||
| 57 | default: false |
||
| 58 | ), |
||
| 59 | new Property( |
||
| 60 | property: "data", |
||
| 61 | ref: ViewCountResourceSchema::class, |
||
| 62 | description: "Updated view count data", |
||
| 63 | type: "object", |
||
| 64 | ), |
||
| 65 | ] |
||
| 66 | ) |
||
| 67 | ), |
||
| 68 | new Response( |
||
| 69 | ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class, |
||
| 70 | response: 400, |
||
| 71 | ), |
||
| 72 | new Response( |
||
| 73 | ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class, |
||
| 74 | response: 404, |
||
| 75 | ), |
||
| 76 | new Response( |
||
| 77 | ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class, |
||
| 78 | response: 500, |
||
| 79 | ), |
||
| 80 | new Response( |
||
| 81 | ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\TooManyRequestsResponseSchema::class, |
||
| 82 | response: 429, |
||
| 83 | ), |
||
| 84 | ] |
||
| 85 | ) |
||
| 86 | ] |
||
| 87 | public function __invoke(Request $request, int $id): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse |
||
| 88 | { |
||
| 89 | $ipAddress = $request->header('X-Forwarded-For') ?? $request->ip(); |
||
| 90 | $userAgent = $request->userAgent(); |
||
| 91 | |||
| 92 | DB::beginTransaction(); |
||
| 93 | |||
| 94 | try { |
||
| 95 | $post = $this->visitorLogsService->trackPostView($id, $ipAddress, $userAgent); |
||
|
|
|||
| 96 | |||
| 97 | DB::commit(); |
||
| 98 | |||
| 99 | return $this |
||
| 100 | ->httpResponse() |
||
| 101 | ->setData(new ViewCountResource($post)) |
||
| 102 | ->toApiResponse(); |
||
| 103 | } catch (\Exception $exception) { |
||
| 104 | DB::rollBack(); |
||
| 105 | |||
| 106 | return $this |
||
| 107 | ->httpResponse() |
||
| 108 | ->setError() |
||
| 109 | ->setStatusCode(500) |
||
| 110 | ->setMessage($exception->getMessage()); |
||
| 111 | } |
||
| 114 |