MetaBoxGetBySlugAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __invoke() 0 114 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
                        enum: ['post', 'page', 'category', 'tag'],
65
                        example: 'category'
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
                        example: 'php',
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(
128
        string $model,
129
        string $slug,
130
        string $lang = AppConstant::DEFAULT_LOCALE
131
    ): JsonResponse|BaseHttpResponse|JsonResource|RedirectResponse {
132
        $slugModel = $this->slugService->getSlugModel($slug, $model);
133
134
        if (!$slugModel) {
135
            return $this
136
                ->httpResponse()
137
                ->setError()
138
                ->setStatusCode(404)
139
                ->setMessage(__('Slug not found!'))
140
                ->toApiResponse();
141
        }
142
143
        $metaBox = $this->metaBoxService->getMetaBoxByModel($model, $slugModel->reference_id, $lang);
144
145
        if (!$metaBox) {
146
            return $this
147
                ->httpResponse()
148
                ->setError()
149
                ->setStatusCode(404)
150
                ->setMessage(__('MetaBox not found!'))
151
                ->toApiResponse();
152
        }
153
154
        return $this
155
            ->httpResponse()
156
            ->setData(MetaBoxResource::make($metaBox))
157
            ->setMessage(__('MetaBox retrieved successfully!'))
158
            ->toApiResponse();
159
    }
160
}
161