Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
90.91% |
30 / 33 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CollectRent | |
90.91% |
30 / 33 |
|
50.00% |
1 / 2 |
8.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
90.62% |
29 / 32 |
|
0.00% |
0 / 1 |
7.04 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Controller\Admin; |
| 6 | |
| 7 | use App\Controller\BaseController; |
| 8 | use App\Enum\PaymentMethodId; |
| 9 | use App\Repository\PaymentMethodRepository; |
| 10 | use App\Repository\StoreRepository; |
| 11 | use App\Repository\UserRepository; |
| 12 | use App\Service\TransactionFactory; |
| 13 | use Doctrine\ORM\EntityManagerInterface; |
| 14 | use Symfony\Component\HttpFoundation\Request; |
| 15 | use Symfony\Component\HttpFoundation\Response; |
| 16 | use Symfony\Component\Routing\Attribute\Route; |
| 17 | use Symfony\Component\Security\Http\Attribute\IsGranted; |
| 18 | use UnexpectedValueException; |
| 19 | |
| 20 | #[Route(path: '/admin/collect-rent', name: 'admin_collect_rent', methods: ['GET', 'POST'])] |
| 21 | #[IsGranted('ROLE_ADMIN')] |
| 22 | class CollectRent extends BaseController |
| 23 | { |
| 24 | public function __construct( |
| 25 | private readonly StoreRepository $storeRepository, |
| 26 | private readonly UserRepository $userRepository, |
| 27 | private readonly PaymentMethodRepository $paymentMethodRepository, |
| 28 | private readonly TransactionFactory $transactionFactory, |
| 29 | ) {} |
| 30 | |
| 31 | public function __invoke( |
| 32 | Request $request, |
| 33 | EntityManagerInterface $entityManager, |
| 34 | ): Response |
| 35 | { |
| 36 | /** @var array<string, string> $values */ |
| 37 | $values = $request->request->all('values'); |
| 38 | if ($values === []) { |
| 39 | return $this->render( |
| 40 | 'admin/collect-rent.html.twig', |
| 41 | [ |
| 42 | 'stores' => $this->storeRepository->getActive(), |
| 43 | ] |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | /** @var array<string, string> $users */ |
| 48 | $users = $request->request->all('users'); |
| 49 | $method = $this->paymentMethodRepository->find(PaymentMethodId::BAR->value); |
| 50 | |
| 51 | if (!$method) { |
| 52 | throw new UnexpectedValueException('Invalid payment method.'); |
| 53 | } |
| 54 | |
| 55 | foreach ($values as $storeId => $value) { |
| 56 | if (!$value) { |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | $user = $this->userRepository->find((int)$users[$storeId]); |
| 61 | |
| 62 | if (!$user) { |
| 63 | throw new UnexpectedValueException('Store has no user.'); |
| 64 | } |
| 65 | |
| 66 | $store = $this->storeRepository->find((int)$storeId); |
| 67 | |
| 68 | if (!$store) { |
| 69 | throw new UnexpectedValueException('Store does not exist.'); |
| 70 | } |
| 71 | |
| 72 | $transaction = $this->transactionFactory->createRent( |
| 73 | $store, |
| 74 | $user, |
| 75 | $method, |
| 76 | (string) $request->request->get('date_cobro'), |
| 77 | $value, |
| 78 | ); |
| 79 | |
| 80 | $entityManager->persist($transaction); |
| 81 | } |
| 82 | |
| 83 | $entityManager->flush(); |
| 84 | |
| 85 | $this->addFlash('success', 'A cobrar...'); |
| 86 | |
| 87 | return $this->redirectToRoute('welcome'); |
| 88 | } |
| 89 | } |