PollSetterController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setPollFromRequest() 0 20 4
A unsetPoll() 0 5 1
1
<?php
2
/*
3
 * Copyright 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\Poll;
25
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28
29
abstract class PollSetterController extends Controller
30
{
31
    /** @var Poll $poll */
32
    protected $poll;
33
34
    /**
35
     * Set poll from Url or session
36
     */
37
    public function setPollFromRequest(Request $request)
38
    {
39
        $em = $this->getDoctrine()->getManager();
40
        $pollUrl = $request->get('pollUrl');
41
        if ($pollUrl) {
42
            $request->getSession()->set('pollUrl', $pollUrl);
43
        }
44
        else {
45
            $pollUrl = $request->getSession()->get('pollUrl');
46
        }
47
        if ($pollUrl) {
48
            $repository = $em->getRepository('KyelaBundle:Poll');
49
            $this->poll = $repository->findOneByUrl($pollUrl);
0 ignored issues
show
Bug introduced by
The method findOneByUrl() does not exist on Doctrine\Persistence\ObjectRepository. Did you maybe mean findOneBy()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
50
            if (!$this->poll)
51
            {
52
                $this->unsetPoll($request);
53
                throw new NotFoundHttpException('Poll object not found.');
54
            }
55
        }
56
    }
57
58
    protected function unsetPoll(Request $request)
59
    {
60
        $this->poll = null;
61
        $request->getSession()->remove('pollUrl');
62
    }
63
}
64