Completed
Push — master ( 645ffa...19a002 )
by Anderson
02:01
created

Request   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 137
rs 10
wmc 16
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C deterimeRequestMethod() 0 25 7
B run() 0 61 8
1
<?php
2
3
/**
4
 * Luthier Request middleware (internal)
5
 *
6
 * @author    Anderson Salas <[email protected]>
7
 * @copyright 2017
8
 * @license   GNU-3.0
9
 *
10
 */
11
12
namespace Luthier\Middleware;
13
14
use Luthier\Core\Route as Route;
15
16
class Request extends \Luthier\Core\Middleware
17
{
18
19
    /**
20
     * Current (improved) route
21
     *
22
     * @var $route
23
     *
24
     * @access protected
25
     */
26
    protected $route;
27
28
    /**
29
     * Infered request method
30
     *
31
     * @var $requestMethod
32
     *
33
     * @access protected
34
     */
35
    protected $requestMethod;
36
37
    /**
38
     * Class constructor
39
     *
40
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
41
     *
42
     * @access public
43
     */
44
    public function __construct()
45
    {
46
        parent::__construct();
47
        $this->deterimeRequestMethod();
48
        $this->route = Route::getRouteByPath(self::$uri_string, $this->requestMethod);
49
    }
50
51
    /**
52
     * Determines the actual request method
53
     *
54
     * @return void
55
     *
56
     * @access private
57
     */
58
    private function deterimeRequestMethod()
0 ignored issues
show
Coding Style introduced by
deterimeRequestMethod uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
deterimeRequestMethod uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
59
    {
60
61
        $requestMethod = $_SERVER['REQUEST_METHOD'];
62
        $formMethod    = NULL;
63
        $validMethods  = Route::getHTTPVerbs();
64
65
        // FIXME: Solve ambiguity here! POST with _method="GET" makes no sense
66
67
        if (isset($_POST['_method']) && in_array(strtoupper($_POST['_method']), $validMethods, TRUE))
68
            $formMethod = strtoupper($_POST['_method']);
69
70
        if (is_null($formMethod))
71
        {
72
            $this->requestMethod = $requestMethod;
73
        }
74
        else
75
        {
76
            if ($requestMethod == 'POST')
77
                $this->requestMethod = $formMethod;
78
79
            if (!$this->CI->input->is_ajax_request() && $this->requestMethod == 'HEAD')
0 ignored issues
show
Bug introduced by
The property input does not seem to exist in Luthier\Core\Middleware.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
80
                $this->requestMethod = 'POST';
81
        }
82
    }
83
84
    /**
85
     * Entry point of the middleware
86
     *
87
     * @return void
88
     *
89
     * @access public
90
     */
91
    public function run()
92
    {
93
        if (!$this->route)
94
        {
95
            //if(is_null(Route::get404()))
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
            //    show_404();
97
98
            if (Route::get404()->controller != get_class($this->CI))
0 ignored issues
show
Bug introduced by
The property controller does not seem to exist. Did you mean defaultController?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
99
            {
100
                if (ENVIRONMENT != 'production')
101
                {
102
                    show_error('The request method '.$this->requestMethod.' is not allowed to view the resource', 403, 'Forbidden method');
103
                } else
104
                {
105
                    //redirect(Route::get404()->path);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
106
                    Route::trigger404();
107
                }
108
            }
109
        } else
110
        {
111
            if (method_exists($this->CI, $this->route->method))
112
            {
113
                $path_args  = Route::getRouteArgs($this->route, self::$uri_string);
0 ignored issues
show
Documentation introduced by
self::$uri_string is of type object<Luthier\Core\Middleware>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
                $route_args = Route::compileRoute($this->route)->args;
115
116
117
118
                // Redirect to 404 if not enough parameters provided
119
120
                if(count($path_args) < count($route_args['required']))
121
                    redirect(Route::get404()->path);
122
123
                if(count($path_args) == 0)
124
                {
125
                    $this->CI->{$this->route->method}();
126
                }
127
                else
128
                {
129
                    call_user_func_array( [$this->CI, $this->route->method], array_values($path_args) );
130
                }
131
132
133
                // TODO: Add support to hooks in this execution thread
134
135
                $this->CI->output->_display();
0 ignored issues
show
Bug introduced by
The property output does not seem to exist in Luthier\Core\Middleware.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
136
                exit(0);
0 ignored issues
show
Coding Style Compatibility introduced by
The method run() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
137
            }
138
            else
139
            {
140
                if (ENVIRONMENT != 'production')
141
                {
142
                    show_error('The method '.$this->route->controller.'::'.$this->route->method.'() does not exists', 500, 'Method not found');
143
                }
144
                else
145
                {
146
                    //redirect(Route::get404()->path);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
147
                    Route::trigger404();
148
                }
149
            }
150
        }
151
    }
152
}