MetaBoxGetByRequestModelAction::__invoke()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 115
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 82
c 2
b 1
f 1
nc 3
nop 1
dl 0
loc 115
rs 8.3927

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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