Conditions | 6 |
Paths | 8 |
Total Lines | 59 |
Code Lines | 38 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
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 |
||
141 | #[Route('/send-gotify', name: '_core_push_notification_send_gotify')] |
||
142 | public function sendGotify(): JsonResponse |
||
143 | { |
||
144 | $user = $this->userHelper->getCurrent(); |
||
145 | |||
146 | if (!$user) { |
||
147 | return new JsonResponse([ |
||
148 | 'error' => $this->translator->trans('User not found.'), |
||
149 | ], 403); |
||
150 | } |
||
151 | |||
152 | $settings = $this->settingsManager->getSetting('platform.push_notification_settings', true); |
||
153 | |||
154 | if (empty($settings)) { |
||
155 | return new JsonResponse([ |
||
156 | 'error' => $this->translator->trans('No push notification settings configured.'), |
||
157 | ], 500); |
||
158 | } |
||
159 | |||
160 | $decoded = json_decode($settings, true); |
||
161 | |||
162 | $gotifyUrl = $decoded['gotify_url'] ?? null; |
||
163 | $gotifyToken = $decoded['gotify_token'] ?? null; |
||
164 | |||
165 | if (!$gotifyUrl || !$gotifyToken) { |
||
166 | return new JsonResponse([ |
||
167 | 'error' => $this->translator->trans('Gotify configuration is missing.'), |
||
168 | ], 500); |
||
169 | } |
||
170 | |||
171 | // Prepare the payload for Gotify |
||
172 | $payload = [ |
||
173 | 'title' => $user->getEmail(), |
||
174 | 'message' => $this->translator->trans('This is a test notification sent to Gotify from this platform.'), |
||
175 | 'priority' => 5, |
||
176 | ]; |
||
177 | |||
178 | $client = HttpClient::create(); |
||
179 | |||
180 | try { |
||
181 | $response = $client->request('POST', rtrim($gotifyUrl, '/') . '/message', [ |
||
182 | 'headers' => [ |
||
183 | 'X-Gotify-Key' => $gotifyToken, |
||
184 | ], |
||
185 | 'json' => $payload, |
||
186 | ]); |
||
187 | |||
188 | $statusCode = $response->getStatusCode(); |
||
189 | $content = $response->toArray(false); |
||
190 | |||
191 | return new JsonResponse([ |
||
192 | 'message' => $this->translator->trans('Gotify notification has been sent.'), |
||
193 | 'status' => $statusCode, |
||
194 | 'response' => $content, |
||
195 | ]); |
||
196 | } catch (\Throwable $e) { |
||
197 | return new JsonResponse([ |
||
198 | 'error' => $this->translator->trans('Error sending notification to Gotify: ') . $e->getMessage(), |
||
199 | ], 500); |
||
200 | } |
||
203 |