TeamController::detail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Obblm\Core\Controller;
4
5
use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
6
use Knp\Snappy\Pdf;
7
use Obblm\Core\Entity\Rule;
8
use Obblm\Core\Entity\Team;
9
use Obblm\Core\Event\TeamVersionEvent;
10
use Obblm\Core\Form\Team\EditTeamType;
11
use Obblm\Core\Helper\TeamHelper;
12
use Obblm\Core\Security\Roles;
13
use Obblm\Core\Security\Voter\TeamVoter;
14
use Obblm\Core\Service\FileTeamUploader;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
19
use Symfony\Component\Routing\Annotation\Route;
20
21
/**
22
 * Class TeamController
23
 * @package Obblm\Core\Controller
24
 *
25
 * @Route("/teams", name="obblm_team")
26
 */
27
class TeamController extends AbstractTeamController
28
{
29
    /**
30
     * @Route("/", name="_mine")
31
     */
32
    public function index(): Response
33
    {
34
        $this->denyAccessUnlessGranted(Roles::COACH);
35
36
        $teams = $this->getUser()->getTeams();
37
38
        return $this->render('@ObblmCore/team/index.html.twig', [
39
            'teams' => $teams
40
        ]);
41
    }
42
43
    /**
44
     * @Route("/create", name="_create")
45
     */
46
    public function create(Request $request): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

46
    public function create(/** @scrutinizer ignore-unused */ Request $request): Response

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
47
    {
48
        $this->denyAccessUnlessGranted(Roles::COACH);
49
50
        return $this->render('@ObblmCore/form/team/rules-selector.html.twig', [
51
        ]);
52
    }
53
54
    /**
55
     * @Route("/create/from-rule/{rule}", name="_create_rule")
56
     */
57
    public function createFromRule(Rule $rule, Request $request): Response
58
    {
59
        $this->denyAccessUnlessGranted(Roles::COACH);
60
61
        $team = (new Team())
62
            ->setRule($rule)
63
            ->setCoach($this->getUser());
64
        return $this->createAndComputeTeamForm($team, $request);
65
    }
66
67
    /**
68
     * @Route("/{team}", name="_detail")
69
     */
70
    public function detail(Team $team): Response
71
    {
72
        $this->denyAccessUnlessGranted(TeamVoter::VIEW, $team);
73
        return $this->render('@ObblmCore/team/detail.html.twig', [
74
            'version' => TeamHelper::getLastVersion($team),
75
        ]);
76
    }
77
78
    /**
79
     * @Route("/{team}/pdf", name="_pdf")
80
     */
81
    public function generatePdf(Team $team, Pdf $pdf): Response
82
    {
83
        $this->denyAccessUnlessGranted(TeamVoter::VIEW, $team);
84
        // Retrieve the HTML generated in our twig file
85
        $html = $this->renderView('@ObblmCore/team/detail-pdf.html.twig', [
86
            'version' => TeamHelper::getLastVersion($team),
87
        ]);
88
        $pdf->setOption('orientation', 'landscape');
89
        $pdf->setOption('disable-javascript', true);
90
        $pdf->setOption('title', $team->getName());
91
92
        $fileName =  urlencode($team->getName()) . '.pdf';
93
94
        return new PdfResponse($pdf->getOutputFromHtml($html), $fileName, null, ResponseHeaderBag::DISPOSITION_INLINE);
95
    }
96
97
    /**
98
     * @Route("/{team}/edit", name="_edit")
99
     */
100
    public function edit(Team $team, Request $request, EventDispatcherInterface $dispatcher): Response
101
    {
102
        $this->denyAccessUnlessGranted(TeamVoter::EDIT, $team);
103
104
        $form = $this->createForm(EditTeamType::class, TeamHelper::getLastVersion($team));
105
106
        $form->handleRequest($request);
107
108
        if ($form->isSubmitted() && $form->isValid()) {
109
            $version = $form->getData();
110
            $em = $this->getDoctrine()->getManager();
111
            $dispatcher->dispatch(new TeamVersionEvent($version), TeamVersionEvent::PRE_SAVE);
112
            $dispatcher->dispatch(new TeamVersionEvent($version), TeamVersionEvent::TREASURE_BASE);
113
            $em->persist($version);
114
            $em->flush();
115
            $this->addFlash(
116
                'success',
117
                'obblm.flash.team.saved'
118
            );
119
            return $this->redirectToRoute('obblm_team_detail', ['team' => $team->getId()]);
120
        }
121
122
        return $this->render('@ObblmCore/form/team/edit.html.twig', [
123
            'form' => $form->createView(),
124
            'team' => $team
125
        ]);
126
    }
127
128
    /**
129
     * @Route("/{team}/delete", name="_delete")
130
     */
131
    public function delete(Team $team, Request $request, FileTeamUploader $uploader): Response
132
    {
133
        $this->denyAccessUnlessGranted(TeamVoter::DELETE, $team);
134
135
        $confirm = $request->get('confirm');
136
        if ($confirm !== null) {
137
            if ($confirm == 1) {
138
                $em = $this->getDoctrine()->getManager();
139
                $uploader->setObjectSubDirectory($team->getId());
140
                $uploader->removeOldFile();
141
                $em->remove($team);
142
                $em->flush();
143
                $this->addFlash(
144
                    'success',
145
                    'obblm.flash.team.deleted'
146
                );
147
                return $this->redirectToRoute('obblm_team_mine');
148
            }
149
            return $this->redirectToRoute('obblm_team_detail', ['team' => $team->getId()]);
150
        }
151
152
        return $this->render('@ObblmCore/form/team/delete.html.twig', [
153
            'team' => $team
154
        ]);
155
    }
156
}
157