Completed
Push — master ( 37876c...34238f )
by Marko
9s
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
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