forked from FriendsOfSymfony/FOSCommentBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultVoteCreator.php
66 lines (57 loc) · 1.72 KB
/
DefaultVoteCreator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* This file is part of the FOSCommentBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace FOS\CommentBundle\Creator;
use FOS\CommentBundle\Blamer\VoteBlamerInterface;
use FOS\CommentBundle\Model\VotableCommentInterface;
use FOS\CommentBundle\Model\VoteInterface;
use FOS\CommentBundle\Model\VoteManagerInterface;
use Symfony\Component\Validator\ValidatorInterface;
/**
* Manages the creation and persistence of Vote objects.
*
* @author Tim Nagel <[email protected]>
*/
class DefaultVoteCreator implements VoteCreatorInterface
{
protected $voteBlamer;
protected $voteManager;
/**
* Constructor.
*
* @param VoteManagerInterface $voteManager
* @param VoteBlamerInterface $voteBlamer
* @param Validator $validator
*/
public function __construct(VoteManagerInterface $voteManager, VoteBlamerInterface $voteBlamer, ValidatorInterface $validator)
{
$this->validator = $validator;
$this->voteBlamer = $voteBlamer;
$this->voteManager = $voteManager;
}
/**
* Validates that a vote is suitable for persisting and persists
* the Vote.
*
* TODO: rate-limiting addition of votes
*
* @param VoteInterface $vote
* @param VotableCommentInterface $comment
* @return bool
*/
public function create(VoteInterface $vote, VotableCommentInterface $comment)
{
$this->voteBlamer->blame($vote);
if (!$this->validator->validate($vote)) {
return false;
}
$this->voteManager->addVote($vote, $comment);
return true;
}
}