Conditions | 3 |
Paths | 3 |
Total Lines | 111 |
Code Lines | 79 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 2 |
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 |
||
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 | default: 'post', |
||
65 | enum: ['post', 'page', 'category', 'tag'] |
||
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 | default: 'post-slug' |
||
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(string $model, string $slug, string $lang = AppConstant::DEFAULT_LOCALE) |
||
128 | { |
||
129 | $slugModel = $this->slugService->getSlugModel($slug, $model); |
||
130 | |||
131 | if (!$slugModel) { |
||
132 | return $this |
||
133 | ->httpResponse() |
||
134 | ->setError() |
||
135 | ->setStatusCode(404) |
||
136 | ->setMessage(__('Slug not found!')) |
||
137 | ->toApiResponse(); |
||
138 | } |
||
139 | |||
140 | $metaBox = $this->metaBoxService->getMetaBoxByModel($model, $slugModel->reference_id, $lang); |
||
141 | |||
142 | if (!$metaBox) { |
||
143 | return $this |
||
144 | ->httpResponse() |
||
145 | ->setError() |
||
146 | ->setStatusCode(404) |
||
147 | ->setMessage(__('MetaBox not found!')) |
||
148 | ->toApiResponse(); |
||
149 | } |
||
150 | |||
151 | return $this |
||
152 | ->httpResponse() |
||
153 | ->setData(MetaBoxResource::make($metaBox)) |
||
154 | ->setMessage(__('MetaBox retrieved successfully!')) |
||
155 | ->toApiResponse(); |
||
156 | } |
||
158 |