OrganisationController::ensureAccessGranted()   A
last analyzed

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 1
1
<?php
2
3
/*
4
 * This file is part of the vseth-semesterly-reports 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\Administration\Base\BaseController;
15
use App\Entity\Organisation;
16
use App\Entity\SemesterReport;
17
use App\Form\Type\SemesterType;
18
use App\Security\Voter\Base\BaseVoter;
19
use App\Service\Interfaces\EmailServiceInterface;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\Annotation\Route;
23
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
24
use Symfony\Contracts\Translation\TranslatorInterface;
25
26
/**
27
 * @Route("/organisation")
28
 */
29
class OrganisationController extends BaseController
30
{
31
    /**
32
     * @Route("/{organisation}", name="organisation_view")
33
     *
34
     * @return Response
35
     */
36
    public function viewAction(Request $request, Organisation $organisation, EmailServiceInterface $emailService, TranslatorInterface $translator)
37
    {
38
        $this->ensureAccessGranted($organisation);
39
40
        // remember last visit
41
        if (\in_array('ROLE_ORGANISATION', $this->getUser()->getRoles(), true)) {
42
            $organisation->setVisitOccurred();
43
            $this->fastSave($organisation);
44
        }
45
46
        $output = [];
47
48
        $hasSemesterReport = $this->getDoctrine()->getRepository(SemesterReport::class)->findOneBy(['organisation' => $organisation->getId(), 'semester' => SemesterType::getCurrentSemester()]);
49
        if (!$hasSemesterReport) {
50
            //allow semester creation
51
            $semester = new SemesterReport();
52
            $semester->setSubmittedDateTime(new \DateTime());
53
            $semester->setOrganisation($organisation);
54
            $semester->setSemester(SemesterType::getCurrentSemester());
55
            $hasSaved = false;
56
            $form = $this->handleCreateForm($request, $semester, function () use (&$hasSaved) {
57
                $hasSaved = true;
58
59
                return true;
60
            });
61
62
            if (!$hasSaved) {
63
                $output['submit_semester_report'] = $form->createView();
64
            } else {
65
                $subject = $translator->trans('report_submitted_email.subject', ['%organisation%' => $organisation->getName()], 'organisation');
66
                $body = $translator->trans(
67
                    'report_submitted_email.body',
68
                    [
69
                        '%event_count%' => $organisation->getEvents()->count(),
70
                        '%report_comment%' => $semester->getComments(),
71
                        '%report_political_events_description%' => $semester->getPoliticalEventsDescription(),
72
                        '%organisation_email%' => $organisation->getEmail(),
73
                        '%organisation_comment%' => $organisation->getComments(),
74
                        '%link%' => $this->generateUrl('administration', [], UrlGeneratorInterface::ABSOLUTE_URL),
75
                    ],
76
                    'organisation'
77
                );
78
79
                $emailService->sendEmailToAdministrator($subject, $body);
80
            }
81
        }
82
83
        $output['organisation'] = $organisation;
84
85
        return $this->render('organisation/view.html.twig', $output);
86
    }
87
88
    private function ensureAccessGranted(Organisation $organisation)
89
    {
90
        $this->denyAccessUnlessGranted(BaseVoter::VIEW, $organisation);
91
    }
92
}
93