Passed
Pull Request — main (#100)
by
unknown
05:33
created

PostGetNavigateAction::httpResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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. This API will return both previous and next posts for navigation purposes.",
53
            summary: "Get previous and next posts for navigation",
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 previous and next posts by slug 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: PostNavigateResourceSchema::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 $slugModel */
103
        $slugModel = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Post::getBaseModel()));
104
105
        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...
106
            return $this
107
                ->httpResponse()
108
                ->setError()
109
                ->setCode(404)
110
                ->setMessage('Not found');
111
        }
112
113
        $currentPost = Post::query()
114
            ->where('id', $slugModel->reference_id)
115
            ->where('status', StatusEnum::PUBLISHED)
116
            ->with(['categories', 'tags'])
117
            ->first();
118
119
        if (!$currentPost) {
120
            return $this
121
                ->httpResponse()
122
                ->setError()
123
                ->setCode(404)
124
                ->setMessage('Not found');
125
        }
126
127
        // Using service method for complex business logic
128
        $navigationPosts = $this->postService->getNavigationPosts($currentPost);
129
130
        return $this
131
            ->httpResponse()
132
            ->setData(new PostNavigateResource($navigationPosts))
133
            ->toApiResponse();
134
    }
135
}
136