Passed
Pull Request — main (#100)
by Tan
05:26
created

PostGetNavigateAction::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 86
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 3
eloc 62
c 3
b 2
f 0
nc 3
nop 1
dl 0
loc 86
rs 8.829

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