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

PostGetNavigateAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 47
c 2
b 1
f 0
dl 0
loc 80
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __invoke() 0 64 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\PostNavigateResource;
7
use CSlant\Blog\Api\Services\PostService;
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\Parameter;
18
use OpenApi\Attributes\Response;
19
use OpenApi\Attributes\Schema;
20
21
/**
22
 * Class PostGetNavigateAction
23
 *
24
 * @group Blog API
25
 *
26
 * @authenticated
27
 *
28
 * @method BaseHttpResponse httpResponse()
29
 * @method BaseHttpResponse setData(mixed $data)
30
 * @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse()
31
 */
32
class PostGetNavigateAction extends Action
33
{
34
    protected PostService $postService;
35
36
    public function __construct(PostService $postService)
37
    {
38
        $this->postService = $postService;
39
    }
40
41
    /**
42
     * @param  string  $slug
43
     *
44
     * @group Blog
45
     * @queryParam Find by slug of post.
46
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
47
     */
48
    #[
49
        Get(
50
            path: "/posts/{slug}/navigate",
51
            operationId: "postGetNavigate",
52
            description: "Get the previous and next posts by slug
53
This API will return both previous and next posts for navigation purposes.
54
            ",
55
            summary: "Get previous and next posts for navigation",
56
            tags: ["Post"],
57
            parameters: [
58
                new Parameter(
59
                    name: 'slug',
60
                    description: 'Post slug',
61
                    in: 'path',
62
                    required: true,
63
                    schema: new Schema(type: 'string')
64
                ),
65
            ],
66
            responses: [
67
                new Response(
68
                    response: 200,
69
                    description: 'Navigation posts retrieved successfully',
70
                ),
71
                new Response(
72
                    response: 404,
73
                    description: 'Post not found',
74
                ),
75
            ]
76
        )
77
    ]
78
    public function __invoke(string $slug): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
79
    {
80
        /** @var Slug $slugModel */
81
        $slugModel = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Post::getBaseModel()));
82
83
        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...
84
            return $this
85
                ->httpResponse()
86
                ->setError()
87
                ->setCode(404)
88
                ->setMessage('Not found');
89
        }
90
91
        $currentPost = Post::query()
92
            ->where('id', $slugModel->reference_id)
93
            ->where('status', StatusEnum::PUBLISHED)
94
            ->with(['categories', 'tags'])
95
            ->first();
96
97
        if (!$currentPost) {
98
            return $this
99
                ->httpResponse()
100
                ->setError()
101
                ->setCode(404)
102
                ->setMessage('Not found');
103
        }
104
105
        // Using service method for complex business logic
106
        $navigationPosts = $this->postService->getNavigationPosts($currentPost);
107
108
        return $this
109
            ->httpResponse()
110
            ->setData(new PostNavigateResource($navigationPosts))
111
            ->toApiResponse();
112
    }
113
}
114