VoteController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 24
dl 0
loc 49
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A votePositive() 0 15 4
A __construct() 0 3 1
B voteNegative() 0 20 7
1
<?php
2
3
/*
4
 * This file is part of the MsalsasVotingBundle package.
5
 *
6
 * (c) Manolo Salsas
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Msalsas\VotingBundle\Controller;
13
14
use Msalsas\VotingBundle\Service\Voter;
15
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16
use Symfony\Component\Finder\Exception\AccessDeniedException;
17
use Symfony\Component\HttpFoundation\Request;
18
19
class VoteController extends Controller
20
{
21
    /**
22
     * @var Voter
23
     */
24
    protected $voter;
25
26
    public function __construct(Voter $voter)
27
    {
28
        $this->voter = $voter;
29
    }
30
31
    public function votePositive($id, Request $request)
32
    {
33
        if (!$request->isXmlHttpRequest()) {
34
            throw new AccessDeniedException();
35
        }
36
37
        try {
38
            $votesCount = $this->voter->votePositive($id);
39
        } catch (AccessDeniedException $e) {
40
            return $this->json($e->getMessage(), 403);
41
        } catch (\Exception $e) {
42
            return $this->json($e->getMessage(), 403);
43
        }
44
45
        return $this->json($votesCount);
46
    }
47
48
    public function voteNegative($id, Request $request)
49
    {
50
        if (!$request->isXmlHttpRequest()) {
51
            throw new AccessDeniedException();
52
        }
53
54
        $reason = $request->getContent();
55
        if (!$reason || !is_string($reason) || !in_array($reason, $this->voter->getNegativeReasons())) {
56
            throw new AccessDeniedException();
57
        }
58
59
        try {
60
            $votesCount = $this->voter->voteNegative($id, $reason);
61
        } catch (AccessDeniedException $e) {
62
            return $this->json($e->getMessage(), 403);
63
        } catch (\Exception $e) {
64
            return $this->json($e->getMessage(), 403);
65
        }
66
67
        return $this->json($votesCount);
68
    }
69
}