Completed
Push — 4.0 ( 2ca4e1...0c3d7c )
by Marco
15:24
created

Headers::getHeaders()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 19
rs 9.2
cc 4
eloc 7
nc 4
nop 0
1
<?php namespace Comodojo\Dispatcher\Request;
2
3
use \Comodojo\Dispatcher\Components\Headers as HeadersTrait;
4
5
/**
6
 * @package     Comodojo Dispatcher
7
 * @author      Marco Giovinazzi <[email protected]>
8
 * @author      Marco Castiello <[email protected]>
9
 * @license     GPL-3.0+
10
 *
11
 * LICENSE:
12
 *
13
 * This program is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License as
15
 * published by the Free Software Foundation, either version 3 of the
16
 * License, or (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License
24
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25
 */
26
27
class Headers {
28
29
    use HeadersTrait;
30
31
    public function __construct() {
32
33
        $this->headers = self::getHeaders();
34
35
    }
36
37
    /**
38
     * Get request headers both in apache and nginx
39
     *
40
     * @return array
41
     */
42
    private static function getHeaders() {
0 ignored issues
show
Coding Style introduced by
getHeaders 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...
43
44
        if ( function_exists('getallheaders') ) return getallheaders();
45
46
        $headers = array();
47
48
        foreach ( $_SERVER as $name => $value ) {
49
50
            if ( substr($name, 0, 5) == 'HTTP_' ) {
51
52
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
53
54
            }
55
56
        }
57
58
        return $headers;
59
60
    }
61
62
}
63