Conditions | 11 |
Paths | 15 |
Total Lines | 106 |
Code Lines | 68 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
32 | #[Route('/send/{userId}', name: 'chamilo_core_push_notification_send', methods: ['GET'])] |
||
33 | public function send(int $userId, UserRepository $userRepository, Request $request): JsonResponse |
||
34 | { |
||
35 | $currentUser = $this->userHelper->getCurrent(); |
||
36 | |||
37 | if (!$currentUser) { |
||
38 | return new JsonResponse([ |
||
39 | 'error' => $this->translator->trans('Current user not found.'), |
||
40 | ], 403); |
||
41 | } |
||
42 | |||
43 | // Check permission (example: only admins) |
||
44 | if (!$currentUser->isAdmin()) { |
||
45 | return new JsonResponse([ |
||
46 | 'error' => $this->translator->trans('You do not have permission to send notifications to other users.'), |
||
47 | ], 403); |
||
48 | } |
||
49 | |||
50 | // Find target user |
||
51 | $user = $userRepository->find($userId); |
||
52 | |||
53 | if (!$user) { |
||
54 | return new JsonResponse([ |
||
55 | 'error' => $this->translator->trans("This user doesn't exist"), |
||
56 | ], 404); |
||
57 | } |
||
58 | |||
59 | $settings = $this->settingsManager->getSetting('platform.push_notification_settings', true); |
||
60 | |||
61 | if (empty($settings)) { |
||
62 | return new JsonResponse([ |
||
63 | 'error' => $this->translator->trans('No push notification setting configured.'), |
||
64 | ], 500); |
||
65 | } |
||
66 | |||
67 | $decoded = json_decode($settings, true); |
||
68 | |||
69 | $vapidPublicKey = $decoded['vapid_public_key'] ?? null; |
||
70 | $vapidPrivateKey = $decoded['vapid_private_key'] ?? null; |
||
71 | |||
72 | if (!$vapidPublicKey || !$vapidPrivateKey) { |
||
73 | return new JsonResponse([ |
||
74 | 'error' => $this->translator->trans('VAPID keys are missing in the configuration.'), |
||
75 | ], 500); |
||
76 | } |
||
77 | |||
78 | $subscriptions = $this->subscriptionRepository->findByUser($user); |
||
79 | |||
80 | if (empty($subscriptions)) { |
||
81 | return new JsonResponse([ |
||
82 | 'error' => $this->translator->trans('No push subscriptions found for this user.'), |
||
83 | ], 404); |
||
84 | } |
||
85 | |||
86 | $webPush = new WebPush([ |
||
87 | 'VAPID' => [ |
||
88 | 'subject' => 'mailto:' . $user->getEmail(), |
||
89 | 'publicKey' => $vapidPublicKey, |
||
90 | 'privateKey' => $vapidPrivateKey, |
||
91 | ], |
||
92 | ]); |
||
93 | |||
94 | $successes = []; |
||
95 | $failures = []; |
||
96 | |||
97 | foreach ($subscriptions as $subEntity) { |
||
98 | try { |
||
99 | $subscription = Subscription::create([ |
||
100 | 'endpoint' => $subEntity->getEndpoint(), |
||
101 | 'publicKey' => $subEntity->getPublicKey(), |
||
102 | 'authToken' => $subEntity->getAuthToken(), |
||
103 | 'contentEncoding' => $subEntity->getContentEncoding() ?? 'aesgcm', |
||
104 | ]); |
||
105 | |||
106 | $payload = json_encode([ |
||
107 | 'title' => $this->translator->trans('Push notification test'), |
||
108 | 'message' => $this->translator->trans("This is a test push notification from this platform to the user's browser or app."), |
||
109 | 'url' => '/account/edit' |
||
110 | ]); |
||
111 | |||
112 | $report = $webPush->sendOneNotification( |
||
113 | $subscription, |
||
114 | $payload |
||
115 | ); |
||
116 | |||
117 | if ($report->isSuccess()) { |
||
118 | $successes[] = $subEntity->getEndpoint(); |
||
119 | } else { |
||
120 | $failures[] = [ |
||
121 | 'endpoint' => $subEntity->getEndpoint(), |
||
122 | 'reason' => $report->getReason(), |
||
123 | 'statusCode' => $report->getResponse()->getStatusCode(), |
||
124 | ]; |
||
125 | } |
||
126 | } catch (\Throwable $e) { |
||
127 | $failures[] = [ |
||
128 | 'endpoint' => $subEntity->getEndpoint(), |
||
129 | 'reason' => $e->getMessage(), |
||
130 | ]; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | return new JsonResponse([ |
||
135 | 'message' => $this->translator->trans('Push notifications have been processed.'), |
||
136 | 'success' => $successes, |
||
137 | 'failures' => $failures, |
||
138 | ]); |
||
203 |