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