|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Command; |
| 4 | + |
| 5 | +use Doctrine\ORM\EntityManagerInterface; |
| 6 | +use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
| 7 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 8 | +use Symfony\Component\Console\Command\Command; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 12 | +use Symfony\Component\Lock\LockFactory; |
| 13 | +use Symfony\Component\Lock\Store\FlockStore; |
| 14 | +use Symfony\Component\Mailer\MailerInterface; |
| 15 | +use Symfony\Component\Mime\Address; |
| 16 | + |
| 17 | +#[AsCommand( |
| 18 | + name: 'app:invite-donors-to-mspr', |
| 19 | + description: 'Send invite to donors on msp, to register for mspr as well' |
| 20 | +)] |
| 21 | +class InviteDonorsToMsprCommand extends Command |
| 22 | +{ |
| 23 | + private int $lastId = 0; |
| 24 | + |
| 25 | + public function __construct(private EntityManagerInterface $entityManager, private MailerInterface $mailer) |
| 26 | + { |
| 27 | + parent::__construct(); |
| 28 | + } |
| 29 | + |
| 30 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 31 | + { |
| 32 | + $store = new FlockStore(); |
| 33 | + $factory = new LockFactory($store); |
| 34 | + $lock = $factory->createLock($this->getName(), 0); |
| 35 | + if (!$lock->acquire()) { |
| 36 | + return Command::FAILURE; |
| 37 | + } |
| 38 | + |
| 39 | + $io = new SymfonyStyle($input, $output); |
| 40 | + $io->section('Command started at '.date('Y-m-d H:i:s')); |
| 41 | + |
| 42 | + while (true) { |
| 43 | + $donorEmails = $this->getDonorEmails(); |
| 44 | + if (empty($donorEmails)) { |
| 45 | + break; |
| 46 | + } |
| 47 | + |
| 48 | + foreach ($donorEmails as $donorEmail) { |
| 49 | + $output->writeln('Send email to '.$donorEmail); |
| 50 | + $this->sendEmail($donorEmail); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + $io->success('Command finished at '.date('Y-m-d H:i:s')); |
| 55 | + |
| 56 | + return Command::SUCCESS; |
| 57 | + } |
| 58 | + |
| 59 | + public function sendEmail(string $email): void |
| 60 | + { |
| 61 | + $message = (new TemplatedEmail()) |
| 62 | + ->to($email) |
| 63 | + ->from(new Address('donatori@mrezasolidarnosti.org', 'Mreža Solidarnosti')) |
| 64 | + ->subject('Vaša solidarna podrška je danas važnija nego ikad, jer se represija pojačava') |
| 65 | + ->htmlTemplate('email/invite-to-mspr.html.twig'); |
| 66 | + |
| 67 | + try { |
| 68 | + $this->mailer->send($message); |
| 69 | + } catch (\Exception $exception) { |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function getDonorEmails(): array |
| 74 | + { |
| 75 | + $stmt = $this->entityManager->getConnection()->executeQuery(' |
| 76 | + SELECT u.id, u.email |
| 77 | + FROM user_donor ud JOIN user u ON (ud.user_id = u.id) |
| 78 | + WHERE u.id > :lastId |
| 79 | + ORDER BY u.id ASC |
| 80 | + ', [ |
| 81 | + 'lastId' => $this->lastId, |
| 82 | + ]); |
| 83 | + |
| 84 | + $items = []; |
| 85 | + while ($row = $stmt->fetchAssociative()) { |
| 86 | + $this->lastId = $row['id']; |
| 87 | + $items[] = $row['email']; |
| 88 | + } |
| 89 | + |
| 90 | + return $items; |
| 91 | + } |
| 92 | +} |
0 commit comments