Completed
Pull Request — master (#165)
by Paul
03:21
created

AuraRouterWrapper::doMatch()   D

Complexity

Conditions 10
Paths 15

Size

Total Lines 42
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 42
rs 4.8196
cc 10
eloc 22
nc 15
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the PPI Framework.
4
 *
5
 * @copyright  Copyright (c) 2011-2016 Paul Dragoonis <[email protected]>
6
 * @license    http://opensource.org/licenses/mit-license.php MIT
7
 *
8
 * @link       http://www.ppi.io
9
 */
10
11
namespace PPI\Framework\Router\Wrapper;
12
13
use Aura\Router\Router as AuraRouter;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
16
use Symfony\Component\Routing\Exception\RouteNotFoundException;
17
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
18
use Symfony\Component\Routing\RequestContext;
19
use Symfony\Component\Routing\RouterInterface;
20
21
/**
22
 * A wrapper around the Aura Router component to make it compliant with the PPI(Symfony-CMF) ChainRouter.
23
 *
24
 * @author Paul Dragoonis <[email protected]>
25
 */
26
class AuraRouterWrapper implements RouterInterface, RequestMatcherInterface
27
{
28
    public function getRouter()
29
    {
30
        return self::class;
31
    }
32
33
    /**
34
     * @var AuraRouter
35
     */
36
    protected $router;
37
38
    /**
39
     * @var @todo typehint this
40
     */
41
    protected $context;
42
43
    /**
44
     * @param AuraRouter $router
45
     */
46
    public function __construct(AuraRouter $router)
47
    {
48
        $this->setRouter($router);
49
    }
50
51
    /**
52
     * @param AuraRouter $router
53
     */
54
    public function setRouter(AuraRouter $router)
55
    {
56
        $this->router = $router;
57
    }
58
59
    /**
60
     * @param RequestContext $context
61
     */
62
    public function setContext(RequestContext $context)
63
    {
64
        $this->context = $context;
65
    }
66
67
    /**
68
     * @return mixed
69
     */
70
    public function getContext()
71
    {
72
        return $this->context;
73
    }
74
75
    /**
76
     * @return \Aura\Router\RouteCollection
77
     */
78
    public function getRouteCollection()
79
    {
80
        // @todo - need to check the signature of these, with what's expected of Symfony's RouteCollection
81
        return $this->router->getRoutes();
82
    }
83
84
    /**
85
     * @param string      $name
86
     * @param array       $parameters
87
     * @param bool|string $referenceType
88
     *
89
     * @return false|string
90
     */
91
    public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
92
    {
93
        $ret = $this->router->generate($name, $parameters);
94
        if ($ret === false) {
95
            throw new RouteNotFoundException('Unable to generate route for: ' . $name);
96
        }
97
98
        return $ret;
99
    }
100
101
    /**
102
     * @param string $pathinfo
103
     *
104
     * @throws \Exception
105
     *
106
     * @return array
107
     */
108
    public function match($pathinfo)
109
    {
110
        return $this->doMatch($pathinfo);
111
    }
112
113
    /**
114
     * @param Request $request
115
     *
116
     * @throws \Exception
117
     *
118
     * @return array
119
     */
120
    public function matchRequest(Request $request)
121
    {
122
        return $this->doMatch($request->getPathInfo(), $request);
123
    }
124
125
    /**
126
     * @param $pathinfo
127
     * @param Request $request
128
     *
129
     * @throws \Exception
130
     *
131
     * @return array
132
     */
133
    protected function doMatch($pathinfo, Request $request = null)
134
    {
135
        $matchedRoute = $this->router->match($pathinfo, $request->server->all());
136
        if ($matchedRoute === false) {
137
            throw new ResourceNotFoundException();
138
        }
139
140
        $routeParams = $matchedRoute->params;
141
142
        // The 'action' key always exists and defaults to the Route Name, so we check accordingly
143
        if (!isset($routeParams['controller']) && $routeParams['action'] === $matchedRoute->name) {
144
            throw new \Exception('Matched the route: ' . $matchedRoute->name . ' but unable to locate
145
            any controller/action params to dispatch');
146
        }
147
148
        // We need _controller, to that symfony ControllerResolver can pick this up
149
        if (!isset($routeParams['_controller'])) {
150
            if (isset($routeParams['controller'])) {
151
                $routeParams['_controller'] = $routeParams['controller'];
152
            } elseif (isset($routeParams['action'])) {
153
                $routeParams['_controller'] = $routeParams['action'];
154
            } else {
155
                throw new \Exception('Unable to determine the controller from route: ' . $matchedRoute->name);
156
            }
157
        }
158
159
        $routeParams['_route'] = $matchedRoute->name;
160
161
        // If the controller is an Object, and 'action' is defaulted to the route name - we default to __invoke
162
        if ($routeParams['action'] === $matchedRoute->name) {
163
            $routeParams['action'] = '__invoke';
164
        }
165
166
        if (false === strpos($routeParams['_controller'], '::') && isset($routeParams['action'])) {
167
            $routeParams['_controller'] = sprintf('%s::%s',
168
                $routeParams['_controller'],
169
                $routeParams['action']
170
            );
171
        }
172
173
        return $routeParams;
174
    }
175
}
176