Completed
Push — master ( bb1ab9...4cd17e )
by Sinnarasa
02:25
created

Route::setResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 7
rs 9.4285
cc 3
eloc 5
nc 2
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Route::addDetail() 0 4 1
1
<?php
2
3
namespace JetFire\Routing;
4
5
/**
6
 * Class Route
7
 * @package JetFire\Routing
8
 * @method getParameters()
9
 * @method getBlock()
10
 * @method getPath()
11
 */
12
class Route
13
{
14
15
    /**
16
     * @var
17
     */
18
    private $url;
19
    /**
20
     * @var
21
     */
22
    private $name;
23
    /**
24
     * @var
25
     */
26
    private $callback;
27
    /**
28
     * @var string
29
     */
30
    private $method;
31
    /**
32
     * @var array
33
     */
34
    private $target = [];
35
    /**
36
     * @var array
37
     */
38
    private $detail = [];
39
40
    /**
41
     */
42
    public function __construct()
0 ignored issues
show
Coding Style introduced by
__construct 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...
Coding Style introduced by
__construct 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...
43
    {
44
        $this->method = (
45
            isset($_POST['_METHOD'])
46
            && in_array($_POST['_METHOD'], array('PUT', 'DELETE'))
47
        ) ? $_POST['_METHOD'] : $_SERVER['REQUEST_METHOD'];
48
    }
49
50
    /**
51
     * @param array $args
52
     */
53
    public function set($args = [])
54
    {
55
        if (isset($args['name'])) $this->name = $args['name'];
56
        if (isset($args['callback'])) $this->callback = $args['callback'];
57
        if (isset($args['target'])) $this->target = $args['target'];
58
        if (isset($args['detail'])) $this->detail = $args['detail'];
59
    }
60
61
    /**
62
     * @return null
63
     */
64
    public function getUrl()
65
    {
66
        return $this->url;
67
    }
68
69
    /**
70
     * @param $url
71
     */
72
    public function setUrl($url)
73
    {
74
        $this->url = $url;
75
    }
76
77
    /**
78
     * @return null
79
     */
80
    public function getName()
81
    {
82
        return $this->name;
83
    }
84
85
    /**
86
     * @param $name
87
     */
88
    public function setName($name)
89
    {
90
        $this->name = $name;
91
    }
92
93
    /**
94
     * @return mixed
95
     */
96
    public function getCallback()
97
    {
98
        return $this->callback;
99
    }
100
101
    /**
102
     * @param $callback
103
     */
104
    public function setCallback($callback)
105
    {
106
        $this->callback = $callback;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function getMethod()
113
    {
114
        return $this->method;
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function getDetail()
121
    {
122
        return $this->detail;
123
    }
124
125
    /**
126
     * @param $detail
127
     */
128
    public function setDetail($detail)
129
    {
130
        $this->detail = array_merge($detail,$this->detail);
131
    }
132
133
    /**
134
     * @param $key
135
     * @param $value
136
     */
137
    public function addDetail($key, $value)
138
    {
139
        $this->detail[$key] = $value;
140
    }
141
142
    /**
143
     * @param null $key
144
     * @return array|string
145
     */
146
    public function getTarget($key = null)
147
    {
148
        if (!is_null($key))
149
            return isset($this->target[$key]) ? $this->target[$key] : '';
150
        return empty($this->target) ? '' : $this->target;
151
    }
152
153
    /**
154
     * @param $target
155
     * @return mixed
156
     */
157
    public function setTarget($target = [])
158
    {
159
        $this->target = $target;
160
    }
161
162
    /**
163
     * @param null $key
164
     * @return bool
165
     */
166
    public function hasTarget($key = null)
167
    {
168
        if (!is_null($key))
169
            return isset($this->target[$key]) ? true : false;
170
        return empty($this->target) ? false : true;
171
    }
172
173
    /**
174
     * @return array
175
     */
176
    public function getData(){
177
        return (isset($this->getDetail()['data']) && is_array($this->getDetail()['data']))?$this->getDetail()['data']:[];
178
    }
179
180
    /**
181
     * @param $name
182
     * @param $arguments
183
     * @return null
184
     */
185
    public function __call($name, $arguments)
186
    {
187
        if (substr($name, 0, 3) === "get") {
188
            $key = strtolower(str_replace('get', '', $name));
189
            return isset($this->detail[$key]) ? $this->detail[$key] : '';
190
        } elseif (substr($name, 0, 3) === "set") {
191
            $key = strtolower(str_replace('set', '', $name));
192
            $this->detail[$key] = $arguments[0];
193
        }
194
        return '';
195
    }
196
}
197