Completed
Push — 4.0 ( 2bab8b...b7ae43 )
by Marco
14:12
created

Collector::getClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
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) {
126
        
127
        $this->response = $response;
128
        
129
        $service = $this->getInstance();
130
        
131
        if (!is_null($service)) {
132
            
133
            $result = $service->run();
134
            
135
            $this->response->content()->set($result);
136
            
137
        } else {
138
            
139
            throw new DispatcherException(sprintf("Unable to execute service '%s'", $this->service));
140
            
141
        }
142
        
143
        
144
    }
145
    
146
    private function parse() {
147
        
148
        $path = $this->request->uri()->getPath();
149
        
150
        foreach ($this->table as $regex => $value) {
151
            
152
            if (preg_match("/" . $regex . "/", $path, $matches)) {
153
                
154
                $this->evalUri($value['query'], $matches);
155
                
156
                foreach ($value['parameters'] as $parameter => $value) {
157
                    
158
                    $this->request->query()->set($parameter, $value);
159
                    
160
                }
161
                
162
                $this->classname  = $value['class'];
163
                $this->type       = $value['type'];
164
                $this->service    = implode('.', $value['service']);
165
                
166
                return true;
167
                
168
            }
169
            
170
        }
171
        
172
        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...
173
        
174
    }
175
    
176
    private function evalUri($parameters, $bits) {
177
        
178
        $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...
179
        
180
        foreach ($parameters as $key => $value) {
181
            
182
            if (isset($bits[$key])) {
183
                
184
                if (preg_match('/' . $value['regex'] . '/', $bits[$key], $matches)) {
185
                    
186
                    if (count($matches) == 1) $matches = $matches[0];
187
                    
188
                    $this->request->query()->set($key, $matches);
189
                    
190
                }
191
                
192
            } elseif ($value['required']) {
193
                
194
                throw new DispatcherException(sprintf("Required parameter '%s' not specified.", $key));
195
                
196
            }
197
            
198
        }
199
        
200
    }
201
202
}
203