Completed
Pull Request — 3.x (#520)
by Christian
11:07
created

CreateCommentAction::createComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 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\Event\FilterCommentResponseEvent;
17
use Sonata\NewsBundle\Event\FormEvent;
18
use Sonata\NewsBundle\Event\GetResponseCommentEvent;
19
use Sonata\NewsBundle\Form\Type\CommentType;
20
use Sonata\NewsBundle\Mailer\MailerInterface;
21
use Sonata\NewsBundle\Model\BlogInterface;
22
use Sonata\NewsBundle\Model\CommentInterface;
23
use Sonata\NewsBundle\Model\CommentManagerInterface;
24
use Sonata\NewsBundle\Model\PostInterface;
25
use Sonata\NewsBundle\Model\PostManagerInterface;
26
use Sonata\NewsBundle\SonataNewsEvents;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
29
use Symfony\Component\Form\FormFactoryInterface;
30
use Symfony\Component\Form\FormInterface;
31
use Symfony\Component\HttpFoundation\RedirectResponse;
32
use Symfony\Component\HttpFoundation\Request;
33
use Symfony\Component\HttpFoundation\Response;
34
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
35
use Symfony\Component\Routing\RouterInterface;
36
37
final class CreateCommentAction extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use {@see AbstractController} instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
38
{
39
    /**
40
     * @var RouterInterface
41
     */
42
    private $router;
43
44
    /**
45
     * @var BlogInterface
46
     */
47
    private $blog;
48
49
    /**
50
     * @var PostManagerInterface
51
     */
52
    private $postManager;
53
54
    /**
55
     * @var CommentManagerInterface
56
     */
57
    private $commentManager;
58
59
    /**
60
     * @var FormFactoryInterface
61
     */
62
    private $formFactory;
63
64
    /**
65
     * @var MailerInterface
66
     */
67
    private $mailer;
68
69
    /**
70
     * @var EventDispatcherInterface
71
     */
72
    private $eventDispatcher;
73
74
    public function __construct(
75
        RouterInterface $router,
76
        BlogInterface $blog,
77
        PostManagerInterface $postManager,
78
        CommentManagerInterface $commentManager,
79
        FormFactoryInterface $formFactory,
80
        MailerInterface $mailer,
81
        EventDispatcherInterface $eventDispatcher
82
    ) {
83
        $this->router = $router;
84
        $this->blog = $blog;
85
        $this->postManager = $postManager;
86
        $this->commentManager = $commentManager;
87
        $this->formFactory = $formFactory;
88
        $this->mailer = $mailer;
89
        $this->eventDispatcher = $eventDispatcher;
90
    }
91
92
    /**
93
     * @param string  $id
94
     * @param Request $request
95
     *
96
     * @throws NotFoundHttpException
97
     *
98
     * @return Response
99
     */
100
    public function __invoke(Request $request, $id)
101
    {
102
        $post = $this->postManager->findOneBy([
103
            'id' => $id,
104
        ]);
105
106
        if (!$post instanceof PostInterface) {
107
            throw new NotFoundHttpException(sprintf('Post (%d) not found', $id));
108
        }
109
110
        if (!$post->isCommentable()) {
111
            // todo : add notice in event listener
112
            return new RedirectResponse($this->router->generate('sonata_news_view', [
113
                'permalink' => $this->blog->getPermalinkGenerator()->generate($post),
114
            ]));
115
        }
116
117
        $comment = $this->createComment($post);
118
119
        $event = new GetResponseCommentEvent($comment, $request);
120
        $this->eventDispatcher->dispatch(SonataNewsEvents::COMMENT_INITIALIZE, $event);
121
122
        if (null !== $event->getResponse()) {
123
            return $event->getResponse();
124
        }
125
126
        $form = $this->getCommentForm($comment);
127
        $form->handleRequest($request);
128
129
        if ($form->isSubmitted() && $form->isValid()) {
130
            $event = new FormEvent($form, $request);
131
            $this->eventDispatcher->dispatch(SonataNewsEvents::COMMENT_SUCCESS, $event);
132
133
            $comment = $form->getData();
134
135
            $this->commentManager->save($comment);
136
            $this->mailer->sendCommentNotification($comment);
137
138
            $response = new RedirectResponse($this->router->generate('sonata_news_view', [
139
                'permalink' => $this->blog->getPermalinkGenerator()->generate($post),
140
            ]));
141
142
            // todo : add notice in event listener
143
            $this->eventDispatcher->dispatch(SonataNewsEvents::COMMENT_COMPLETED, new FilterCommentResponseEvent($comment, $request, $response));
144
145
            return $response;
146
        }
147
148
        return $this->render('@SonataNews/Post/view.html.twig', [
149
            'post' => $post,
150
            'form' => $form,
151
        ]);
152
    }
153
154
    private function getCommentForm(CommentInterface $comment): FormInterface
155
    {
156
        return $this->formFactory->createNamed('comment', CommentType::class, $comment, [
157
            'action' => $this->router->generate('sonata_news_add_comment', [
158
                'id' => $comment->getPost()->getId(),
159
            ]),
160
            'method' => 'POST',
161
        ]);
162
    }
163
164
    private function createComment(PostInterface $post): CommentInterface
165
    {
166
        $comment = $this->commentManager->create();
167
        $comment->setPost($post);
168
        $comment->setStatus($post->getCommentsDefaultStatus());
169
170
        return $comment;
171
    }
172
}
173