Passed
Pull Request — main (#52)
by
unknown
02:41
created

ViewCountAction::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 89
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 63
c 1
b 0
f 1
dl 0
loc 89
rs 8.8072
cc 3
nc 3
nop 1

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace CSlant\Blog\Api\Http\Controllers\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\Enums\StatusEnum;
7
use CSlant\Blog\Api\Http\Resources\ViewCountResource;
8
use CSlant\Blog\Api\OpenApi\Schemas\Resources\Post\ViewCountResourceSchema;
9
use CSlant\Blog\Core\Facades\Base\SlugHelper;
10
use CSlant\Blog\Core\Http\Controllers\Base\BasePostController;
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 ViewCountAction extends BasePostController
37
{
38
    /**
39
     * @param  string  $slug
40
     *
41
     * @group Blog
42
     * @queryParam slug 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 records from the database and return views count of the post by slug.
52
            ",
53
            summary: "Get views count of the post by slug",
54
            security: [['sanctum' => []]],
55
            tags: ["Post"],
56
            parameters: [
57
                new Parameter(
58
                    name: 'slug',
59
                    description: 'Post slug',
60
                    in: 'path',
61
                    required: true,
62
                    schema: new Schema(type: 'string', example: 'php')
63
                ),
64
            ],
65
            responses: [
66
                new Response(
67
                    response: 200,
68
                    description: "Get views count successfully",
69
                    content: new JsonContent(
70
                        properties: [
71
                            new Property(
72
                                property: 'error',
73
                                description: 'Error status',
74
                                type: 'boolean',
75
                                default: false
76
                            ),
77
                            new Property(
78
                                property: "data",
79
                                ref: ViewCountResourceSchema::class,
80
                                description: "Data of model",
81
                                type: "object",
82
                            ),
83
                        ]
84
                    )
85
                ),
86
                new Response(
87
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
88
                    response: 400,
89
                ),
90
                new Response(
91
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
92
                    response: 404,
93
                ),
94
                new Response(
95
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
96
                    response: 500,
97
                ),
98
            ]
99
        )
100
    ]
101
    public function __invoke(string $slug): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
102
    {
103
        /** @var Slug $slug */
104
        $slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Post::getBaseModel()));
105
106
        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...
107
            return $this
108
                ->httpResponse()
109
                ->setError()
110
                ->setCode(404)
111
                ->setMessage('Not found');
112
        }
113
114
        $post = Post::query()
115
            ->select('id', 'views')
116
            ->where([
117
                'id' => $slug->reference_id,
118
                'status' => StatusEnum::PUBLISHED,
119
            ])
120
            ->first();
121
122
        if (!$post) {
123
            return $this
124
                ->httpResponse()
125
                ->setError()
126
                ->setCode(404)
127
                ->setMessage('Not found');
128
        }
129
130
        return $this
131
            ->httpResponse()
132
            ->setData(new ViewCountResource($post))
133
            ->toApiResponse();
134
    }
135
}
136