Completed
Pull Request — master (#465)
by Marko
27:33
created

ModerateCommentAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 7
dl 0
loc 66
rs 10
c 0
b 0
f 0

2 Methods

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