Completed
Pull Request — master (#342)
by Christian
03:40
created

PostController::resolveRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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\Controller;
13
14
use Sonata\NewsBundle\Model\BlogInterface;
15
use Sonata\NewsBundle\Model\CommentInterface;
16
use Sonata\NewsBundle\Model\CommentManagerInterface;
17
use Sonata\NewsBundle\Model\PostInterface;
18
use Sonata\NewsBundle\Model\PostManagerInterface;
19
use Sonata\SeoBundle\Seo\SeoPageInterface;
20
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
21
use Symfony\Component\Form\FormInterface;
22
use Symfony\Component\HttpFoundation\RedirectResponse;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
27
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
28
29
class PostController extends Controller
30
{
31
    /**
32
     * @param Request $request
33
     *
34
     * @return RedirectResponse
35
     */
36
    public function homeAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
        return $this->redirect($this->generateUrl('sonata_news_archive'));
39
    }
40
41
    /**
42
     * @param Request $request
43
     * @param array   $criteria
44
     * @param array   $parameters
45
     *
46
     * @return Response
47
     */
48
    public function renderArchive(Request $request, array $criteria = array(), array $parameters = array())
49
    {
50
        $pager = $this->getPostManager()->getPager(
51
            $criteria,
52
            $request->get('page', 1)
53
        );
54
55
        $parameters = array_merge(array(
56
            'pager' => $pager,
57
            'blog' => $this->getBlog(),
58
            'tag' => false,
59
            'collection' => false,
60
            'route' => $request->get('_route'),
61
            'route_parameters' => $request->get('_route_params'),
62
        ), $parameters);
63
64
        $response = $this->render(sprintf('SonataNewsBundle:Post:archive.%s.twig', $request->getRequestFormat()), $parameters);
65
66
        if ('rss' === $request->getRequestFormat()) {
67
            $response->headers->set('Content-Type', 'application/rss+xml');
68
        }
69
70
        return $response;
71
    }
72
73
    /**
74
     * @param Request $request
75
     *
76
     * @return Response
77
     */
78
    public function archiveAction(Request $request)
79
    {
80
        return $this->renderArchive($request);
81
    }
82
83
    /**
84
     * @param Request $request
85
     * @param string  $tag
86
     *
87
     * @return Response
88
     */
89
    public function tagAction(Request $request, $tag)
90
    {
91
        $tag = $this->get('sonata.classification.manager.tag')->findOneBy(array(
92
            'slug' => $tag,
93
            'enabled' => true,
94
        ));
95
96
        if (!$tag || !$tag->getEnabled()) {
97
            throw new NotFoundHttpException('Unable to find the tag');
98
        }
99
100
        return $this->renderArchive($request, array(
101
            'tag' => $tag->getSlug()
102
        ), array(
103
            'tag' => $tag
104
        ));
105
    }
106
107
    /**
108
     * @param Request $request
109
     * @param string $collection
110
     *
111
     * @return Response
112
     *
113
     * @throws NotFoundHttpException
114
     */
115
    public function collectionAction(Request $request, $collection)
116
    {
117
        $collection = $this->get('sonata.classification.manager.collection')->findOneBy(array(
118
            'slug' => $collection,
119
            'enabled' => true,
120
        ));
121
122
        if (!$collection || !$collection->getEnabled()) {
123
            throw new NotFoundHttpException('Unable to find the collection');
124
        }
125
126
        return $this->renderArchive($request, array(
127
            'collection' => $collection
128
        ), array(
129
            'collection' => $collection
130
        ));
131
    }
132
133
    /**
134
     * @param string  $year
135
     * @param string  $month
136
     * @param Request $request
137
     *
138
     * @return Response
139
     */
140
    public function archiveMonthlyAction(Request $request, $year, $month)
141
    {
142
        return $this->renderArchive($request, array(
143
            'date' => $this->getPostManager()->getPublicationDateQueryParts(sprintf('%d-%d-%d', $year, $month, 1), 'month'),
144
        ));
145
    }
146
147
    /**
148
     * @param string  $year
149
     * @param Request $request
150
     *
151
     * @return Response
152
     */
153
    public function archiveYearlyAction(Request $request, $year)
154
    {
155
        return $this->renderArchive($request, array(
156
            'date' => $this->getPostManager()->getPublicationDateQueryParts(sprintf('%d-%d-%d', $year, 1, 1), 'year'),
157
        ));
158
    }
159
160
    /**
161
     * @throws NotFoundHttpException
162
     *
163
     * @param $permalink
164
     *
165
     * @return Response
166
     */
167
    public function viewAction(Request $request, $permalink)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
168
    {
169
        $post = $this->getPostManager()->findOneByPermalink($permalink, $this->getBlog());
170
171
        if (!$post || !$post->isPublic()) {
172
            throw new NotFoundHttpException('Unable to find the post');
173
        }
174
175
        if ($seoPage = $this->getSeoPage()) {
176
            $seoPage
177
                ->setTitle($post->getTitle())
178
                ->addMeta('name', 'description', $post->getAbstract())
179
                ->addMeta('property', 'og:title', $post->getTitle())
180
                ->addMeta('property', 'og:type', 'blog')
181
                ->addMeta('property', 'og:url', $this->generateUrl('sonata_news_view', array(
182
                    'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post, true),
0 ignored issues
show
Unused Code introduced by
The call to PermalinkInterface::generate() has too many arguments starting with true.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
183
                ), UrlGeneratorInterface::ABSOLUTE_URL))
184
                ->addMeta('property', 'og:description', $post->getAbstract())
185
            ;
186
        }
187
188
        return $this->render('SonataNewsBundle:Post:view.html.twig', array(
189
            'post' => $post,
190
            'form' => false,
191
            'blog' => $this->getBlog(),
192
        ));
193
    }
194
195
    /**
196
     * @return SeoPageInterface
197
     */
198
    public function getSeoPage()
199
    {
200
        if ($this->has('sonata.seo.page')) {
201
            return $this->get('sonata.seo.page');
202
        }
203
204
        return;
205
    }
206
207
    /**
208
     * @param Request $request
209
     * @param int     $postId
210
     *
211
     * @return Response
212
     */
213
    public function commentsAction(Request $request, $postId)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
    {
215
        $pager = $this->getCommentManager()
216
            ->getPager(array(
217
                'postId' => $postId,
218
                'status' => CommentInterface::STATUS_VALID,
219
            ), 1, 500); //no limit
220
221
        return $this->render('SonataNewsBundle:Post:comments.html.twig', array(
222
            'pager' => $pager,
223
        ));
224
    }
225
226
    /**
227
     * @param Request $request
228
     * @param string        $postId
229
     * @param bool    $form
230
     *
231
     * @return Response
232
     */
233
    public function addCommentFormAction(Request $request, $postId, $form = false)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
234
    {
235
        if (!$form) {
236
            $post = $this->getPostManager()->findOneBy(array(
237
                'id' => $postId,
238
            ));
239
240
            $form = $this->getCommentForm($post);
0 ignored issues
show
Documentation introduced by
$post is of type object|null, but the function expects a object<Sonata\NewsBundle\Model\PostInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
241
        }
242
243
        return $this->render('SonataNewsBundle:Post:comment_form.html.twig', array(
244
            'form' => $form->createView(),
0 ignored issues
show
Bug introduced by
It seems like $form is not always an object, but can also be of type boolean. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
245
            'post_id' => $postId,
246
        ));
247
    }
248
249
    /**
250
     * @param PostInterface $post
251
     *
252
     * @return FormInterface
253
     */
254
    public function getCommentForm(PostInterface $post)
255
    {
256
        $comment = $this->getCommentManager()->create();
257
        $comment->setPost($post);
258
        $comment->setStatus($post->getCommentsDefaultStatus());
259
260
        return $this->get('form.factory')->createNamed('comment', 'sonata_post_comment', $comment, array(
261
            'action' => $this->generateUrl('sonata_news_add_comment', array(
262
                'id' => $post->getId(),
263
            )),
264
            'method' => 'POST',
265
        ));
266
    }
267
268
    /**
269
     * @throws NotFoundHttpException
270
     *
271
     * @param Request $request
272
     * @param string  $id
273
     *
274
     * @return Response
275
     */
276
    public function addCommentAction(Request $request, $id)
277
    {
278
        $post = $this->getPostManager()->findOneBy(array(
279
            'id' => $id,
280
        ));
281
282
        if (!$post) {
283
            throw new NotFoundHttpException(sprintf('Post (%d) not found', $id));
284
        }
285
286
        if (!$post->isCommentable()) {
287
            // todo add notice
288
            return new RedirectResponse($this->generateUrl('sonata_news_view', array(
289
                'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post),
290
            )));
291
        }
292
293
        $form = $this->getCommentForm($post);
294
        $form->submit($request);
295
296
        if ($form->isValid()) {
297
            $comment = $form->getData();
298
299
            $this->getCommentManager()->save($comment);
300
            $this->get('sonata.news.mailer')->sendCommentNotification($comment);
301
302
            // todo : add notice
303
            return new RedirectResponse($this->generateUrl('sonata_news_view', array(
304
                'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($post),
305
            )));
306
        }
307
308
        return $this->render('SonataNewsBundle:Post:view.html.twig', array(
309
            'post' => $post,
310
            'form' => $form,
311
        ));
312
    }
313
314
    /**
315
     * @param Request $request
316
     * @param string  $commentId
317
     * @param string  $hash
318
     * @param string  $status
319
     *
320
     * @return RedirectResponse
321
     */
322
    public function commentModerationAction(Request $request, $commentId, $hash, $status)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
323
    {
324
        $comment = $this->getCommentManager()->findOneBy(array('id' => $commentId));
325
326
        if (!$comment) {
327
            throw new AccessDeniedException();
328
        }
329
330
        $computedHash = $this->get('sonata.news.hash.generator')->generate($comment);
331
332
        if ($computedHash != $hash) {
333
            throw new AccessDeniedException();
334
        }
335
336
        $comment->setStatus($status);
337
338
        $this->getCommentManager()->save($comment);
339
340
        return new RedirectResponse($this->generateUrl('sonata_news_view', array(
341
            'permalink' => $this->getBlog()->getPermalinkGenerator()->generate($comment->getPost()),
342
        )));
343
    }
344
345
    /**
346
     * @return PostManagerInterface
347
     */
348
    protected function getPostManager()
349
    {
350
        return $this->get('sonata.news.manager.post');
351
    }
352
353
    /**
354
     * @return CommentManagerInterface
355
     */
356
    protected function getCommentManager()
357
    {
358
        return $this->get('sonata.news.manager.comment');
359
    }
360
361
    /**
362
     * @return BlogInterface
363
     */
364
    protected function getBlog()
365
    {
366
        return $this->get('sonata.news.blog');
367
    }
368
}
369