Completed
Push — 4.0 ( ed2d0a...2bab8b )
by Marco
15:57
created

Collector::parse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
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 $cache      = null;
37
38
    private $classname  = "";
39
40
    private $type       = "";
41
42
    private $parameters = array();
43
44
    private $cache      = null;
0 ignored issues
show
Unused Code introduced by
The property $cache is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
45
46
    private $table      = array();
47
48
    public function __construct($routing_table, $configuration = null, $logger = null, $cache = null) {
49
50
        parent::__construct($configuration, $logger);
51
        
52
        $this->table = $routing_table->get();
53
54
        $this->setTimestamp();
55
56
    }
57
58
    public function bypass($mode = true) {
59
60
        $this->bypass = filter_var($mode, FILTER_VALIDATE_BOOLEAN);
61
62
        return $this;
63
64
    }
65
66
    public function route(Request $request) {
67
        
68
        $value = $this->parse($request);
69
        
70
        $this->query($request);
71
        
72
        $this->classname  = $value['class'];
73
        $this->type       = $value['type'];
74
        $this->parameters = array_merge($value['parameters'], $request->post()->get());
75
        
76
    }
77
    
78
    private function query(Request $request) {
79
        
80
        $keys = $request->uri()->query->keys();
81
        
82
        foreach ($keys as $key) {
83
            
84
            $request->post()->set(rawurldecode($key), $request->uri()->query->getValue($key));
85
            
86
        }
87
        
88
    }
89
    
90
    private function parse(Request $request) {
91
        
92
        $path = $request->uri()->getPath();
93
        
94
        foreach ($this->table as $regex => $value) {
95
            
96
            if (preg_match("/" . $regex . "/", $path, $matches)) {
97
                
98
                array_shift($matches);
99
                
100
                $this->setParameters($value['query'], $matches, $request);
101
                
102
                return $value;
103
                
104
            }
105
            
106
        }
107
        
108
    }
109
    
110
    private function setParameters($parameters, $values, Request $request) {
111
        
112
        $params = array_keys($parameters);
113
        
114
        $count  = 0;
115
        
116
        foreach ($values as $paramValue) {
117
            
118
            while ($count < count($params)) {
119
                
120
                $parameter   = $params[$count];
121
                
122
                $param_regex = $parameters[$parameter];
123
                
124
                $count++;
125
                
126
                if (preg_match("/" . $param_regex . "/", $paramValue)) {
127
                    
128
                    $request->post()->set($parameter, $paramValue);
129
                    
130
                    break;
131
                    
132
                }
133
                
134
            }
135
            
136
        }
137
        
138
    }
139
140
    public function compose(Response $response) {
141
142
    }
143
144
}
145