Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
Create
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
5.00
0.00% covered (danger)
0.00%
0 / 1
 __invoke
94.74% covered (success)
94.74%
18 / 19
0.00% covered (danger)
0.00%
0 / 1
5.00
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller\PaymentMethods;
6
7use App\Controller\BaseController;
8use App\Entity\PaymentMethod;
9use App\Form\PaymentMethodType;
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: '/payment-methods/create', name: 'payment_methods_create', methods: ['GET', 'POST'])]
18class Create extends BaseController
19{
20    public function __invoke(
21        Request $request,
22        EntityManagerInterface $entityManager,
23    ): Response
24    {
25        $paymentMethod = new PaymentMethod();
26        $form = $this->createForm(PaymentMethodType::class, $paymentMethod);
27        $form->handleRequest($request);
28        if ($form->isSubmitted() && $form->isValid()) {
29            $entityManager->persist($paymentMethod);
30            $entityManager->flush();
31
32            $this->addFlash('success', 'Payment method has been saved');
33
34            return $this->redirectToRoute('payment_methods_index');
35        }
36
37        $template = $request->query->get('ajax')
38            ? '_form.html.twig'
39            : 'form.html.twig';
40
41        return $this->render(
42            'payment-methods/'.$template,
43            [
44                'form' => $form,
45                'data' => $paymentMethod,
46            ],
47            new Response(null, $form->isSubmitted() ? 422 : 200)
48        );
49    }
50}