Ecodev /
my-ichtus
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | namespace Application\Service; |
||||
| 6 | |||||
| 7 | use Application\DBAL\Types\MessageTypeType; |
||||
| 8 | use Application\Model\Bookable; |
||||
| 9 | use Application\Model\Booking; |
||||
| 10 | use Application\Model\Message; |
||||
| 11 | use Application\Model\User; |
||||
| 12 | use Application\Repository\UserRepository; |
||||
| 13 | use Doctrine\ORM\EntityManager; |
||||
| 14 | use Ecodev\Felix\Service\MessageRenderer; |
||||
| 15 | |||||
| 16 | /** |
||||
| 17 | * Service to queue new message for pre-defined purposes. |
||||
| 18 | */ |
||||
| 19 | class MessageQueuer |
||||
| 20 | { |
||||
| 21 | private bool $toFamilyOwner = false; |
||||
| 22 | |||||
| 23 | private readonly UserRepository $userRepository; |
||||
| 24 | |||||
| 25 | 14 | public function __construct( |
|||
| 26 | private readonly EntityManager $entityManager, |
||||
| 27 | private readonly MessageRenderer $messageRenderer, |
||||
| 28 | ) { |
||||
| 29 | 14 | $this->userRepository = $this->entityManager->getRepository(User::class); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 30 | } |
||||
| 31 | |||||
| 32 | 3 | public function queueRegister(User $user): ?Message |
|||
| 33 | { |
||||
| 34 | 3 | $subject = 'Demande de création de compte au Club Nautique Ichtus'; |
|||
| 35 | 3 | $mailParams = [ |
|||
| 36 | 3 | 'token' => $user->createToken(), |
|||
| 37 | 3 | ]; |
|||
| 38 | |||||
| 39 | 3 | $message = $this->createMessage($user, $subject, MessageTypeType::REGISTER, $mailParams); |
|||
| 40 | |||||
| 41 | 3 | return $message; |
|||
| 42 | } |
||||
| 43 | |||||
| 44 | 2 | public function queueUnregister(User $admin, User $unregisteredUser): ?Message |
|||
| 45 | { |
||||
| 46 | 2 | $subject = 'Démission'; |
|||
| 47 | 2 | $mailParams = [ |
|||
| 48 | 2 | 'unregisteredUser' => $unregisteredUser, |
|||
| 49 | 2 | ]; |
|||
| 50 | |||||
| 51 | 2 | $message = $this->createMessage($admin, $subject, MessageTypeType::UNREGISTER, $mailParams); |
|||
| 52 | |||||
| 53 | 2 | return $message; |
|||
| 54 | } |
||||
| 55 | |||||
| 56 | /** |
||||
| 57 | * Queue a reset password email to specified user. |
||||
| 58 | * |
||||
| 59 | * @param User $user The user for which a password reset will be done |
||||
| 60 | */ |
||||
| 61 | 7 | public function queueResetPassword(User $user): ?Message |
|||
| 62 | { |
||||
| 63 | 7 | $subject = 'Demande de modification de mot de passe'; |
|||
| 64 | 7 | $mailParams = [ |
|||
| 65 | 7 | 'token' => $user->createToken(), |
|||
| 66 | 7 | ]; |
|||
| 67 | |||||
| 68 | 7 | $message = $this->createMessage($user, $subject, MessageTypeType::RESET_PASSWORD, $mailParams); |
|||
| 69 | |||||
| 70 | 7 | return $message; |
|||
| 71 | } |
||||
| 72 | |||||
| 73 | /** |
||||
| 74 | * @param Bookable[] $bookables |
||||
| 75 | */ |
||||
| 76 | 3 | public function queueBalance(User $user, iterable $bookables): ?Message |
|||
| 77 | { |
||||
| 78 | 3 | $subject = 'Balance de compte'; |
|||
| 79 | 3 | $mailParams = [ |
|||
| 80 | 3 | 'bookables' => $bookables, |
|||
| 81 | 3 | ]; |
|||
| 82 | |||||
| 83 | 3 | $message = $this->createMessage($user, $subject, MessageTypeType::BALANCE, $mailParams); |
|||
| 84 | |||||
| 85 | 3 | return $message; |
|||
| 86 | } |
||||
| 87 | |||||
| 88 | 3 | public function queueLeaveFamily(User $user): ?Message |
|||
| 89 | { |
||||
| 90 | 3 | $subject = 'Ménage quitté'; |
|||
| 91 | 3 | $mailParams = []; |
|||
| 92 | |||||
| 93 | 3 | $message = $this->createMessage($user, $subject, MessageTypeType::LEAVE_FAMILY, $mailParams); |
|||
| 94 | |||||
| 95 | 3 | return $message; |
|||
| 96 | } |
||||
| 97 | |||||
| 98 | 3 | public function queueAdminLeaveFamily(User $independentUser): ?Message |
|||
| 99 | { |
||||
| 100 | 3 | $subject = 'Ménage quitté'; |
|||
| 101 | 3 | $mailParams = ['independentUser' => $independentUser]; |
|||
| 102 | |||||
| 103 | 3 | $message = $this->createMessage(null, $subject, MessageTypeType::ADMIN_LEAVE_FAMILY, $mailParams, '[email protected]'); |
|||
| 104 | |||||
| 105 | 3 | return $message; |
|||
| 106 | } |
||||
| 107 | |||||
| 108 | 2 | public function queueRequestUserDeletion(User $requestingUser, User $userToDelete): ?Message |
|||
| 109 | { |
||||
| 110 | 2 | $subject = _tr('Demande de suppression de compte'); |
|||
| 111 | 2 | $mailParams = [ |
|||
| 112 | 2 | 'requestingUser' => $requestingUser, |
|||
| 113 | 2 | 'userToDelete' => $userToDelete, |
|||
| 114 | 2 | ]; |
|||
| 115 | |||||
| 116 | 2 | $message = $this->createMessage(null, $subject, MessageTypeType::REQUEST_USER_DELETION, $mailParams, '[email protected]'); |
|||
| 117 | |||||
| 118 | 2 | return $message; |
|||
| 119 | } |
||||
| 120 | |||||
| 121 | /** |
||||
| 122 | * Create a message by rendering the template. |
||||
| 123 | */ |
||||
| 124 | 21 | private function createMessage(?User $user, string $subject, string $type, array $mailParams, ?string $email = null): ?Message |
|||
| 125 | { |
||||
| 126 | 21 | $email = $this->getEmail($user, $email); |
|||
| 127 | 21 | if (!$email) { |
|||
| 128 | 2 | return null; |
|||
| 129 | } |
||||
| 130 | |||||
| 131 | 19 | $content = $this->messageRenderer->render($user, $email, $subject, $type, $mailParams); |
|||
| 132 | |||||
| 133 | 19 | $message = new Message(); |
|||
| 134 | 19 | $message->setType($type); |
|||
| 135 | 19 | $message->setRecipient($user); |
|||
| 136 | 19 | $message->setSubject($subject); |
|||
| 137 | 19 | $message->setBody($content); |
|||
| 138 | 19 | $message->setEmail($email); |
|||
| 139 | 19 | $this->entityManager->persist($message); |
|||
| 140 | |||||
| 141 | 19 | return $message; |
|||
| 142 | } |
||||
| 143 | |||||
| 144 | /** |
||||
| 145 | * @param User[] $users |
||||
| 146 | */ |
||||
| 147 | 2 | private function queueBalanceForEachUsers(array $users): int |
|||
| 148 | { |
||||
| 149 | 2 | foreach ($users as $user) { |
|||
| 150 | 1 | $bookables = $user->getRunningBookings()->map(fn (Booking $booking) => $booking->getBookable()); |
|||
| 151 | |||||
| 152 | 1 | $this->queueBalance($user, $bookables); |
|||
| 153 | } |
||||
| 154 | |||||
| 155 | 2 | return count($users); |
|||
| 156 | } |
||||
| 157 | |||||
| 158 | 1 | public function queueAllBalance(): int |
|||
| 159 | { |
||||
| 160 | 1 | $users = $this->userRepository->getAllToQueueBalanceMessage(); |
|||
| 161 | |||||
| 162 | 1 | return $this->queueBalanceForEachUsers($users); |
|||
| 163 | } |
||||
| 164 | |||||
| 165 | 1 | public function queueNegativeBalance(): int |
|||
| 166 | { |
||||
| 167 | /** @var UserRepository $userRepository */ |
||||
| 168 | 1 | $userRepository = $this->entityManager->getRepository(User::class); |
|||
| 169 | 1 | $users = $userRepository->getAllToQueueBalanceMessage(true); |
|||
| 170 | |||||
| 171 | 1 | return $this->queueBalanceForEachUsers($users); |
|||
| 172 | } |
||||
| 173 | |||||
| 174 | 21 | private function getEmail(?User $user, ?string $email): ?string |
|||
| 175 | { |
||||
| 176 | 21 | $this->toFamilyOwner = false; |
|||
| 177 | 21 | $email ??= $user?->getEmail(); |
|||
| 178 | |||||
| 179 | // Fallback to family owner if any |
||||
| 180 | 21 | if (!$email && $user?->getOwner()) { |
|||
| 181 | 3 | $email = $this->userRepository->getAclFilter()->runWithoutAcl(fn () => $user->getOwner()->getEmail()); |
|||
|
0 ignored issues
–
show
The method
getOwner() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||
| 182 | |||||
| 183 | 3 | $this->toFamilyOwner = true; |
|||
| 184 | } |
||||
| 185 | |||||
| 186 | 21 | return $email; |
|||
| 187 | } |
||||
| 188 | |||||
| 189 | /** |
||||
| 190 | * Whether the last sent email was sent to family owner instead of original recipient. |
||||
| 191 | */ |
||||
| 192 | 4 | public function wasToFamilyOwner(): bool |
|||
| 193 | { |
||||
| 194 | 4 | return $this->toFamilyOwner; |
|||
| 195 | } |
||||
| 196 | } |
||||
| 197 |