Completed
Push — master ( 317699...2816c1 )
by Vítor
02:40
created

RoutesController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 12.5%

Importance

Changes 5
Bugs 0 Features 5
Metric Value
wmc 10
c 5
b 0
f 5
lcom 1
cbo 7
dl 0
loc 79
ccs 4
cts 32
cp 0.125
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A indexAction() 0 4 1
A listAllAction() 0 10 1
A renderMatchRouteViewAction() 0 8 1
B matchRouteAction() 0 18 6
1
<?php
2
/**
3
 * ZfDebugModule. Console commands and other utilities for debugging ZF2 apps.
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
6
 * @copyright 2016 Vítor Brandão <[email protected]>
7
 */
8
9
namespace Noiselabs\ZfDebugModule\Controller\Http;
10
11
use Noiselabs\ZfDebugModule\Module;
12
use Noiselabs\ZfDebugModule\Package;
13
use Noiselabs\ZfDebugModule\Util\Routing\RouteCollection;
14
use Noiselabs\ZfDebugModule\Util\Routing\RouteMatcher;
15
use Zend\Http\Request;
16
use Zend\Http\Response;
17
use Zend\Mvc\Controller\AbstractActionController;
18
use Zend\View\Model\JsonModel;
19
use Zend\View\Model\ViewModel;
20
21
class RoutesController extends AbstractActionController
22
{
23
    /**
24
     * @var RouteCollection
25
     */
26
    private $routeCollection;
27
    /**
28
     * @var RouteMatcher
29
     */
30
    private $routeMatcher;
31
32
    /**
33
     * RoutesController constructor.
34
     *
35
     * @param RouteCollection $routeCollection
36
     * @param RouteMatcher $routeMatcher
37
     */
38 1
    public function __construct(RouteCollection $routeCollection, RouteMatcher $routeMatcher)
39
    {
40 1
        $this->routeCollection = $routeCollection;
41 1
        $this->routeMatcher = $routeMatcher;
42 1
    }
43
44
    /**
45
     * @return Response
46
     */
47
    public function indexAction()
48
    {
49
        return $this->redirect()->toRoute(Package::NAME . '/routes/list');
50
    }
51
52
    /**
53
     * @return ViewModel
54
     */
55
    public function listAllAction()
56
    {
57
        $this->layout(Module::DEFAULT_LAYOUT);
58
        $view = new ViewModel([
59
            'routeCollection' => $this->routeCollection,
60
        ]);
61
        $view->setTemplate(Package::FQPN . '/routes/list-all');
62
63
        return $view;
64
    }
65
66
    /**
67
     * @return ViewModel
68
     */
69
    public function renderMatchRouteViewAction()
70
    {
71
        $this->layout(Module::DEFAULT_LAYOUT);
72
        $view = new ViewModel();
73
        $view->setTemplate(Package::FQPN . '/routes/match');
74
75
        return $view;
76
    }
77
78
    /**
79
     * @return JsonModel
80
     */
81
    public function matchRouteAction()
82
    {
83
        $data = $this->params()->fromQuery();
84
        $route = null;
85
        if (isset($data['method']) && !empty($data['method']) && isset($data['url']) && !empty($data['url'])) {
86
            if (null !== ($routeName = $this->routeMatcher->match($data['method'], $data['url']))) {
87
                $route = $this->routeCollection->getRoute($routeName);
88
                $route = [
89
                    'name' => $route->getName(),
90
                    'url' => $route->getUrl(),
91
                    'controller' => $route->getController(),
92
                    'action' => $route->getAction(),
93
                ];
94
            }
95
        }
96
97
        return new JsonModel(['requestedRouteData' => $data, 'routeMatch' => $route]);
98
    }
99
}
100