Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.86% covered (success)
92.86%
26 / 28
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PaymentsAccountant
92.86% covered (success)
92.86%
26 / 28
50.00% covered (danger)
50.00%
1 / 2
4.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
 __invoke
92.59% covered (success)
92.59%
25 / 27
0.00% covered (danger)
0.00%
0 / 1
3.00
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller\Mail;
6
7use App\Controller\BaseController;
8use App\Repository\TransactionRepository;
9use App\Service\EmailHelper;
10use Symfony\Component\Clock\ClockInterface;
11use Symfony\Component\DependencyInjection\Attribute\Autowire;
12use Symfony\Component\HttpFoundation\Request;
13use Symfony\Component\HttpFoundation\Response;
14use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
15use Symfony\Component\Mailer\MailerInterface;
16use Symfony\Component\Mime\Address;
17use Symfony\Component\Routing\Attribute\Route;
18use Symfony\Component\Security\Http\Attribute\IsGranted;
19
20#[Route(path: '/mail/payments-accountant', name: 'mail_payments_accountant', methods: ['GET', 'POST'])]
21#[IsGranted('ROLE_ADMIN')]
22class PaymentsAccountant extends BaseController
23{
24    public function __construct(
25        private readonly TransactionRepository $repository,
26        private readonly EmailHelper $emailHelper,
27        private readonly MailerInterface $mailer,
28        private readonly ClockInterface $clock,
29    ) {}
30
31    public function __invoke(
32        Request $request,
33        #[Autowire('%env(EMAIL_ACCOUNTANT)%')] string $emailAccountant,
34    ): Response
35    {
36        $now = $this->clock->now();
37        $year = $request->request->getInt('year', (int) $now->format('Y'));
38        $month = $request->request->getInt('month', (int) $now->format('m'));
39        $ids = array_map(intval(...), array_filter($request->request->all('ids'), is_numeric(...)));
40
41        if ($ids !== []) {
42            $email = $this->emailHelper->createTemplatedEmail(
43                to: Address::create($emailAccountant),
44                subject: sprintf('Pagos del MiniCC KuKu - %d / %d', $month, $year)
45            )
46                ->htmlTemplate('email/cobros-contador.twig')
47                ->context([
48                    'year' => $year,
49                    'month' => $month,
50                    'payments' => $this->repository->findByIds($ids),
51                    'fileName' => '@todo$fileName',//@todo ->attach($document, $fileName)
52                ]);
53
54            try {
55                $this->mailer->send($email);
56                $this->addFlash('success', 'Payments have been mailed.');
57            } catch (TransportExceptionInterface $exception) {
58                $this->addFlash('danger', 'Payments have NOT been mailed! - '.$exception->getMessage());
59            }
60
61            //@todo redirect elsewhere
62            //return $this->redirectToRoute('mail_cobros_contador');
63        }
64
65        return $this->render('mail/cobros-contador.html.twig',
66            [
67                'month' => $month,
68                'year' => $year,
69                'payments' => $this->repository->findByDate($year, $month),
70            ]
71        );
72    }
73}