Conditions | 3 |
Paths | 3 |
Total Lines | 115 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
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 | } |
||
163 |