ViewPostAction   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 8
dl 0
loc 82
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __invoke() 0 27 4
A setSeoPage() 0 4 1
A isVisible() 0 6 3
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
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 "Symfony\Bundle\FrameworkBundle\Controller\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...
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 || !$this->isVisible($post)) {
70
            throw new NotFoundHttpException('Unable to find the post');
71
        }
72
73
        if ($seoPage = $this->seoPage) {
74
            $seoPage
75
                ->addTitle($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
    public function setSeoPage(?SeoPageInterface $seoPage = null): void
94
    {
95
        $this->seoPage = $seoPage;
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    protected function isVisible(PostInterface $post)
102
    {
103
        return $post->isPublic() ||
104
            $this->authChecker->isGranted('ROLE_SUPER_ADMIN') ||
105
            $this->authChecker->isGranted('ROLE_SONATA_NEWS_ADMIN_POST_EDIT');
106
    }
107
}
108