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 | 13 | public function __construct( |
|||
| 26 | private readonly EntityManager $entityManager, |
||||
| 27 | private readonly MessageRenderer $messageRenderer, |
||||
| 28 | ) { |
||||
| 29 | 13 | $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 | /** |
||||
| 109 | * Create a message by rendering the template. |
||||
| 110 | */ |
||||
| 111 | 19 | private function createMessage(?User $user, string $subject, string $type, array $mailParams, ?string $email = null): ?Message |
|||
| 112 | { |
||||
| 113 | 19 | $email = $this->getEmail($user, $email); |
|||
| 114 | 19 | if (!$email) { |
|||
| 115 | 2 | return null; |
|||
| 116 | } |
||||
| 117 | |||||
| 118 | 17 | $content = $this->messageRenderer->render($user, $email, $subject, $type, $mailParams); |
|||
| 119 | |||||
| 120 | 17 | $message = new Message(); |
|||
| 121 | 17 | $message->setType($type); |
|||
| 122 | 17 | $message->setRecipient($user); |
|||
| 123 | 17 | $message->setSubject($subject); |
|||
| 124 | 17 | $message->setBody($content); |
|||
| 125 | 17 | $message->setEmail($email); |
|||
| 126 | 17 | $this->entityManager->persist($message); |
|||
| 127 | |||||
| 128 | 17 | return $message; |
|||
| 129 | } |
||||
| 130 | |||||
| 131 | /** |
||||
| 132 | * @param User[] $users |
||||
| 133 | */ |
||||
| 134 | 2 | private function queueBalanceForEachUsers(array $users): int |
|||
| 135 | { |
||||
| 136 | 2 | foreach ($users as $user) { |
|||
| 137 | 1 | $bookables = $user->getRunningBookings()->map(fn (Booking $booking) => $booking->getBookable()); |
|||
| 138 | |||||
| 139 | 1 | $this->queueBalance($user, $bookables); |
|||
| 140 | } |
||||
| 141 | |||||
| 142 | 2 | return count($users); |
|||
| 143 | } |
||||
| 144 | |||||
| 145 | 1 | public function queueAllBalance(): int |
|||
| 146 | { |
||||
| 147 | 1 | $users = $this->userRepository->getAllToQueueBalanceMessage(); |
|||
| 148 | |||||
| 149 | 1 | return $this->queueBalanceForEachUsers($users); |
|||
| 150 | } |
||||
| 151 | |||||
| 152 | 1 | public function queueNegativeBalance(): int |
|||
| 153 | { |
||||
| 154 | /** @var UserRepository $userRepository */ |
||||
| 155 | 1 | $userRepository = $this->entityManager->getRepository(User::class); |
|||
| 156 | 1 | $users = $userRepository->getAllToQueueBalanceMessage(true); |
|||
| 157 | |||||
| 158 | 1 | return $this->queueBalanceForEachUsers($users); |
|||
| 159 | } |
||||
| 160 | |||||
| 161 | 19 | private function getEmail(?User $user, ?string $email): ?string |
|||
| 162 | { |
||||
| 163 | 19 | $this->toFamilyOwner = false; |
|||
| 164 | 19 | $email ??= $user?->getEmail(); |
|||
| 165 | |||||
| 166 | // Fallback to family owner if any |
||||
| 167 | 19 | if (!$email && $user?->getOwner()) { |
|||
| 168 | 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...
|
|||||
| 169 | |||||
| 170 | 3 | $this->toFamilyOwner = true; |
|||
| 171 | } |
||||
| 172 | |||||
| 173 | 19 | return $email; |
|||
| 174 | } |
||||
| 175 | |||||
| 176 | /** |
||||
| 177 | * Whether the last sent email was sent to family owner instead of original recipient. |
||||
| 178 | */ |
||||
| 179 | 4 | public function wasToFamilyOwner(): bool |
|||
| 180 | { |
||||
| 181 | 4 | return $this->toFamilyOwner; |
|||
| 182 | } |
||||
| 183 | } |
||||
| 184 |