NewsExtension::generatePermalink()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Twig\Extension;
15
16
use Sonata\ClassificationBundle\Model\TagManagerInterface;
17
use Sonata\Doctrine\Model\ManagerInterface;
18
use Sonata\NewsBundle\Model\BlogInterface;
19
use Sonata\NewsBundle\Model\PostInterface;
20
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
21
use Symfony\Component\Routing\RouterInterface;
22
use Twig\Environment;
23
use Twig\Extension\AbstractExtension;
24
use Twig\Extension\InitRuntimeInterface;
25
use Twig\TwigFunction;
26
27
class NewsExtension extends AbstractExtension implements InitRuntimeInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Twig\Extension\InitRuntimeInterface has been deprecated with message: since Twig 2.7, to be removed in 3.0

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...
28
{
29
    /**
30
     * @var RouterInterface
31
     */
32
    private $router;
33
34
    /**
35
     * @var TagManagerInterface
36
     */
37
    private $tagManager;
38
39
    /**
40
     * @var Environment
41
     */
42
    private $environment;
43
44
    /**
45
     * @var BlogInterface
46
     */
47
    private $blog;
48
49
    public function __construct(RouterInterface $router, ManagerInterface $tagManager, BlogInterface $blog)
50
    {
51
        if (!$tagManager instanceof TagManagerInterface) {
52
            @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...
53
                'Calling the '.__METHOD__.' method with a Sonata\Doctrine\Model\ManagerInterface is deprecated'
54
                .' since version 2.4 and will be removed in 4.0.'
55
                .' Use the new signature with a Sonata\ClassificationBundle\Model\TagManagerInterface instead.',
56
                E_USER_DEPRECATED
57
            );
58
        }
59
60
        $this->router = $router;
61
        $this->tagManager = $tagManager;
0 ignored issues
show
Documentation Bug introduced by
$tagManager is of type object<Sonata\Doctrine\Model\ManagerInterface>, but the property $tagManager was declared to be of type object<Sonata\Classifica...el\TagManagerInterface>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
62
        $this->blog = $blog;
63
    }
64
65
    /**
66
     * Returns a list of functions to add to the existing list.
67
     *
68
     * @return array An array of functions
69
     */
70
    public function getFunctions()
71
    {
72
        return [
73
            new TwigFunction(
74
                'sonata_news_link_tag_rss',
75
                [$this, 'renderTagRss', ['is_safe' => ['html']]]
76
            ),
77
            new TwigFunction(
78
                'sonata_news_permalink',
79
                [$this, 'generatePermalink']
80
            ),
81
        ];
82
    }
83
84
    public function initRuntime(Environment $environment): void
85
    {
86
        $this->environment = $environment;
87
    }
88
89
    /**
90
     * Returns the name of the extension.
91
     *
92
     * @return string The extension name
93
     */
94
    public function getName()
95
    {
96
        return 'sonata_news';
97
    }
98
99
    /**
100
     * @return string
101
     */
102
    public function renderTagRss()
103
    {
104
        $rss = [];
105
        foreach ($this->tagManager->findBy(['enabled' => true]) as $tag) {
106
            $rss[] = sprintf(
107
                '<link href="%s" title="%s : %s" type="application/rss+xml" rel="alternate" />',
108
                $this->router->generate('sonata_news_tag', ['tag' => $tag->getSlug(), '_format' => 'rss'], UrlGeneratorInterface::ABSOLUTE_URL),
109
                $this->blog->getTitle(),
110
                $tag->getName()
111
            );
112
        }
113
114
        return implode("\n", $rss);
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function generatePermalink(PostInterface $post)
121
    {
122
        return $this->blog->getPermalinkGenerator()->generate($post);
123
    }
124
}
125