Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
92.86% |
26 / 28 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| PaymentsAccountant | |
92.86% |
26 / 28 |
|
50.00% |
1 / 2 |
4.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
92.59% |
25 / 27 |
|
0.00% |
0 / 1 |
3.00 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Mail; |
| 6 | |
| 7 | use App\Controller\BaseController; |
| 8 | use App\Repository\TransactionRepository; |
| 9 | use App\Service\EmailHelper; |
| 10 | use Symfony\Component\Clock\ClockInterface; |
| 11 | use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 12 | use Symfony\Component\HttpFoundation\Request; |
| 13 | use Symfony\Component\HttpFoundation\Response; |
| 14 | use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
| 15 | use Symfony\Component\Mailer\MailerInterface; |
| 16 | use Symfony\Component\Mime\Address; |
| 17 | use Symfony\Component\Routing\Attribute\Route; |
| 18 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 19 | |
| 20 | #[Route(path: '/mail/payments-accountant', name: 'mail_payments_accountant', methods: ['GET', 'POST'])] |
| 21 | #[IsGranted('ROLE_ADMIN')] |
| 22 | class 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 | } |