Completed
Branch master (600230)
by Andy
03:12 queued 59s
created

CanonicalLinkExtension::generateUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Palmtree\CanonicalUrlBundle\Twig\Extension;
4
5
use Palmtree\CanonicalUrlBundle\Service\CanonicalUrlGenerator;
6
use Symfony\Component\HttpFoundation\RequestStack;
7
8
class CanonicalLinkExtension extends \Twig_Extension
9
{
10
    /** @var CanonicalUrlGenerator */
11
    protected $canonicalUrlGenerator;
12
    /** @var RequestStack */
13
    protected $requestStack;
14
15
    /**
16
     * CanonicalLinkExtension constructor.
17
     * @param CanonicalUrlGenerator $canonicalUrlGenerator
18
     * @param RequestStack          $requestStack
19
     */
20 3
    public function __construct(CanonicalUrlGenerator $canonicalUrlGenerator, RequestStack $requestStack)
21
    {
22 3
        $this->canonicalUrlGenerator = $canonicalUrlGenerator;
23 3
        $this->requestStack          = $requestStack;
24 3
    }
25
26
    /**
27
     * @return \Twig_Function[]
28
     */
29 1
    public function getFunctions()
30
    {
31
        return [
32 1
            new \Twig_SimpleFunction('palmtree_canonical_url', [$this, 'generateUrl']),
33 1
            new \Twig_SimpleFunction('palmtree_canonical_link_tag', [$this, 'renderLinkTag'], [
34 1
                'needs_environment' => true,
35
                'is_safe'           => ['html'],
36
            ]),
37
        ];
38
    }
39
40
    /**
41
     * @param \Twig_Environment $environment
42
     * @param string            $href
43
     *
44
     * @return string
45
     */
46 1
    public function renderLinkTag(\Twig_Environment $environment, $href = null)
47
    {
48 1
        $output = $environment->render('@PalmtreeCanonicalUrl/canonical_link_tag.html.twig', [
49 1
            'href' => $href,
50
        ]);
51
52 1
        return $output;
53
    }
54
55
    /**
56
     * @param string       $route
57
     * @param string|array $parameters
58
     *
59
     * @see CanonicalUrlGenerator::generate() For parameter descriptions.
60
     *
61
     * @return string
62
     */
63 2
    public function generateUrl($route = null, $parameters = null)
64
    {
65 2
        if ($route === null) {
66 1
            $request = $this->requestStack->getCurrentRequest();
67 1
            $route   = $request->attributes->get('_route');
68
        }
69
70 2
        return $this->canonicalUrlGenerator->generate($route, $parameters);
0 ignored issues
show
Bug introduced by
It seems like $parameters defined by parameter $parameters on line 63 can also be of type null; however, Palmtree\CanonicalUrlBun...rlGenerator::generate() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
71
    }
72
}
73