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; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 24 | |||
| 25 | 11 | public function __construct(private readonly EntityManager $entityManager, private readonly MessageRenderer $messageRenderer) |
|
| 26 | { |
||
| 27 | 11 | $this->userRepository = $this->entityManager->getRepository(User::class); |
|
| 28 | } |
||
| 29 | |||
| 30 | 2 | public function queueRegister(User $user): ?Message |
|
| 31 | { |
||
| 32 | 2 | $subject = 'Demande de création de compte au Club Nautique Ichtus'; |
|
| 33 | 2 | $mailParams = [ |
|
| 34 | 2 | 'token' => $user->createToken(), |
|
| 35 | ]; |
||
| 36 | |||
| 37 | 2 | $message = $this->createMessage($user, $subject, MessageTypeType::REGISTER, $mailParams); |
|
| 38 | |||
| 39 | 2 | return $message; |
|
| 40 | } |
||
| 41 | |||
| 42 | 2 | public function queueUnregister(User $admin, User $unregisteredUser): ?Message |
|
| 43 | { |
||
| 44 | 2 | $subject = 'Démission'; |
|
| 45 | 2 | $mailParams = [ |
|
| 46 | 2 | 'unregisteredUser' => $unregisteredUser, |
|
| 47 | ]; |
||
| 48 | |||
| 49 | 2 | $message = $this->createMessage($admin, $subject, MessageTypeType::UNREGISTER, $mailParams); |
|
| 50 | |||
| 51 | 2 | return $message; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Queue a reset password email to specified user. |
||
| 56 | * |
||
| 57 | * @param User $user The user for which a password reset will be done |
||
| 58 | */ |
||
| 59 | 7 | public function queueResetPassword(User $user): ?Message |
|
| 60 | { |
||
| 61 | 7 | $subject = 'Demande de modification de mot de passe'; |
|
| 62 | 7 | $mailParams = [ |
|
| 63 | 7 | 'token' => $user->createToken(), |
|
| 64 | ]; |
||
| 65 | |||
| 66 | 7 | $message = $this->createMessage($user, $subject, MessageTypeType::RESET_PASSWORD, $mailParams); |
|
| 67 | |||
| 68 | 7 | return $message; |
|
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param Bookable[] $bookables |
||
| 73 | */ |
||
| 74 | 3 | public function queueBalance(User $user, iterable $bookables): ?Message |
|
| 75 | { |
||
| 76 | 3 | $subject = 'Balance de compte'; |
|
| 77 | 3 | $mailParams = [ |
|
| 78 | 3 | 'bookables' => $bookables, |
|
| 79 | ]; |
||
| 80 | |||
| 81 | 3 | $message = $this->createMessage($user, $subject, MessageTypeType::BALANCE, $mailParams); |
|
| 82 | |||
| 83 | 3 | return $message; |
|
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Create a message by rendering the template. |
||
| 88 | */ |
||
| 89 | 14 | private function createMessage(?User $user, string $subject, string $type, array $mailParams): ?Message |
|
| 90 | { |
||
| 91 | 14 | $email = $this->getEmail($user); |
|
| 92 | 14 | if (!$email) { |
|
| 93 | 2 | return null; |
|
| 94 | } |
||
| 95 | |||
| 96 | 12 | $content = $this->messageRenderer->render($user, $email, $subject, $type, $mailParams); |
|
| 97 | |||
| 98 | 12 | $message = new Message(); |
|
| 99 | 12 | $message->setType($type); |
|
| 100 | 12 | $message->setRecipient($user); |
|
| 101 | 12 | $message->setSubject($subject); |
|
| 102 | 12 | $message->setBody($content); |
|
| 103 | 12 | $message->setEmail($email); |
|
| 104 | 12 | $this->entityManager->persist($message); |
|
| 105 | |||
| 106 | 12 | return $message; |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param User[] $users |
||
| 111 | */ |
||
| 112 | 2 | private function queueBalanceForEachUsers(array $users): int |
|
| 113 | { |
||
| 114 | 2 | foreach ($users as $user) { |
|
| 115 | 1 | $bookables = $user->getRunningBookings()->map(fn (Booking $booking) => $booking->getBookable()); |
|
| 116 | |||
| 117 | 1 | $this->queueBalance($user, $bookables); |
|
| 118 | } |
||
| 119 | |||
| 120 | 2 | return count($users); |
|
| 121 | } |
||
| 122 | |||
| 123 | 1 | public function queueAllBalance(): int |
|
| 124 | { |
||
| 125 | 1 | $users = $this->userRepository->getAllToQueueBalanceMessage(); |
|
| 126 | |||
| 127 | 1 | return $this->queueBalanceForEachUsers($users); |
|
| 128 | } |
||
| 129 | |||
| 130 | 1 | public function queueNegativeBalance(): int |
|
| 131 | { |
||
| 132 | /** @var UserRepository $userRepository */ |
||
| 133 | 1 | $userRepository = $this->entityManager->getRepository(User::class); |
|
| 134 | 1 | $users = $userRepository->getAllToQueueBalanceMessage(true); |
|
| 135 | |||
| 136 | 1 | return $this->queueBalanceForEachUsers($users); |
|
| 137 | } |
||
| 138 | |||
| 139 | 14 | private function getEmail(?User $user): ?string |
|
| 140 | { |
||
| 141 | 14 | $this->toFamilyOwner = false; |
|
| 142 | 14 | if (!$user) { |
|
| 143 | return null; |
||
| 144 | } |
||
| 145 | |||
| 146 | 14 | $email = $user->getEmail(); |
|
| 147 | |||
| 148 | // Fallback to family owner if any |
||
| 149 | 14 | if (!$email && $user->getOwner()) { |
|
| 150 | 3 | $email = $this->userRepository->getAclFilter()->runWithoutAcl(fn () => $user->getOwner()->getEmail()); |
|
| 151 | |||
| 152 | 3 | $this->toFamilyOwner = true; |
|
| 153 | } |
||
| 154 | |||
| 155 | 14 | return $email; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Whether the last sent email was sent to family owner instead of original recipient. |
||
| 160 | */ |
||
| 161 | 3 | public function wasToFamilyOwner(): bool |
|
| 162 | { |
||
| 163 | 3 | return $this->toFamilyOwner; |
|
| 164 | } |
||
| 165 | } |
||
| 166 |