Test Failed
Branch master (354693)
by Valery
12:29
created

CurrencyController::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 12
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller\Admin;
6
7
use App\Entity\Currency;
8
use App\Form\Type\CurrencyType;
9
use App\Repository\CurrencyRepository;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\Form\ClickableInterface;
13
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\Routing\Annotation\Route;
17
18
final class CurrencyController extends AbstractController
19
{
20
    /**
21
     * @Route("/admin/currency", name="admin_currency")
22
     */
23
    public function index(CurrencyRepository $repository): Response
24
    {
25
        return $this->render('admin/currency/index.html.twig', [
26
            'currencies' => $repository->findAll(),
27
        ]);
28
    }
29
30
    /**
31
     * @Route("/admin/currency/new", name="admin_currency_new")
32
     */
33
    public function new(Request $request): Response
34
    {
35
        $currency = new Currency();
36
37
        $form = $this->createForm(CurrencyType::class, $currency)
38
            ->add('saveAndCreateNew', SubmitType::class);
39
        $form->handleRequest($request);
40
41
        if ($form->isSubmitted() && $form->isValid()) {
42
            $em = $this->getDoctrine()->getManager();
43
            $em->persist($currency);
44
            $em->flush();
45
46
            $this->addFlash('success', 'message.created');
47
48
            /** @var ClickableInterface $button */
49
            $button = $form->get('saveAndCreateNew');
50
            if ($button->isClicked()) {
51
                return $this->redirectToRoute('admin_currency_new');
52
            }
53
54
            return $this->redirectToRoute('admin_currency');
55
        }
56
57
        return $this->render('admin/currency/new.html.twig', [
58
            'currency' => $currency,
59
            'form' => $form->createView(),
60
        ]);
61
    }
62
63
    /**
64
     * Displays a form to edit an existing Currency entity.
65
     *
66
     * @Route("/admin/currency/{id<\d+>}/edit",methods={"GET", "POST"}, name="admin_currency_edit")
67
     */
68
    public function edit(Request $request, Currency $currency): Response
69
    {
70
        $form = $this->createForm(CurrencyType::class, $currency);
71
        $form->handleRequest($request);
72
        if ($form->isSubmitted() && $form->isValid()) {
73
            $this->getDoctrine()->getManager()->flush();
74
            $this->addFlash('success', 'message.updated');
75
76
            return $this->redirectToRoute('admin_currency');
77
        }
78
79
        return $this->render('admin/currency/edit.html.twig', [
80
            'form' => $form->createView(),
81
        ]);
82
    }
83
84
    /**
85
     * Deletes a Currency entity.
86
     *
87
     * @Route("/currency/{id<\d+>}/delete", methods={"POST"}, name="admin_currency_delete")
88
     * @IsGranted("ROLE_ADMIN")
89
     */
90
    public function delete(Request $request, Currency $currency): Response
91
    {
92
        if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {
93
            return $this->redirectToRoute('admin_currency');
94
        }
95
96
        $em = $this->getDoctrine()->getManager();
97
        $em->remove($currency);
98
        $em->flush();
99
        $this->addFlash('success', 'message.deleted');
100
101
        return $this->redirectToRoute('admin_currency');
102
    }
103
}
104