Completed
Branch develop (49b6a1)
by Benjamin
05:18
created

TeamController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 53
c 1
b 0
f 0
dl 0
loc 120
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createFromRule() 0 8 1
A generatePdf() 0 14 1
A create() 0 5 1
A index() 0 8 1
A detail() 0 5 1
A edit() 0 25 3
A delete() 0 19 3
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
     * @Route("/create", name="_create")
44
     */
45
    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

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