Passed
Push — main ( 8a97d0...8510ef )
by Tan
06:04 queued 02:41
created

MetaBoxGetByRequestModelAction::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 115
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

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