Completed
Push — master ( 37876c...34238f )
by Marko
9s
created

CreateCommentAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 12
dl 0
loc 111
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A __invoke() 0 37 5
A getCommentForm() 0 13 1
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\Form\Type\CommentType;
17
use Sonata\NewsBundle\Mailer\MailerInterface;
18
use Sonata\NewsBundle\Model\BlogInterface;
19
use Sonata\NewsBundle\Model\CommentManagerInterface;
20
use Sonata\NewsBundle\Model\PostInterface;
21
use Sonata\NewsBundle\Model\PostManagerInterface;
22
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
23
use Symfony\Component\Form\FormFactoryInterface;
24
use Symfony\Component\Form\FormInterface;
25
use Symfony\Component\HttpFoundation\RedirectResponse;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
use Symfony\Component\Routing\RouterInterface;
30
31
final class CreateCommentAction extends Controller
32
{
33
    /**
34
     * @var RouterInterface
35
     */
36
    private $router;
37
38
    /**
39
     * @var BlogInterface
40
     */
41
    private $blog;
42
43
    /**
44
     * @var PostManagerInterface
45
     */
46
    private $postManager;
47
48
    /**
49
     * @var CommentManagerInterface
50
     */
51
    private $commentManager;
52
53
    /**
54
     * @var FormFactoryInterface
55
     */
56
    private $formFactory;
57
58
    /**
59
     * @var MailerInterface
60
     */
61
    private $mailer;
62
63
    public function __construct(
64
        RouterInterface $router,
65
        BlogInterface $blog,
66
        PostManagerInterface $postManager,
67
        CommentManagerInterface $commentManager,
68
        FormFactoryInterface $formFactory,
69
        MailerInterface $mailer
70
    ) {
71
        $this->router = $router;
72
        $this->blog = $blog;
73
        $this->postManager = $postManager;
74
        $this->commentManager = $commentManager;
75
        $this->formFactory = $formFactory;
76
        $this->mailer = $mailer;
77
    }
78
79
    /**
80
     * @param string  $id
81
     * @param Request $request
82
     *
83
     * @throws NotFoundHttpException
84
     *
85
     * @return Response
86
     */
87
    public function __invoke(Request $request, $id)
88
    {
89
        $post = $this->postManager->findOneBy([
90
            'id' => $id,
91
        ]);
92
93
        if (!$post) {
94
            throw new NotFoundHttpException(sprintf('Post (%d) not found', $id));
95
        }
96
97
        if (!$post->isCommentable()) {
98
            // todo add notice.
99
            return new RedirectResponse($this->router->generate('sonata_news_view', [
100
                'permalink' => $this->blog->getPermalinkGenerator()->generate($post),
101
            ]));
102
        }
103
104
        $form = $this->getCommentForm($post);
105
        $form->handleRequest($request);
106
107
        if ($form->isSubmitted() && $form->isValid()) {
108
            $comment = $form->getData();
109
110
            $this->commentManager->save($comment);
111
            $this->mailer->sendCommentNotification($comment);
112
113
            // todo : add notice
114
            return new RedirectResponse($this->router->generate('sonata_news_view', [
115
                'permalink' => $this->blog->getPermalinkGenerator()->generate($post),
116
            ]));
117
        }
118
119
        return $this->render('@SonataNews/Post/view.html.twig', [
120
            'post' => $post,
121
            'form' => $form,
122
        ]);
123
    }
124
125
    /**
126
     * @return FormInterface
127
     */
128
    private function getCommentForm(PostInterface $post)
129
    {
130
        $comment = $this->commentManager->create();
131
        $comment->setPost($post);
132
        $comment->setStatus($post->getCommentsDefaultStatus());
133
134
        return $this->formFactory->createNamed('comment', CommentType::class, $comment, [
135
            'action' => $this->router->generate('sonata_news_add_comment', [
136
                'id' => $post->getId(),
137
            ]),
138
            'method' => 'POST',
139
        ]);
140
    }
141
}
142