Passed
Pull Request — main (#99)
by Tan
03:05
created

PostGetNavigateAction::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 49
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 33
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 49
rs 9.392
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Actions\Post;
4
5
use Botble\Blog\Repositories\Interfaces\PostInterface;
0 ignored issues
show
Bug introduced by
The type Botble\Blog\Repositories\Interfaces\PostInterface 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\Core\Facades\Base\SlugHelper;
8
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse;
9
use CSlant\Blog\Core\Models\Post;
10
use CSlant\Blog\Core\Models\Slug;
11
use Illuminate\Http\JsonResponse;
12
use Illuminate\Http\RedirectResponse;
13
use Illuminate\Http\Resources\Json\JsonResource;
14
use OpenApi\Attributes\Get;
15
use OpenApi\Attributes\Parameter;
16
use OpenApi\Attributes\Response;
17
use OpenApi\Attributes\Schema;
18
19
/**
20
 * Class PostGetNavigateAction
21
 *
22
 * @package CSlant\Blog\Api\Http\Actions\Post
23
 *
24
 * @method BaseHttpResponse httpResponse()
25
 * @method BaseHttpResponse setData(mixed $data)
26
 * @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse()
27
 */
28
class PostGetNavigateAction
29
{
30
    public function __construct(
31
        protected PostInterface $postRepository
32
    ) {
33
    }
34
35
    /**
36
     * @group Blog API
37
     *
38
     * @param  string  $slug
39
     *
40
     * @return BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse
41
     */
42
    #[
43
        Get(
44
            path: "/posts/{slug}/navigate",
45
            operationId: "postGetNavigate",
46
            description: "Get the previous and next posts by slug
47
This API will return both previous and next posts for navigation purposes.
48
            ",
49
            summary: "Get previous and next posts for navigation",
50
            tags: ["Post"],
51
            parameters: [
52
                new Parameter(
53
                    name: 'slug',
54
                    description: 'Post slug',
55
                    in: 'path',
56
                    required: true,
57
                    schema: new Schema(type: 'string')
58
                ),
59
            ],
60
            responses: [
61
                new Response(
62
                    response: 200,
63
                    description: 'Navigation posts retrieved successfully',
64
                ),
65
                new Response(
66
                    response: 404,
67
                    description: 'Post not found',
68
                ),
69
            ]
70
        )
71
    ]
72
    public function __invoke(string $slug): BaseHttpResponse|JsonResponse|JsonResource|RedirectResponse
73
    {
74
        /** @var Slug $slug */
75
        $slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Post::getBaseModel()));
76
77
        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...
78
            return $this
79
                ->httpResponse()
80
                ->setError()
81
                ->setCode(404)
82
                ->setMessage('Post not found');
83
        }
84
85
        $navigationPosts = $this->postRepository->getNavigatePosts($slug->reference_id);
86
87
        return $this
88
            ->httpResponse()
89
            ->setData(new PostNavigateResource($navigationPosts))
90
            ->toApiResponse();
91
    }
92
93
    /**
94
     * @return BaseHttpResponse
95
     */
96
    protected function httpResponse(): BaseHttpResponse
97
    {
98
        return new BaseHttpResponse;
99
    }
100
}
101