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

PostViewCountAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Api\Services\PostService;
9
use CSlant\Blog\Core\Http\Actions\Action;
10
use CSlant\Blog\Core\Models\Post;
11
use Illuminate\Http\JsonResponse;
12
use Illuminate\Http\RedirectResponse;
13
use Illuminate\Http\Request;
14
use Illuminate\Http\Resources\Json\JsonResource;
15
use OpenApi\Attributes\Get;
16
use OpenApi\Attributes\JsonContent;
17
use OpenApi\Attributes\Parameter;
18
use OpenApi\Attributes\Property;
19
use OpenApi\Attributes\Response;
20
use OpenApi\Attributes\Schema;
21
22
class PostViewCountAction extends Action
23
{
24
    protected PostService $postService;
25
26
    public function __construct(PostService $postService)
27
    {
28
        $this->postService = $postService;
29
    }
30
31
    #[
32
        Get(
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 in 1 hour.",
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
            ]
81
        )
82
    ]
83
    public function __invoke(Request $request, int $id): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
84
    {
85
        $post = Post::findOrFail($id);
86
87
        $ipAddress = $request->header('X-Forwarded-For')
88
            ?? $request->header('X-Real-IP')
89
            ?? $request->ip()
90
            ?? '127.0.0.1';
91
92
        $this->postService->trackView($post->id, $ipAddress);
0 ignored issues
show
Bug introduced by
It seems like $ipAddress can also be of type array; however, parameter $ipAddress of CSlant\Blog\Api\Services\PostService::trackView() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
        $this->postService->trackView($post->id, /** @scrutinizer ignore-type */ $ipAddress);
Loading history...
93
94
        return $this
95
            ->httpResponse()
96
            ->setData(new ViewCountResource($post))
97
            ->toApiResponse();
98
    }
99
}
100