Passed
Push — main ( a422fb...18c4ea )
by Tan
03:26
created

MetaBoxGetBySlugAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 84
c 2
b 0
f 2
dl 0
loc 123
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 111 3
1
<?php
2
3
namespace CSlant\Blog\Api\Http\Actions\MetaBox;
4
5
use CSlant\Blog\Api\Http\Resources\MetaBox\MetaBoxResource;
6
use CSlant\Blog\Api\OpenApi\Schemas\Resources\MetaBox\MetaBoxModelResourceSchema;
7
use CSlant\Blog\Api\Services\MetaBoxService;
8
use CSlant\Blog\Api\Services\SlugService;
9
use CSlant\Blog\Core\Constants\AppConstant;
10
use CSlant\Blog\Core\Http\Actions\Action;
11
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse;
12
use Illuminate\Http\JsonResponse;
13
use Illuminate\Http\RedirectResponse;
14
use Illuminate\Http\Resources\Json\JsonResource;
15
use OpenApi\Attributes\Get;
16
use OpenApi\Attributes\JsonContent;
17
use OpenApi\Attributes\Parameter;
18
use OpenApi\Attributes\Property;
19
use OpenApi\Attributes\Response;
20
use OpenApi\Attributes\Schema;
21
22
/**
23
 * Class MetaBoxGetBySlugAction
24
 *
25
 * @group Blog API
26
 *
27
 * @authenticated
28
 *
29
 * @method BaseHttpResponse httpResponse()
30
 * @method BaseHttpResponse setData(mixed $data)
31
 * @method BaseHttpResponse|JsonResource|JsonResponse|RedirectResponse toApiResponse()
32
 */
33
class MetaBoxGetBySlugAction extends Action
34
{
35
    protected MetaBoxService $metaBoxService;
36
37
    protected SlugService $slugService;
38
39
    public function __construct(MetaBoxService $metaBoxService, SlugService $slugService)
40
    {
41
        $this->metaBoxService = $metaBoxService;
42
        $this->slugService = $slugService;
43
    }
44
45
    #[
46
        Get(
47
            path: "/meta-boxes/{model}/{modelSlug}/{lang?}",
48
            operationId: "metaBoxGetBySlugModel",
49
            description: "Get the meta data by slug and model.
50
            
51
    This API will get the meta SEO data by slug and model.
52
    The model can be one of the following: post, page, category, tag, etc.
53
            ",
54
            summary: "Get meta data by slug and model",
55
            tags: ["MetaBox"],
56
            parameters: [
57
                new Parameter(
58
                    name: 'model',
59
                    description: 'The model name. Can be one of the following: post, page, category, tag, etc.',
60
                    in: 'path',
61
                    required: true,
62
                    schema: new Schema(
63
                        type: 'string',
64
                        default: 'post',
65
                        enum: ['post', 'page', 'category', 'tag']
66
                    )
67
                ),
68
                new Parameter(
69
                    name: 'modelSlug',
70
                    description: 'The slug of the model. Can be one of the following: post, page, category, tag, etc.
71
                    
72
    Example: post-slug, page-slug, category-slug, tag-slug, etc.',
73
                    in: 'path',
74
                    required: true,
75
                    schema: new Schema(
76
                        type: 'string',
77
                        default: 'post-slug'
78
                    )
79
                ),
80
                new Parameter(
81
                    name: 'lang',
82
                    description: 'The language code. Default is en.',
83
                    in: 'path',
84
                    required: false,
85
                    schema: new Schema(
86
                        type: 'string',
87
                        default: AppConstant::DEFAULT_LOCALE
88
                    )
89
                ),
90
            ],
91
            responses: [
92
                new Response(
93
                    response: 200,
94
                    description: "Get meta data by slug and model",
95
                    content: new JsonContent(
96
                        properties: [
97
                            new Property(
98
                                property: 'error',
99
                                description: 'Error status',
100
                                type: 'boolean',
101
                                default: false
102
                            ),
103
                            new Property(
104
                                property: "data",
105
                                ref: MetaBoxModelResourceSchema::class,
106
                                description: "Data of the meta box",
107
                                type: "object",
108
                            ),
109
                        ]
110
                    )
111
                ),
112
                new Response(
113
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\BadRequestResponseSchema::class,
114
                    response: 400,
115
                ),
116
                new Response(
117
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\ErrorNotFoundResponseSchema::class,
118
                    response: 404,
119
                ),
120
                new Response(
121
                    ref: \CSlant\Blog\Api\OpenApi\Responses\Errors\InternalServerResponseSchema::class,
122
                    response: 500,
123
                ),
124
            ]
125
        )
126
    ]
127
    public function __invoke(string $model, string $slug, string $lang = AppConstant::DEFAULT_LOCALE)
128
    {
129
        $slugModel = $this->slugService->getSlugModel($slug, $model);
130
131
        if (!$slugModel) {
132
            return $this
133
                ->httpResponse()
134
                ->setError()
135
                ->setStatusCode(404)
136
                ->setMessage(__('Slug not found!'))
137
                ->toApiResponse();
138
        }
139
140
        $metaBox = $this->metaBoxService->getMetaBoxByModel($model, $slugModel->reference_id, $lang);
141
142
        if (!$metaBox) {
143
            return $this
144
                ->httpResponse()
145
                ->setError()
146
                ->setStatusCode(404)
147
                ->setMessage(__('MetaBox not found!'))
148
                ->toApiResponse();
149
        }
150
151
        return $this
152
            ->httpResponse()
153
            ->setData(MetaBoxResource::make($metaBox))
154
            ->setMessage(__('MetaBox retrieved successfully!'))
155
            ->toApiResponse();
156
    }
157
}
158