|
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
|
|
|
} |