Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
70.00% |
21 / 30 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| Transactions | |
70.00% |
21 / 30 |
|
50.00% |
1 / 2 |
5.68 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
68.97% |
20 / 29 |
|
0.00% |
0 / 1 |
4.48 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Mail; |
| 6 | |
| 7 | use App\Controller\BaseController; |
| 8 | use App\Repository\StoreRepository; |
| 9 | use App\Repository\TransactionRepository; |
| 10 | use App\Service\EmailHelper; |
| 11 | use App\Service\PdfHelper; |
| 12 | use Exception; |
| 13 | use Knp\Snappy\Pdf; |
| 14 | use Symfony\Component\Clock\ClockInterface; |
| 15 | use Symfony\Component\HttpFoundation\RedirectResponse; |
| 16 | use Symfony\Component\HttpFoundation\Request; |
| 17 | use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
| 18 | use Symfony\Component\Mailer\MailerInterface; |
| 19 | use Symfony\Component\Mime\Part\DataPart; |
| 20 | use Symfony\Component\Routing\Attribute\Route; |
| 21 | |
| 22 | #[Route(path: '/mail/transactions', name: 'mail_transactions', methods: ['POST'])] |
| 23 | class Transactions extends BaseController |
| 24 | { |
| 25 | public function __construct( |
| 26 | private readonly TransactionRepository $transactionRepository, |
| 27 | private readonly StoreRepository $storeRepository, |
| 28 | private readonly Pdf $pdf, |
| 29 | private readonly PdfHelper $PDFHelper, |
| 30 | private readonly MailerInterface $mailer, |
| 31 | private readonly EmailHelper $emailHelper, |
| 32 | private readonly ClockInterface $clock, |
| 33 | ) {} |
| 34 | |
| 35 | public function __invoke( |
| 36 | Request $request, |
| 37 | ): RedirectResponse |
| 38 | { |
| 39 | $now = $this->clock->now(); |
| 40 | $year = $request->request->getInt('year', (int) $now->format('Y')); |
| 41 | $htmlPages = []; |
| 42 | $stores = $this->storeRepository->findAll(); |
| 43 | foreach ($stores as $store) { |
| 44 | if ($store->getUserId()) { |
| 45 | $htmlPages[] = $this->PDFHelper->renderTransactionHtml( |
| 46 | $this->transactionRepository, |
| 47 | $store, |
| 48 | $year |
| 49 | ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | $fileName = sprintf('movimientos-%d.pdf', $year); |
| 54 | try { |
| 55 | $attachment = new DataPart( |
| 56 | $this->pdf->getOutputFromHtml($htmlPages), |
| 57 | $fileName, |
| 58 | 'application/pdf' |
| 59 | ); |
| 60 | |
| 61 | $email = $this->emailHelper->createEmail( |
| 62 | to: $this->emailHelper->getFrom(), |
| 63 | subject: 'Movimientos de los locales ano '.$year |
| 64 | ) |
| 65 | ->text('Backup - Date: '.$now->format('Y-m-d')) |
| 66 | ->html('<h3>Backup</h3>Date: '.$now->format('Y-m-d')) |
| 67 | ->addPart($attachment); |
| 68 | |
| 69 | $this->mailer->send($email); |
| 70 | $this->addFlash('success', 'Mail has been sent successfully.'); |
| 71 | } catch (Exception|TransportExceptionInterface $exception) { |
| 72 | $this->addFlash('danger', $exception->getMessage()); |
| 73 | } |
| 74 | |
| 75 | return $this->redirectToRoute('welcome'); |
| 76 | } |
| 77 | |
| 78 | } |