Completed
Pull Request — master (#465)
by Marko
27:33
created

ViewPostAction::__invoke()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 4
nc 3
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\Action;
13
14
use Sonata\NewsBundle\Model\BlogInterface;
15
use Sonata\NewsBundle\Model\PostInterface;
16
use Sonata\NewsBundle\Model\PostManagerInterface;
17
use Sonata\SeoBundle\Seo\SeoPageInterface;
18
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
23
24
final class ViewPostAction extends Controller
25
{
26
    /**
27
     * @var BlogInterface
28
     */
29
    private $blog;
30
31
    /**
32
     * @var PostManagerInterface
33
     */
34
    private $postManager;
35
36
    /**
37
     * @var AuthorizationCheckerInterface
38
     */
39
    private $authChecker;
40
41
    /**
42
     * @var SeoPageInterface|null
43
     */
44
    private $seoPage;
45
46
    public function __construct(
47
        BlogInterface $blog,
48
        PostManagerInterface $postManager,
49
        AuthorizationCheckerInterface $authChecker
50
    ) {
51
        $this->blog = $blog;
52
        $this->postManager = $postManager;
53
        $this->authChecker = $authChecker;
54
    }
55
56
    /**
57
     * @param string $permalink
58
     *
59
     * @throws NotFoundHttpException
60
     *
61
     * @return Response
62
     */
63
    public function __invoke($permalink)
64
    {
65
        $post = $this->postManager->findOneByPermalink($permalink, $this->blog);
66
67
        if (!$post || !$post->isPublic()) {
68
            throw new NotFoundHttpException('Unable to find the post');
69
        }
70
71
        if ($seoPage = $this->seoPage) {
72
            $seoPage
73
                ->setTitle($post->getTitle())
74
                ->addMeta('name', 'description', $post->getAbstract())
75
                ->addMeta('property', 'og:title', $post->getTitle())
76
                ->addMeta('property', 'og:type', 'blog')
77
                ->addMeta('property', 'og:url', $this->generateUrl('sonata_news_view', [
78
                    'permalink' => $this->blog->getPermalinkGenerator()->generate($post),
79
                ], UrlGeneratorInterface::ABSOLUTE_URL))
80
                ->addMeta('property', 'og:description', $post->getAbstract())
81
            ;
82
        }
83
84
        return $this->render('@SonataNews/Post/view.html.twig', [
85
            'post' => $post,
86
            'form' => false,
87
            'blog' => $this->blog,
88
        ]);
89
    }
90
91
    /**
92
     * @param null|SeoPageInterface $seoPage
93
     */
94
    public function setSeoPage(SeoPageInterface $seoPage = null)
95
    {
96
        $this->seoPage = $seoPage;
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    protected function isVisible(PostInterface $post)
103
    {
104
        return $post->isPublic() ||
105
            $this->authChecker->isGranted('ROLE_SUPER_ADMIN') ||
106
            $this->authChecker->isGranted('ROLE_SONATA_NEWS_ADMIN_POST_EDIT');
107
    }
108
}
109