Completed
Push — 4.0 ( 0c3d7c...75a76e )
by Marco
12:24
created

Model::route()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
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 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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 \Monolog\Logger;
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
    public function __construct(Configuration $configuration, Logger $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...
53
54
        parent::__construct($configuration, $logger);
55
56
        $this->setTimestamp($_SERVER['REQUEST_TIME_FLOAT']);
57
58
        $this->headers = new Headers();
59
60
        $this->uri = HttpUri::createFromServer($_SERVER);
61
62
        $this->post = new Post();
63
64
        $this->query = new Query();
65
66
        $this->useragent = new UserAgent();
67
68
        $this->method = new Method();
69
70
    }
71
72
    public function headers() {
73
74
        return $this->headers;
75
76
    }
77
78
    public function uri() {
79
80
        return $this->uri;
81
82
    }
83
84
    public function post() {
85
86
        return $this->post;
87
88
    }
89
90
    public function query() {
91
92
        return $this->query;
93
94
    }
95
96
    public function useragent() {
97
98
        return $this->useragent;
99
100
    }
101
102
    public function method() {
103
104
        return $this->method;
105
    }
106
107
    public function route() {
108
109
        return str_replace($this->configuration()->get("base-uri"), "", $this->uri->getPath());
110
    }
111
112
}
113