Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
11.11% |
1 / 9 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ClientsToExcel | |
11.11% |
1 / 9 |
|
50.00% |
1 / 2 |
9.32 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Download; |
| 6 | |
| 7 | use App\Controller\BaseController; |
| 8 | use App\Repository\UserRepository; |
| 9 | use App\Service\PhpXlsxGenerator; |
| 10 | use App\Service\TextFormatter; |
| 11 | use Symfony\Component\Clock\ClockInterface; |
| 12 | use Symfony\Component\HttpFoundation\RedirectResponse; |
| 13 | use Symfony\Component\Routing\Attribute\Route; |
| 14 | use 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'])] |
| 18 | class 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 | } |