Map   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 26
dl 0
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 16 4
A get() 0 5 1
A url() 0 9 2
A post() 0 4 1
A __construct() 0 4 2
A add() 0 14 4
1
<?php
2
/**
3
 * KNUT7 K7F (https://marciozebedeu.com/)
4
 * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @link      https://github.com/knut7/framework/ for the canonical source repository
11
 * @copyright (c) 2015.  KNUT7  Software Technologies AO Inc. (https://marciozebedeu.com/)
12
 * @license   https://marciozebedeu.com/license/new-bsd New BSD License
13
 * @author    Marcio Zebedeu - [email protected]
14
 * @version   1.0.2
15
 */
16
17
namespace Ballybran\Helpers\Routing;
18
19
use Ballybran\Core\Http\RestUtilities;
20
use Ballybran\Exception\KException;
21
use Ballybran\Helpers\vardump\Vardump;
0 ignored issues
show
Bug introduced by
The type Ballybran\Helpers\vardump\Vardump was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
/**
24
 * Class Map
25
 * @package Ballybran\Helpers\Routing
26
 */
27
class Map
28
{
29
    private  $url;
30
    private  $callable;
0 ignored issues
show
introduced by
The private property $callable is not used, and could be removed.
Loading history...
31
    private  $routes = [];
32
    private  $nameRoute;
33
34
35
    public function __construct()
36
    {
37
        if (!empty($_SERVER['REQUEST_URI'])) {
38
            $this->url = trim($_SERVER['REQUEST_URI'], '/');
39
        }      
40
       
41
    }
42
43
    /**
44
     * @param $path
45
     * @param $callable
46
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
47
     * @return Route
48
     * example $router->add( '/user/:id', function($id) {}, 'name')->with('id','[0-9]+');
49
     */
50
    public function get($path, $callable, $name = null) : Routes
51
    {
52
53
        /** @var TYPE_NAME $callable */
54
        return self::add($path, $callable, $name, 'GET');
0 ignored issues
show
Bug Best Practice introduced by
The method Ballybran\Helpers\Routing\Map::add() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        return self::/** @scrutinizer ignore-call */ add($path, $callable, $name, 'GET');
Loading history...
Bug Best Practice introduced by
The expression return self::add($path, $callable, $name, 'GET') returns the type Ballybran\Helpers\Routing\Routes which is incompatible with the documented return type Ballybran\Helpers\Routing\Route.
Loading history...
55
56
    }
57
58
    /**
59
     * @param $path
60
     * @param $callable
61
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
62
     * @return Routes
63
     * example $router->post( '/user/:id', function() {}, 'name')
64
     */
65
    public  function post($path, $callable, $name = null) : Routes
66
    {
67
68
        return self::add($path, $callable, $name, 'POST');
0 ignored issues
show
Bug Best Practice introduced by
The method Ballybran\Helpers\Routing\Map::add() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        return self::/** @scrutinizer ignore-call */ add($path, $callable, $name, 'POST');
Loading history...
69
70
71
    }
72
73
    /**
74
     * @param $path
75
     * @param $callable
76
     * @param $name
77
     * @param $method
78
     * @return Routes
79
     * example $router->add( '/user/:id', function($id) {}, 'name')->with('id','[0-9]+');
80
     */
81
    public  function add($path, $callable, $name, $method) : Routes
82
    {
83
        $route = new Routes($path, $callable);
84
        $this->routes[$method][] = $route;
85
86
        if (is_string($callable) && $name === null) {
87
88
            $name = $callable;
89
90
        }
91
        if ($name) {
92
            $this->nameRoute[$name] = $route;
93
        }
94
        return $route;
95
96
    }
97
98
    public  function run()
99
    {
100
        if (!isset($this->routes[$_SERVER['REQUEST_METHOD']])) {
101
            throw new \Exception(' REQUEST_METHOD does not exists', 1);
102
103
        }
104
        foreach ($this->routes[$_SERVER['REQUEST_METHOD']] as $route) {
105
106
            if ($route->match($this->url)) {
107
108
                return $route->call();
109
            }
110
        }
111
112
            RestUtilities::sendResponse(200);
113
            KException::notFound();
114
115
    }
116
    /*
117
     * @param $name
118
     * @param $params array optional 
119
	 * $router->url('name', ['id'=> 1] )
120
	* example $router->get( '/user/:id', function($id) { return $router->url('name', ['id'=> 1] ) }, 'name')->with('id','[0-9]+');
121
	*/
122
123
    public function url($name, $params = [])
124
    {
125
        if (!isset($this->nameRoute[$name])) {
126
127
            throw new \Exception("No route match this name", 1);
128
129
        }
130
131
        return $this->nameRoute[$name]->getUrl($params);
132
    }
133
134
135
}