Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
11.11% covered (danger)
11.11%
1 / 9
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClientsToExcel
11.11% covered (danger)
11.11%
1 / 9
50.00% covered (danger)
50.00%
1 / 2
9.32
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
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller\Download;
6
7use App\Controller\BaseController;
8use App\Repository\UserRepository;
9use App\Service\PhpXlsxGenerator;
10use App\Service\TextFormatter;
11use Symfony\Component\Clock\ClockInterface;
12use Symfony\Component\HttpFoundation\RedirectResponse;
13use Symfony\Component\Routing\Attribute\Route;
14use Symfony\Component\Security\Http\Attribute\IsGranted;
15
16#[IsGranted('ROLE_ADMIN')]
17#[Route(path: '/download/clients-to-excel', name: 'download_clients_to_excel', methods: ['GET'])]
18class ClientsToExcel extends BaseController
19{
20    public function __construct(
21        private readonly UserRepository $userRepository,
22        private readonly TextFormatter $textFormatter,
23        private readonly ClockInterface $clock,
24    ) {}
25
26    public function __invoke(): RedirectResponse
27    {
28        $users = $this->userRepository->getSortedByStore();
29        $rows = [];
30        $rows[] = ['Nombre', 'Email', 'RUC', 'Direccion', 'Telefono'];
31        foreach ($users as $user) {
32            $rows[] = [$user->getName(), $user->getEmail(), $this->textFormatter->formatRUC($user), $user->getDireccion(), $user->getTelefono()];
33        }
34
35        $xlsx = PhpXlsxGenerator::fromArray($rows);
36        $xlsx->downloadAs('clientes-'.$this->clock->now()->format('Y-m-d').'.xlsx');
37        return $this->redirectToRoute('admin_tasks');
38    }
39}