Passed
Pull Request — main (#69)
by
unknown
03:27
created

PostViewCountAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 72
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 63 1
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Actions\Post;
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\Post\ViewCountResource;
7
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Post\ViewCountResourceSchema;
8
use CSlant\Blog\Core\Http\Actions\Action;
9
use CSlant\Blog\Core\Models\Post;
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
class PostViewCountAction extends Action
22
{
23
    protected PostService $postService;
0 ignored issues
show
Bug introduced by
The type CSlant\Blog\Api\Http\Actions\Post\PostService 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...
24
25
    public function __construct(PostService $postService)
26
    {
27
        $this->postService = $postService;
28
    }
29
30
    #[
31
        Get(
32
            path: "/posts/{id}/increment-views",
33
            operationId: "incrementViewCountPostById",
34
            description: "Increment views count of the post by ID. Only adds 1 view per IP in 1 hour.",
35
            summary: "Increment views count of the post by ID",
36
            tags: ["Post"],
37
            parameters: [
38
                new Parameter(
39
                    name: 'id',
40
                    description: 'Post Id',
41
                    in: 'path',
42
                    required:  true,
43
                    schema: new Schema(type: 'integer', example: 1)
44
                ),
45
            ],
46
            responses: [
47
                new Response(
48
                    response: 200,
49
                    description: "Success",
50
                    content: new JsonContent(
51
                        properties: [
52
                            new Property(
53
                                property: 'error',
54
                                description: 'Error status',
55
                                type: 'boolean',
56
                                default: false
57
                            ),
58
                            new Property(
59
                                property: "data",
60
                                ref: ViewCountResourceSchema::class,
61
                                description: "Updated view count data",
62
                                type: "object",
63
                            ),
64
                        ]
65
                    )
66
                ),
67
                new Response(
68
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
69
                    response: 400,
70
                ),
71
                new Response(
72
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
73
                    response: 404,
74
                ),
75
                new Response(
76
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
77
                    response: 500,
78
                ),
79
            ]
80
        )
81
    ]
82
    public function __invoke(Request $request, int $id): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
83
    {
84
        $post = Post::findOrFail($id);
85
86
        $ipAddress = $request->ip();
87
        $this->postService->trackView($post->id, $ipAddress);
88
89
        return $this
90
            ->httpResponse()
91
            ->setData(new ViewCountResource($post))
92
            ->toApiResponse();
93
    }
94
}
95