Completed
Push — master ( 70dcd2...ca2130 )
by Andy
02:07
created

CanonicalLinkExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 64
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0

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 10 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 2
    public function __construct(CanonicalUrlGenerator $canonicalUrlGenerator, RequestStack $requestStack)
21
    {
22 2
        $this->canonicalUrlGenerator = $canonicalUrlGenerator;
23 2
        $this->requestStack          = $requestStack;
24 2
    }
25
26
    /**
27
     * @return array
28
     */
29 1
    public function getFunctions()
30
    {
31
        return [
32 1
            new \Twig_Function('palmtree_canonical_url', [$this, 'generateUrl']),
33 1
            new \Twig_Function('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
     * @return string
60
     */
61 1
    public function generateUrl($route = null, $parameters = null)
62
    {
63 1
        if (!$route) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $route of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
64
            $request = $this->requestStack->getCurrentRequest();
65
66
            $route = $request->attributes->get('_route');
67
        }
68
69 1
        return $this->canonicalUrlGenerator->generateUrl($route, $parameters);
0 ignored issues
show
Bug introduced by
It seems like $parameters defined by parameter $parameters on line 61 can also be of type null; however, Palmtree\CanonicalUrlBun...enerator::generateUrl() does only seem to accept array|string, 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...
70
    }
71
}
72