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

CanonicalLinkExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 5
c 9
b 0
f 0
lcom 1
cbo 5
dl 0
loc 65
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getFunctions() 0 10 1
A renderLinkTag() 0 8 1
A generateUrl() 0 9 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