Passed
Push — master ( ece3c1...a0e7b2 )
by Florian
02:27
created

EventController::tryProcessForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of the TheAlternativeZurich/events project.
5
 *
6
 * (c) Florian Moser <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Controller;
13
14
use App\Controller\Base\BaseDoctrineController;
15
use App\Entity\Event;
16
use App\Entity\Registration;
17
use App\Form\Event\EditType;
18
use App\Security\Voter\EventVoter;
19
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\Annotation\Route;
23
use Symfony\Contracts\Translation\TranslatorInterface;
24
25
/**
26
 * @Route("/event")
27
 */
28
class EventController extends BaseDoctrineController
29
{
30
    /**
31
     * @Route("/create", name="event_create")
32
     *
33
     * @return Response
34
     */
35
    public function createAction(Request $request, TranslatorInterface $translator)
36
    {
37
        $event = new Event();
38
        $form = $this->createForm(EditType::class, $event)
39
            ->add('submit', SubmitType::class, ['translation_domain' => 'event', 'label' => 'create.submit']);
40
        $form->handleRequest($request);
41
        if ($form->isSubmitted() && $form->isValid()) {
42
            $eventRepository = $this->getDoctrine()->getRepository(Event::class);
43
            $eventRepository->save($event);
44
45
            $registration = Registration::createFromUser($event, $this->getUser(), true);
0 ignored issues
show
Bug introduced by
It seems like $this->getUser() can also be of type null; however, parameter $user of App\Entity\Registration::createFromUser() does only seem to accept App\Entity\User, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            $registration = Registration::createFromUser($event, /** @scrutinizer ignore-type */ $this->getUser(), true);
Loading history...
46
            $registrationRepository = $this->getDoctrine()->getRepository(Registration::class);
47
            $registrationRepository->save($registration);
48
49
            $message = $translator->trans('create.success.created', [], 'event');
50
            $this->displaySuccess($message);
51
52
            return $this->redirectToRoute('event_view', ['event' => $event->getId()]);
53
        }
54
55
        return $this->render('event/create.html.twig', ['form' => $form->createView()]);
56
    }
57
58
    /**
59
     * @Route("/{event}", name="event_view")
60
     *
61
     * @return Response
62
     */
63
    public function viewAction(Event $event)
64
    {
65
        $this->denyAccessUnlessGranted(EventVoter::EVENT_VIEW, $event);
66
67
        $registrations = $event->getRegistrations();
68
69
        /** @var Registration[] $participants */
70
        $participants = [];
71
        /** @var Registration[] $organizers */
72
        $organizers = [];
73
        foreach ($registrations as $registration) {
74
            $key = $registration->getCreatedAt()->format('c').'_'.$registration->getId();
75
76
            if ($registration->getIsOrganizer()) {
77
                $organizers[$key] = $registration;
78
            } else {
79
                $participants[$key] = $registration;
80
            }
81
        }
82
83
        return $this->render('event/view.html.twig', ['event' => $event, 'participatns' => $participants, 'organizers' => $organizers]);
84
    }
85
86
    /**
87
     * @Route("/{event}/update", name="event_update")
88
     *
89
     * @return Response
90
     */
91
    public function updateAction(Request $request, Event $event, TranslatorInterface $translator)
92
    {
93
        $this->denyAccessUnlessGranted(EventVoter::EVENT_UPDATE, $event);
94
95
        $form = $this->createForm(EditType::class, $event)
96
            ->add('submit', SubmitType::class, ['translation_domain' => 'event', 'label' => 'update.submit']);
97
        $form->handleRequest($request);
98
        if ($form->isSubmitted() && $form->isValid()) {
99
            $this->fastSave($event);
100
101
            $message = $translator->trans('update.success.updated', [], 'event');
102
            $this->displaySuccess($message);
103
104
            return $this->redirectToRoute('event_view', ['event' => $event->getId()]);
105
        }
106
107
        return $this->render('event/update.html.twig', ['form' => $form->createView()]);
108
    }
109
}
110