ParticipationController::showAction()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/*
3
 * Copyright 2014-2016 Arnaud Bienvenu
4
 *
5
 * This file is part of Kyela.
6
7
 * Kyela is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
12
 * Kyela is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with Kyela.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace Abienvenu\KyelaBundle\Controller;
23
24
use Abienvenu\KyelaBundle\Entity\Choice;
25
use Abienvenu\KyelaBundle\Entity\Event;
26
use Abienvenu\KyelaBundle\Entity\Participant;
27
use Abienvenu\KyelaBundle\Entity\Poll;
28
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
29
use Symfony\Component\Routing\Annotation\Route;
30
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
31
use Abienvenu\KyelaBundle\Entity\Participation;
32
33
/**
34
 * Participation controller.
35
 *
36
 * @Route("/participation")
37
 */
38
class ParticipationController extends Controller
39
{
40
    /**
41
     * Displays interactive participation table
42
     *
43
     * @Route("", methods="GET")
44
     * @Template()
45
     */
46
    public function showAction(Poll $poll, $isFuture)
47
    {
48
        $em = $this->getDoctrine()->getManager();
49
        $events = $em->getRepository('KyelaBundle:Event')->getFutureOrPastEvents($poll, $isFuture);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getFutureOrPastEvents() does only exist in the following implementations of said interface: Abienvenu\KyelaBundle\Entity\EventRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
50
        $choices = $em->getRepository("KyelaBundle:Choice")->getOrderedChoices($poll);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getOrderedChoices() does only exist in the following implementations of said interface: Abienvenu\KyelaBundle\Entity\ChoiceRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
51
        $participationsArray = [];
52
        foreach ($events as $event)
53
        {
54
            foreach ($event->getParticipations() as $participation)
55
            {
56
                $accessKey = "{$event->getId()}-{$participation->getParticipant()->getId()}";
57
                $participationsArray[$accessKey] = $participation;
58
            }
59
        }
60
        return [
61
            'poll' => $poll,
62
            'choices' => $choices,
63
            'events' => $events,
64
            'participations' => $participationsArray,
65
        ];
66
    }
67
68
    /**
69
     * Creates a new Participation on the fly.
70
     *
71
     * @Route("/new/{event}/{participant}/{choice}", name="participation_new", methods="GET")
72
     */
73
    public function newAction(Event $event, Participant $participant, Choice $choice)
74
    {
75
        $em = $this->getDoctrine()->getManager();
76
77
        // If participation exists, editAction should have been called, not newAction
78
        // But just in case, we look for an existing participation
79
        $participation = $em->getRepository('KyelaBundle:Participation')->findOneBy(['participant' => $participant->getId(), 'event' => $event->getId()]);
80
        if (!$participation)
81
        {
82
            $participation = new Participation();
83
        }
84
        $participation->setEvent($event);
85
        $participation->setParticipant($participant);
86
        $participation->setChoice($choice);
87
88
        $em->persist($participation);
89
        $em->flush();
90
        return $this->render(
91
            'KyelaBundle:participation:_cell.html.twig',
92
            ['participation' => $participation, 'choices' => $event->getPoll()->getChoices(), 'event' => $event, 'participant' => $participant]);
93
    }
94
95
    /**
96
     * Edits a Participation on the fly
97
     *
98
     * @Route("/edit/{participation}/{newChoice}", name="participation_edit", methods="GET")
99
     */
100 View Code Duplication
    public function editAction(Participation $participation, Choice $newChoice)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $em = $this->getDoctrine()->getManager();
103
104
        $participation->setChoice($newChoice);
105
        $em->flush();
106
        return $this->render(
107
            'KyelaBundle:participation:_cell.html.twig',
108
            [
109
                'participation' => $participation,
110
                'choices' => $newChoice->getPoll()->getChoices(),
111
                'event' => $participation->getEvent(),
112
                'participant' => $participation->getParticipant()
113
            ]
114
        );
115
    }
116
117
    /**
118
     * Removes a Participation on the fly
119
     *
120
     * @Route("/delete/{participation}", name="participation_delete", methods="GET")
121
     */
122 View Code Duplication
    public function deleteAction(Participation $participation)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124
        $em = $this->getDoctrine()->getManager();
125
        $em->remove($participation);
126
        $em->flush();
127
        return $this->render(
128
            'KyelaBundle:participation:_cell.html.twig',
129
            [
130
                'participation' => null,
131
                'choices' => $participation->getEvent()->getPoll()->getChoices(),
132
                'event' => $participation->getEvent(),
133
                'participant' => $participation->getParticipant()
134
            ]
135
        );
136
    }
137
}
138