Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Edit
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 __invoke
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller\Transactions;
6
7use App\Controller\BaseController;
8use App\Entity\Transaction;
9use App\Form\TransactionTypeType;
10use Doctrine\ORM\EntityManagerInterface;
11use Symfony\Component\HttpFoundation\Request;
12use Symfony\Component\HttpFoundation\Response;
13use Symfony\Component\Routing\Attribute\Route;
14use Symfony\Component\Security\Http\Attribute\IsGranted;
15
16#[IsGranted('ROLE_ADMIN')]
17#[Route(path: '/transactions/edit/{id}', name: 'transactions_edit', methods: ['GET', 'POST'])]
18class Edit extends BaseController
19{
20    public function __invoke(
21        Transaction $transaction,
22        Request $request,
23        EntityManagerInterface $entityManager,
24    ): Response
25    {
26        $view = $request->query->get('view');
27        $form = $this->createForm(TransactionTypeType::class, $transaction);
28        $form->handleRequest($request);
29        if ($form->isSubmitted() && $form->isValid()) {
30            $entityManager->persist($transaction);
31            $entityManager->flush();
32
33            $this->addFlash('success', 'La Transaccion ha sido guardada.');
34
35            if ($view) {
36                return $this->redirect((string)$view);
37            }
38
39            return $this->redirectToRoute(
40                'stores_transactions',
41                [
42                    'id' => $transaction->getStore()->getId(),
43                ]
44            );
45        }
46
47        return $this->render(
48            'transaction/form.html.twig',
49            [
50                'form' => $form,
51                'data' => $transaction,
52                'redirect' => $view,
53            ]
54        );
55    }
56}