Completed
Push — 3.x ( fe4009...05c76d )
by Grégoire
03:50
created

PostController::addCommentAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 46 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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\Controller;
13
14
// NEXT_MAJOR: remove this file
15
16
@trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
17
    'The '.__NAMESPACE__.'\PostController class is deprecated since version 3.x and will be removed in 4.0.'
18
    .' Use '.__NAMESPACE__.'\Action\* classes instead.',
19
    E_USER_DEPRECATED
20
);
21
22
use Sonata\NewsBundle\Action\CollectionPostArchiveAction;
23
use Sonata\NewsBundle\Action\CommentListAction;
24
use Sonata\NewsBundle\Action\CreateCommentAction;
25
use Sonata\NewsBundle\Action\CreateCommentFormAction;
26
use Sonata\NewsBundle\Action\ModerateCommentAction;
27
use Sonata\NewsBundle\Action\MonthlyPostArchiveAction;
28
use Sonata\NewsBundle\Action\PostArchiveAction;
29
use Sonata\NewsBundle\Action\TagPostArchiveAction;
30
use Sonata\NewsBundle\Action\ViewPostAction;
31
use Sonata\NewsBundle\Action\YearlyPostArchiveAction;
32
use Sonata\NewsBundle\Form\Type\CommentType;
33
use Sonata\NewsBundle\Model\BlogInterface;
34
use Sonata\NewsBundle\Model\CommentManagerInterface;
35
use Sonata\NewsBundle\Model\PostInterface;
36
use Sonata\NewsBundle\Model\PostManagerInterface;
37
use Sonata\SeoBundle\Seo\SeoPageInterface;
38
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
39
use Symfony\Component\Form\FormInterface;
40
use Symfony\Component\HttpFoundation\RedirectResponse;
41
use Symfony\Component\HttpFoundation\Request;
42
use Symfony\Component\HttpFoundation\Response;
43
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
44
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
45
46
class PostController extends Controller
47
{
48
    /**
49
     * @return RedirectResponse
50
     */
51
    public function homeAction()
52
    {
53
        return $this->redirect($this->generateUrl('sonata_news_archive'));
54
    }
55
56
    /**
57
     * @param array   $criteria
58
     * @param array   $parameters
59
     * @param Request $request
60
     *
61
     * @return Response
62
     */
63
    public function renderArchive(array $criteria = [], array $parameters = [], Request $request = null)
64
    {
65
        $action = $this->container->get(PostArchiveAction::class);
66
67
        return $action->renderArchive($this->resolveRequest($request), $criteria, $parameters);
68
    }
69
70
    /**
71
     * @param Request $request
72
     *
73
     * @return Response
74
     */
75
    public function archiveAction(Request $request = null)
76
    {
77
        $action = $this->container->get(PostArchiveAction::class);
78
79
        return $action($this->resolveRequest($request));
80
    }
81
82
    /**
83
     * @param string  $tag
84
     * @param Request $request
85
     *
86
     * @throws NotFoundHttpException
87
     *
88
     * @return Response
89
     */
90
    public function tagAction($tag, Request $request = null)
91
    {
92
        $action = $this->container->get(TagPostArchiveAction::class);
93
94
        return $action($this->resolveRequest($request), $tag);
95
    }
96
97
    /**
98
     * @param string  $collection
99
     * @param Request $request
100
     *
101
     * @throws NotFoundHttpException
102
     *
103
     * @return Response
104
     */
105
    public function collectionAction($collection, Request $request = null)
106
    {
107
        $action = $this->container->get(CollectionPostArchiveAction::class);
108
109
        return $action($this->resolveRequest($request), $collection);
110
    }
111
112
    /**
113
     * @param string  $year
114
     * @param string  $month
115
     * @param Request $request
116
     *
117
     * @return Response
118
     */
119
    public function archiveMonthlyAction($year, $month, Request $request = null)
120
    {
121
        $action = $this->container->get(MonthlyPostArchiveAction::class);
122
123
        return $action($this->resolveRequest($request), $year, $month);
124
    }
125
126
    /**
127
     * @param string  $year
128
     * @param Request $request
129
     *
130
     * @return Response
131
     */
132
    public function archiveYearlyAction($year, Request $request = null)
133
    {
134
        $action = $this->container->get(YearlyPostArchiveAction::class);
135
136
        return $action($this->resolveRequest($request), $year);
137
    }
138
139
    /**
140
     * @param string $permalink
141
     *
142
     * @throws NotFoundHttpException
143
     *
144
     * @return Response
145
     */
146
    public function viewAction($permalink)
147
    {
148
        $action = $this->container->get(ViewPostAction::class);
149
150
        return $action($permalink);
151
    }
152
153
    /**
154
     * @return SeoPageInterface|null
155
     */
156
    public function getSeoPage()
157
    {
158
        if ($this->has('sonata.seo.page')) {
159
            return $this->get('sonata.seo.page');
160
        }
161
162
        return null;
163
    }
164
165
    /**
166
     * @param int $postId
167
     *
168
     * @return Response
169
     */
170
    public function commentsAction($postId)
171
    {
172
        $action = $this->container->get(CommentListAction::class);
173
174
        return $action($postId);
175
    }
176
177
    /**
178
     * @param $postId
179
     * @param bool $form
180
     *
181
     * @return Response
182
     */
183
    public function addCommentFormAction($postId, $form = false)
184
    {
185
        $action = $this->container->get(CreateCommentFormAction::class);
186
187
        return $action($postId, $form);
188
    }
189
190
    /**
191
     * @param PostInterface $post
192
     *
193
     * @return FormInterface
194
     */
195
    public function getCommentForm(PostInterface $post)
196
    {
197
        $comment = $this->getCommentManager()->create();
198
        $comment->setPost($post);
199
        $comment->setStatus($post->getCommentsDefaultStatus());
200
201
        return $this->get('form.factory')->createNamed('comment', CommentType::class, $comment, [
202
            'action' => $this->generateUrl('sonata_news_add_comment', [
203
                'id' => $post->getId(),
204
            ]),
205
            'method' => 'POST',
206
        ]);
207
    }
208
209
    /**
210
     * @param string  $id
211
     * @param Request $request
212
     *
213
     * @throws NotFoundHttpException
214
     *
215
     * @return Response
216
     */
217
    public function addCommentAction($id, Request $request = null)
218
    {
219
        $action = $this->container->get(CreateCommentAction::class);
220
221
        return $action($this->resolveRequest($request), $id);
222
    }
223
224
    /**
225
     * @param string $commentId
226
     * @param string $hash
227
     * @param string $status
228
     *
229
     * @throws AccessDeniedException
230
     *
231
     * @return RedirectResponse
232
     */
233
    public function commentModerationAction($commentId, $hash, $status)
234
    {
235
        $action = $this->container->get(ModerateCommentAction::class);
236
237
        return $action($commentId, $hash, $status);
238
    }
239
240
    /**
241
     * @return PostManagerInterface
242
     */
243
    protected function getPostManager()
244
    {
245
        return $this->get('sonata.news.manager.post');
246
    }
247
248
    /**
249
     * @return CommentManagerInterface
250
     */
251
    protected function getCommentManager()
252
    {
253
        return $this->get('sonata.news.manager.comment');
254
    }
255
256
    /**
257
     * @return BlogInterface
258
     */
259
    protected function getBlog()
260
    {
261
        return $this->get('sonata.news.blog');
262
    }
263
264
    /**
265
     * To keep backwards compatibility with older Sonata News code.
266
     *
267
     * @internal
268
     *
269
     * @param Request $request
270
     *
271
     * @return Request
272
     */
273
    private function resolveRequest(Request $request = null)
274
    {
275
        if (null === $request) {
276
            return $this->get('request_stack')->getCurrentRequest();
277
        }
278
279
        return $request;
280
    }
281
}
282