Completed
Push — 4.0 ( 1533b9...4d1c3b )
by Marco
13:08
created

Collector   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 26
c 4
b 2
f 0
lcom 2
cbo 5
dl 0
loc 202
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getType() 0 5 1
A getService() 0 5 1
A getParameters() 0 5 1
A getClassName() 0 5 1
A getInstance() 0 17 2
A bypass() 0 7 1
A route() 0 12 2
B compose() 0 48 5
B parse() 0 30 5
B evalUri() 0 25 6
1
<?php namespace Comodojo\Dispatcher\Router;
2
3
use \Comodojo\Components\Model as DispatcherClassModel;
4
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait;
5
use \Comodojo\Dispatcher\Request\Model as Request;
6
use \Comodojo\Dispatcher\Response\Model as Response;
7
8
/**
9
 *
10
 * @package     Comodojo dispatcher
11
 * @author      Marco Giovinazzi <[email protected]>
12
 * @license     GPL-3.0+
13
 *
14
 * LICENSE:
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28
 */
29
30
class Collector extends DispatcherClassModel {
31
32
    use TimestampTrait;
33
34
    private $bypass     = false;
35
36
    private $classname  = "";
37
38
    private $type       = "";
39
40
    private $service    = "";
41
42
    private $cache      = null;
43
44
    private $request    = null;
45
46
    private $response   = null;
47
48
    private $table      = array();
49
50
    public function __construct($routing_table, $configuration = null, $logger = null, $cache = null) {
51
52
        parent::__construct($configuration, $logger);
53
        
54
        $this->table = $routing_table->get();
55
        
56
        $this->cache = $cache;
57
58
        $this->setTimestamp();
59
60
    }
61
    
62
    public function getType() {
63
        
64
        return $this->type;
65
        
66
    }
67
    
68
    public function getService() {
69
        
70
        return $this->service;
71
        
72
    }
73
    
74
    public function getParameters() {
75
        
76
        return $this->parameters;
77
        
78
    }
79
    
80
    public function getClassName() {
81
        
82
        return $this->classname;
83
        
84
    }
85
    
86
    public function getInstance() {
87
        
88
        $class = $this->classname;
89
        
90
        if (class_exists($class)) {
91
            
92
            return new $class(
93
                $this->service, 
94
                $this->logger,
95
                $this,
96
                $this->response
97
            );
98
            
99
        }
100
        else return null;
101
        
102
    }
103
104
    public function bypass($mode = true) {
105
106
        $this->bypass = filter_var($mode, FILTER_VALIDATE_BOOLEAN);
107
108
        return $this;
109
110
    }
111
112
    public function route(Request $request) {
113
        
114
        $this->request = $request;
115
        
116
        if (!$this->parse()) {
117
            
118
            throw new DispatcherException("Unable to find a valid route for the specified uri");
119
            
120
        }
121
        
122
        
123
    }
124
125
    public function compose(Response $response) {
0 ignored issues
show
Coding Style introduced by
compose 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...
126
        
127
        $this->response = $response;
128
        
129
        $service = $this->getInstance();
130
        
131
        if (!is_null($service)) {
132
            
133
            $result = "";
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
134
135
            switch( $_SERVER['REQUEST_METHOD'] ) {
136
    
137
                case 'POST':
138
    
139
                    $result = $service->post();
140
    
141
                    break;
142
    
143
                case 'PUT':
144
    
145
                    $result = $service->put();
146
    
147
                    break;
148
                    
149
                case 'DELETE':
150
    
151
                    $result = $service->delete();
152
    
153
                    break;
154
    
155
                default:
156
    
157
                    $result = $service->get();
158
    
159
                    break;
160
    
161
            }
162
            
163
            $this->response->content()->set($result);
164
            
165
        } else {
166
            
167
            throw new DispatcherException(sprintf("Unable to execute service '%s'", $this->service));
168
            
169
        }
170
        
171
        
172
    }
173
    
174
    private function parse() {
175
        
176
        $path = $this->request->uri()->getPath();
177
        
178
        foreach ($this->table as $regex => $value) {
179
            
180
            if (preg_match("/" . $regex . "/", $path, $matches)) {
181
                
182
                $this->evalUri($value['query'], $matches);
183
                
184
                foreach ($value['parameters'] as $parameter => $value) {
185
                    
186
                    $this->request->query()->set($parameter, $value);
187
                    
188
                }
189
                
190
                $this->classname  = $value['class'];
191
                $this->type       = $value['type'];
192
                $this->service    = implode('.', $value['service']);
193
                $this->service    = empty($this->service)?"default":$this->service;
194
                
195
                return true;
196
                
197
            }
198
            
199
        }
200
        
201
        return $false;
0 ignored issues
show
Bug introduced by
The variable $false does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
202
        
203
    }
204
    
205
    private function evalUri($parameters, $bits) {
206
        
207
        $count  = 0;
0 ignored issues
show
Unused Code introduced by
$count is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
208
        
209
        foreach ($parameters as $key => $value) {
210
            
211
            if (isset($bits[$key])) {
212
                
213
                if (preg_match('/^' . $value['regex'] . '$/', $bits[$key], $matches)) {
214
                    
215
                    if (count($matches) == 1) $matches = $matches[0];
216
                    
217
                    $this->request->query()->set($key, $matches);
218
                    
219
                }
220
                
221
            } elseif ($value['required']) {
222
                
223
                throw new DispatcherException(sprintf("Required parameter '%s' not specified.", $key));
224
                
225
            }
226
            
227
        }
228
        
229
    }
230
231
}
232