1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NewsBundle\Action; |
15
|
|
|
|
16
|
|
|
use Sonata\NewsBundle\Model\BlogInterface; |
17
|
|
|
use Sonata\NewsBundle\Model\CommentManagerInterface; |
18
|
|
|
use Sonata\NewsBundle\Util\HashGeneratorInterface; |
19
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
22
|
|
|
|
23
|
|
|
final class ModerateCommentAction |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var RouterInterface |
27
|
|
|
*/ |
28
|
|
|
private $router; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var BlogInterface |
32
|
|
|
*/ |
33
|
|
|
private $blog; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var CommentManagerInterface |
37
|
|
|
*/ |
38
|
|
|
private $commentManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var HashGeneratorInterface |
42
|
|
|
*/ |
43
|
|
|
private $hashGenerator; |
44
|
|
|
|
45
|
|
|
public function __construct( |
46
|
|
|
RouterInterface $router, |
47
|
|
|
BlogInterface $blog, |
48
|
|
|
CommentManagerInterface $commentManager, |
49
|
|
|
HashGeneratorInterface $hashGenerator |
50
|
|
|
) { |
51
|
|
|
$this->router = $router; |
52
|
|
|
$this->blog = $blog; |
53
|
|
|
$this->commentManager = $commentManager; |
54
|
|
|
$this->hashGenerator = $hashGenerator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $commentId |
59
|
|
|
* @param string $hash |
60
|
|
|
* @param string $status |
61
|
|
|
* |
62
|
|
|
* @throws AccessDeniedException |
63
|
|
|
* |
64
|
|
|
* @return RedirectResponse |
65
|
|
|
*/ |
66
|
|
|
public function __invoke($commentId, $hash, $status) |
67
|
|
|
{ |
68
|
|
|
$comment = $this->commentManager->findOneBy(['id' => $commentId]); |
69
|
|
|
|
70
|
|
|
if (!$comment) { |
71
|
|
|
throw new AccessDeniedException(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$computedHash = $this->hashGenerator->generate($comment); |
75
|
|
|
|
76
|
|
|
if ($computedHash != $hash) { |
77
|
|
|
throw new AccessDeniedException(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$comment->setStatus($status); |
81
|
|
|
|
82
|
|
|
$this->commentManager->save($comment); |
83
|
|
|
|
84
|
|
|
return new RedirectResponse($this->router->generate('sonata_news_view', [ |
85
|
|
|
'permalink' => $this->blog->getPermalinkGenerator()->generate($comment->getPost()), |
86
|
|
|
])); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|