Routing::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace DoS\ResourceBundle\Twig\Extension;
4
5
use Cocur\Slugify\SlugifyInterface;
6
use Symfony\Component\PropertyAccess\PropertyAccess;
7
use Symfony\Component\Routing\RouterInterface;
8
9
/**
10
 * @author liverbool <[email protected]>
11
 */
12
class Routing extends \Twig_Extension
13
{
14
    /**
15
     * @var RouterInterface
16
     */
17
    protected $router;
18
19
    /**
20
     * @var SlugifyInterface
21
     */
22
    protected $slugify;
23
24
    public function __construct(RouterInterface $router, SlugifyInterface $slugify)
25
    {
26
        $this->router = $router;
27
        $this->slugify = $slugify;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getFunctions()
34
    {
35
        return array(
36
            new \Twig_SimpleFunction('ui_resource_route', array($this, 'generateObjectUrl')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: 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...
37
            new \Twig_SimpleFunction('ui_route', array($this, 'generateObjectUrl')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: 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...
38
        );
39
    }
40
41
    /**
42
     * @param mixed $object
43
     * @param string $routeName #Route
44
     * @param string $separator
45
     * @param array $query
46
     *
47
     * @return string
48
     */
49
    public function generateObjectUrl($object, $routeName, $separator = '_', array $query = array())
50
    {
51
        $parameters = array();
52
        $accessor = PropertyAccess::createPropertyAccessor();
53
        $pattern = $this->router->getRouteCollection()->get($routeName)->getPath();
54
55
        if (preg_match_all('/\{([a-z]+)\}/', $pattern, $matches)) {
56
            foreach($matches[1] as $holder) {
57
                $value = $accessor->getValue($object, $holder);
58
59
                if (!is_numeric($value)) {
60
                    $value = $this->slugify->slugify($value, $separator);
61
                }
62
63
                $parameters[$holder] = $value;
64
            }
65
        }
66
67
        return $this->router->generate($routeName, $parameters, $query);
0 ignored issues
show
Documentation introduced by
$query is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getName()
74
    {
75
        return 'ui_routing';
76
    }
77
}
78