ObjectRouter::generate()   C
last analyzed

Complexity

Conditions 8
Paths 48

Size

Total Lines 36
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 36
ccs 18
cts 18
cp 1
rs 5.3846
cc 8
eloc 18
nc 48
nop 3
crap 8
1
<?php
2
3
namespace Zenstruck\ObjectRoutingBundle\Routing;
4
5
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
6
use Symfony\Component\Routing\RequestContext;
7
use Symfony\Component\Routing\RouterInterface;
8
use Zenstruck\ObjectRoutingBundle\ObjectTransformer\ObjectTransformer;
9
10
/**
11
 * @author Kevin Bond <[email protected]>
12
 */
13
class ObjectRouter implements RouterInterface, WarmableInterface
14
{
15
    private $router;
16
    private $transformers;
17
18
    /**
19
     * @param RouterInterface     $router
20
     * @param ObjectTransformer[] $transformers
21
     */
22 16
    public function __construct(RouterInterface $router, array $transformers = array())
23
    {
24 16
        $this->router = $router;
25 16
        $this->transformers = $transformers;
26 16
    }
27
28
    /**
29
     * @param ObjectTransformer $transformer
30
     */
31 10
    public function addTransformer(ObjectTransformer $transformer)
32
    {
33 10
        $this->transformers[] = $transformer;
34 10
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 1
    public function setContext(RequestContext $context)
40
    {
41 1
        $this->router->setContext($context);
42 1
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 10
    public function getContext()
48
    {
49 10
        return $this->router->getContext();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function getRouteCollection()
56
    {
57 1
        return $this->router->getRouteCollection();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 16
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
64
    {
65 16
        $routeName = is_object($name) ? null : $name;
66 16
        $object = $this->getObject($name, $parameters);
67
68
        // ensure parameters is array
69 16
        if (is_object($parameters)) {
70 7
            $parameters = array();
71
        }
72
73
        // allow 3rd arg to be extra parameters array
74 16
        if (is_array($referenceType)) {
75 6
            $parameters = $referenceType;
76 6
            $referenceType = self::ABSOLUTE_PATH;
77
        }
78
79
        // check for reference type as 4th arg
80 16
        if (3 < count($args = func_get_args())) {
81 2
            $referenceType = $args[3];
82
        }
83
84
        // check if first argument is an object
85 16
        if ($object && ($routeContext = $this->transform($object, $routeName))) {
86 13
            $name = $routeContext->getName();
87 13
            $parameters = array_merge($routeContext->getParameters(), $parameters);
88 13
            $uri = $this->router->generate($name, $parameters, $referenceType);
89
90 13
            if ($fragment = $routeContext->getFragment()) {
91 1
                $uri .= '#'.$fragment;
92
            }
93
94 13
            return $uri;
95
        }
96
97 3
        return $this->router->generate($name, $parameters, $referenceType);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 10
    public function match($pathinfo)
104
    {
105 10
        return $this->router->match($pathinfo);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function warmUp($cacheDir)
112
    {
113 1
        if ($this->router instanceof WarmableInterface) {
114 1
            $this->router->warmUp($cacheDir);
115
        }
116 1
    }
117
118
    /**
119
     * @param object      $object
120
     * @param null|string $routeName
121
     *
122
     * @return null|\Zenstruck\ObjectRoutingBundle\RouteContext
123
     */
124 14
    private function transform($object, $routeName = null)
125
    {
126 14
        foreach ($this->transformers as $transformer) {
127 13
            if (!$transformer->supports($object, $routeName)) {
128 8
                continue;
129
            }
130
131 13
            return $transformer->transform($object, $routeName);
132
        }
133
134 1
        return null;
135
    }
136
137
    /**
138
     * @param object|string $name
139
     * @param array|object  $parameters
140
     *
141
     * @return null|object
142
     */
143 16
    private function getObject($name, $parameters)
144
    {
145 16
        if (is_object($name)) {
146 7
            return $name;
147
        }
148
149 9
        if (is_object($parameters)) {
150 7
            return $parameters;
151
        }
152
153 2
        return null;
154
    }
155
}
156