PollController::createPoll()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 4
dl 0
loc 14
ccs 0
cts 10
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller\Project;
4
5
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
7
use Symfony\Component\Routing\Annotation\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
9
10
use Symfony\Component\HttpKernel\Exception\{
11
    AccessDeniedHttpException,
12
    BadRequestHttpException,
13
    NotFoundHttpException
14
};
15
16
use App\Manager\Project\{
17
    DetailsManager,
18
    PollManager,
19
    ProjectManager
20
};
21
22
class PollController extends Controller
23
{
24
    /**
25
     * @Route("/projects/{slug}/polls", name="create_project_poll", methods={"POST"})
26
     * @Security("has_role('ROLE_USER')")
27
     */
28
    public function createPoll(ProjectManager $projectManager, DetailsManager $detailsManager, PollManager $pollManager, string $slug)
29
    {
30
        if (($project = $projectManager->get($slug)) === null) {
31
            throw new NotFoundHttpException('projects.not_found');
32
        }
33
        if (!$this->getUser()->getProjects()->contains($project)) {
34
            throw new AccessDeniedHttpException('projects.access_denied');
35
        }
36
        if (($details = $detailsManager->getCurrentProjectDetails($project)) === null) {
37
            throw new BadRequestHttpException('projects.polls.not_ready');
38
        }
39
        $poll = $pollManager->createPoll($project, $details);
40
        return $this->redirectToRoute('get_poll', [
41
            'id' => $poll->getId()
42
        ]);
43
    }
44
    
45
    /**
46
     * @Route("/polls/{id}", name="get_poll", methods={"GET"})
47
     */
48
    public function getPoll(PollManager $pollManager, int $id)
49
    {
50
        return $this->render('projects/poll.html.twig', [
51
            'poll' => $pollManager->get($id)
52
        ]);
53
    }
54
}