Router   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPlugin() 0 4 1
A getController() 0 4 1
A getAction() 0 4 1
A getPath() 0 14 4
A dispatch() 0 17 2
A sanitize() 0 6 3
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Jitamin\Foundation\Http;
13
14
use Jitamin\Foundation\Base;
15
16
/**
17
 * Route Dispatcher.
18
 */
19
class Router extends Base
20
{
21
    const DEFAULT_CONTROLLER = 'Dashboard/DashboardController';
22
    const DEFAULT_METHOD = 'index';
23
24
    /**
25
     * Plugin name.
26
     *
27
     * @var string
28
     */
29
    private $currentPluginName = '';
30
31
    /**
32
     * Controller.
33
     *
34
     * @var string
35
     */
36
    private $currentControllerName = '';
37
38
    /**
39
     * Action.
40
     *
41
     * @var string
42
     */
43
    private $currentActionName = '';
44
45
    /**
46
     * Get plugin name.
47
     *
48
     * @return string
49
     */
50
    public function getPlugin()
51
    {
52
        return $this->currentPluginName;
53
    }
54
55
    /**
56
     * Get controller.
57
     *
58
     * @return string
59
     */
60
    public function getController()
61
    {
62
        return $this->currentControllerName;
63
    }
64
65
    /**
66
     * Get action.
67
     *
68
     * @return string
69
     */
70
    public function getAction()
71
    {
72
        return $this->currentActionName;
73
    }
74
75
    /**
76
     * Get the path to compare patterns.
77
     *
78
     * @return string
79
     */
80
    public function getPath()
81
    {
82
        $path = substr($this->request->getUri(), strlen($this->helper->url->dir()));
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\Router>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property helper does not exist on object<Jitamin\Foundation\Http\Router>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
83
84
        if ($this->request->getQueryString() !== '') {
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\Router>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
85
            $path = substr($path, 0, -strlen($this->request->getQueryString()) - 1);
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\Router>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
86
        }
87
88
        if ($path !== '' && $path[0] === '/') {
89
            $path = substr($path, 1);
90
        }
91
92
        return $path;
93
    }
94
95
    /**
96
     * Find controller/action from the route table or from get arguments.
97
     */
98
    public function dispatch()
99
    {
100
        $controller = urldecode($this->request->getStringParam('controller'));
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\Router>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
        $action = $this->request->getStringParam('action');
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\Router>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102
        $plugin = $this->request->getStringParam('plugin');
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Foundation\Http\Router>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
103
104
        if ($controller === '') {
105
            $route = $this->route->findRoute($this->getPath());
0 ignored issues
show
Documentation introduced by
The property route does not exist on object<Jitamin\Foundation\Http\Router>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106
            $controller = $route['controller'];
107
            $action = $route['action'];
108
            $plugin = $route['plugin'];
109
        }
110
111
        $this->currentControllerName = ucfirst($this->sanitize($controller, self::DEFAULT_CONTROLLER, true));
112
        $this->currentActionName = $this->sanitize($action, self::DEFAULT_METHOD);
113
        $this->currentPluginName = ucfirst($this->sanitize($plugin));
114
    }
115
116
    /**
117
     * Check controller and action parameter.
118
     *
119
     * @param string $value
120
     * @param string $default
121
     *
122
     * @return string
123
     */
124
    protected function sanitize($value, $default = '', $is_controller = false)
125
    {
126
        $pattern = $is_controller ? '/^[a-zA-Z_0-9\/]+$/' : '/^[a-zA-Z_0-9]+$/';
127
128
        return preg_match($pattern, $value) ? $value : $default;
129
    }
130
}
131