PostGetViewCountAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 95
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 86 3
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\Enums\StatusEnum;
9
use CSlant\Blog\Core\Facades\Base\SlugHelper;
10
use CSlant\Blog\Core\Http\Actions\Action;
11
use CSlant\Blog\Core\Models\Post;
12
use CSlant\Blog\Core\Models\Slug;
13
use Illuminate\Http\JsonResponse;
14
use Illuminate\Http\RedirectResponse;
15
use Illuminate\Http\Resources\Json\JsonResource;
16
use OpenApi\Attributes\Get;
17
use OpenApi\Attributes\JsonContent;
18
use OpenApi\Attributes\Parameter;
19
use OpenApi\Attributes\Property;
20
use OpenApi\Attributes\Response;
21
use OpenApi\Attributes\Schema;
22
23
/**
24
 * Class ViewCountAction
25
 *
26
 * @package CSlant\Blog\Api\Http\Controllers\Actions\Post
27
 *
28
 * @group Blog API
29
 *
30
 * @authenticated
31
 *
32
 * @method BaseHttpResponse httpResponse()
33
 * @method BaseHttpResponse setData(mixed $data)
34
 * @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse()
35
 */
36
class PostGetViewCountAction extends Action
37
{
38
    /**
39
     * @param  string  $slug
40
     *
41
     * @group Blog
42
     * @queryParam Find by slug of post.
43
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
44
     */
45
    #[
46
        Get(
47
            path: "/posts/{slug}/view-count",
48
            operationId: "viewCountPostBySlug",
49
            description: "Get views count of the post by slug
50
            
51
    This API will get record from the database and return views count of the post by slug.
52
            ",
53
            summary: "Get views count of the post by slug",
54
            tags: ["Post"],
55
            parameters: [
56
                new Parameter(
57
                    name: 'slug',
58
                    description: 'Post slug',
59
                    in: 'path',
60
                    required: true,
61
                    schema: new Schema(type: 'string', example: 'php')
62
                ),
63
            ],
64
            responses: [
65
                new Response(
66
                    response: 200,
67
                    description: "Get views count successfully",
68
                    content: new JsonContent(
69
                        properties: [
70
                            new Property(
71
                                property: 'error',
72
                                description: 'Error status',
73
                                type: 'boolean',
74
                                default: false
75
                            ),
76
                            new Property(
77
                                property: "data",
78
                                ref: ViewCountResourceSchema::class,
79
                                description: "Data of model",
80
                                type: "object",
81
                            ),
82
                        ]
83
                    )
84
                ),
85
                new Response(
86
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
87
                    response: 400,
88
                ),
89
                new Response(
90
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
91
                    response: 404,
92
                ),
93
                new Response(
94
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
95
                    response: 500,
96
                ),
97
            ]
98
        )
99
    ]
100
    public function __invoke(string $slug): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
101
    {
102
        /** @var Slug $slug */
103
        $slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Post::getBaseModel()));
104
105
        if (!$slug) {
0 ignored issues
show
introduced by
$slug is of type CSlant\Blog\Core\Models\Slug, thus it always evaluated to true.
Loading history...
106
            return $this
107
                ->httpResponse()
108
                ->setError()
109
                ->setCode(404)
110
                ->setMessage('Not found');
111
        }
112
113
        $post = Post::query()
114
            ->select(['id', 'views'])
115
            ->whereId($slug->reference_id)
116
            ->where('status', StatusEnum::PUBLISHED)
117
            ->first();
118
119
        if (!$post) {
120
            return $this
121
                ->httpResponse()
122
                ->setError()
123
                ->setCode(404)
124
                ->setMessage('Not found');
125
        }
126
127
        return $this
128
            ->httpResponse()
129
            ->setData(new ViewCountResource($post))
130
            ->toApiResponse();
131
    }
132
}
133