Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
19.44% |
7 / 36 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TransactionsClients | |
19.44% |
7 / 36 |
|
33.33% |
1 / 3 |
24.82 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
19.35% |
6 / 31 |
|
0.00% |
0 / 1 |
4.10 | |||
| flashBatchResult | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Mail; |
| 6 | |
| 7 | use App\Controller\BaseController; |
| 8 | use App\Entity\Store; |
| 9 | use App\Repository\StoreRepository; |
| 10 | use App\Repository\TransactionRepository; |
| 11 | use App\Service\BulkMailService; |
| 12 | use App\Service\EmailHelper; |
| 13 | use App\Service\MailBatchResult; |
| 14 | use App\Service\PdfHelper; |
| 15 | use Symfony\Component\Clock\ClockInterface; |
| 16 | use Symfony\Component\HttpFoundation\Request; |
| 17 | use Symfony\Component\HttpFoundation\Response; |
| 18 | use Symfony\Component\Mime\Address; |
| 19 | use Symfony\Component\Mime\Email; |
| 20 | use Symfony\Component\Routing\Attribute\Route; |
| 21 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 22 | |
| 23 | #[Route(path: '/mail/transactions-clients', name: 'mail_transactions_clients', methods: ['GET', 'POST'])] |
| 24 | #[IsGranted('ROLE_ADMIN')] |
| 25 | class TransactionsClients extends BaseController |
| 26 | { |
| 27 | public function __construct( |
| 28 | private readonly StoreRepository $storeRepository, |
| 29 | private readonly TransactionRepository $transactionRepository, |
| 30 | private readonly PdfHelper $PDFHelper, |
| 31 | private readonly EmailHelper $emailHelper, |
| 32 | private readonly BulkMailService $bulkMailService, |
| 33 | private readonly ClockInterface $clock, |
| 34 | ) {} |
| 35 | |
| 36 | public function __invoke(Request $request): Response |
| 37 | { |
| 38 | $recipients = $request->request->all('recipients'); |
| 39 | |
| 40 | if ($recipients === []) { |
| 41 | return $this->render('mail/transactions-clients.html.twig', [ |
| 42 | 'stores' => $this->storeRepository->getActive(), |
| 43 | 'years' => range((int) $this->clock->now()->format('Y'), (int) $this->clock->now()->modify('-5 years')->format('Y')), |
| 44 | ]); |
| 45 | } |
| 46 | |
| 47 | $year = $request->request->getInt('year', (int) $this->clock->now()->format('Y')); |
| 48 | |
| 49 | $result = $this->bulkMailService->sendToFilteredStores( |
| 50 | $this->storeRepository->getActive(), |
| 51 | $recipients, |
| 52 | function (Store $store) use ($year): Email { |
| 53 | $fileName = sprintf('movimientos-%s-%d.pdf', $store->getId(), $year); |
| 54 | |
| 55 | $document = $this->PDFHelper->renderTransactionsPdf( |
| 56 | $this->PDFHelper->renderTransactionHtml($this->transactionRepository, $store, $year) |
| 57 | ); |
| 58 | |
| 59 | return $this->emailHelper->createTemplatedEmail( |
| 60 | to: new Address((string) $store->getUser()?->getEmail(), (string) $store->getUser()?->getName()), |
| 61 | subject: sprintf('Movimientos del local %s ano %d', $store->getId(), $year) |
| 62 | ) |
| 63 | ->htmlTemplate('email/client-transactions.twig') |
| 64 | ->context([ |
| 65 | 'user' => $store->getUser(), |
| 66 | 'store' => $store, |
| 67 | 'fileName' => $fileName, |
| 68 | 'year' => $year, |
| 69 | ]) |
| 70 | ->attach($document, $fileName); |
| 71 | } |
| 72 | ); |
| 73 | |
| 74 | $this->flashBatchResult($result); |
| 75 | |
| 76 | return $this->redirectToRoute('welcome'); |
| 77 | } |
| 78 | |
| 79 | private function flashBatchResult(MailBatchResult $result): void |
| 80 | { |
| 81 | if ($result->hasFailures()) { |
| 82 | $this->addFlash('warning', implode('<br>', $result->getFailures())); |
| 83 | } |
| 84 | |
| 85 | if ($result->hasSuccesses()) { |
| 86 | $this->addFlash('success', 'Mails have been sent to stores: '.implode(', ', $result->getSuccesses())); |
| 87 | } |
| 88 | } |
| 89 | } |