Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.24% covered (warning)
88.24%
15 / 17
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Planillas
88.24% covered (warning)
88.24%
15 / 17
50.00% covered (danger)
50.00%
1 / 2
3.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 planillas
87.50% covered (warning)
87.50%
14 / 16
0.00% covered (danger)
0.00%
0 / 1
2.01
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller\Mail;
6
7use App\Controller\BaseController;
8use App\Service\EmailHelper;
9use App\Service\PayrollHelper;
10use App\Service\PdfHelper;
11use Symfony\Component\Clock\ClockInterface;
12use Symfony\Component\HttpFoundation\Response;
13use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
14use Symfony\Component\Mailer\MailerInterface;
15use Symfony\Component\Routing\Attribute\Route;
16use Symfony\Component\Security\Http\Attribute\IsGranted;
17
18#[IsGranted('ROLE_ADMIN')]
19class Planillas extends BaseController
20{
21    public function __construct(
22        private readonly PdfHelper $PDFHelper,
23        private readonly MailerInterface $mailer,
24        private readonly EmailHelper $emailHelper,
25        private readonly PayrollHelper $payrollHelper,
26        private readonly ClockInterface $clock,
27    ) {}
28
29    #[Route(path: '/mail/planillas', name: 'mail_planillas', methods: ['GET'])]
30    public function planillas(): Response
31    {
32        $now = $this->clock->now();
33        $year = (int) $now->format('Y');
34        $month = (int) $now->format('m');
35        $fileName = sprintf('payrolls-%d-%s.pdf', $year, $month);
36        $document = $this->PDFHelper->renderPayrollPdf($year, $month, $this->payrollHelper);
37
38        $email = $this->emailHelper->createEmail(
39            to: $this->emailHelper->getFrom(),
40            subject: sprintf('Planillas %d-%s', $year, $month)
41        )
42            ->html('Attachment: '.$fileName)
43            ->attach($document, $fileName);
44
45        try {
46            $this->mailer->send($email);
47            $this->addFlash('success', 'Mail has been sent.');
48        } catch (TransportExceptionInterface $transportException) {
49            $this->addFlash('danger', 'ERROR sending mail: '.$transportException->getMessage());
50        }
51
52        return $this->render('admin/tasks.html.twig');
53    }
54}