Conditions | 3 |
Paths | 3 |
Total Lines | 115 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
164 |