Completed
Push — 4.0 ( ad8a54...ed771a )
by Marco
06:33
created

Model::route()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Comodojo\Dispatcher\Request;
2
3
use \Comodojo\Dispatcher\Components\Model as DispatcherClassModel;
4
use \Comodojo\Dispatcher\Request\Headers;
5
use \Comodojo\Dispatcher\Request\Post;
6
use \Comodojo\Dispatcher\Request\Query;
7
use \Comodojo\Dispatcher\Request\UserAgent;
8
use \Comodojo\Dispatcher\Request\Method;
9
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait;
10
use \Comodojo\Dispatcher\Components\Configuration;
11
use \League\Uri\Schemes\Http as HttpUri;
12
use \Psr\Log\LoggerInterface;
13
14
/**
15
 * @package     Comodojo Dispatcher
16
 * @author      Marco Giovinazzi <[email protected]>
17
 * @author      Marco Castiello <[email protected]>
18
 * @license     GPL-3.0+
19
 *
20
 * LICENSE:
21
 *
22
 * This program is free software: you can redistribute it and/or modify
23
 * it under the terms of the GNU Affero General Public License as
24
 * published by the Free Software Foundation, either version 3 of the
25
 * License, or (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU Affero General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU Affero General Public License
33
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34
 */
35
36
class Model extends DispatcherClassModel {
37
38
    use TimestampTrait;
39
40
    private $headers;
41
42
    private $uri;
43
44
    private $useragent;
45
46
    private $post;
47
48
    private $query;
49
50
    private $method;
51
52
    private $version;
53
54 3
    public function __construct(Configuration $configuration, LoggerInterface $logger) {
0 ignored issues
show
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...
55
56 3
        parent::__construct($configuration, $logger);
57
58 3
        $this->setTimestamp($_SERVER['REQUEST_TIME_FLOAT']);
59
60 3
        $this->headers = new Headers();
61
62 3
        $this->uri = HttpUri::createFromServer($_SERVER);
63
64 3
        $this->post = new Post();
65
66 3
        $this->query = new Query();
67
68 3
        $this->useragent = new UserAgent();
69
70 3
        $this->method = new Method();
71
72 3
        $this->version = new Version();
73
74 3
    }
75
76
    public function headers() {
77
78
        return $this->headers;
79
80
    }
81
82 1
    public function uri() {
83
84 1
        return $this->uri;
85
86
    }
87
88
    public function post() {
89
90
        return $this->post;
91
92
    }
93
94
    public function query() {
95
96
        return $this->query;
97
98
    }
99
100
    public function useragent() {
101
102
        return $this->useragent;
103
104
    }
105
106 2
    public function method() {
107
108 2
        return $this->method;
109
    }
110
111 1
    public function version() {
112
113 1
        return $this->version;
114
115
    }
116
117 1
    public function route() {
118
119 1
        return str_replace($this->configuration()->get("base-uri"), "", $this->uri->getPath());
120
    }
121
122
}
123