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

DefaultConfiguration   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
c 2
b 1
f 0
lcom 1
cbo 0
dl 0
loc 41
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 13 1
A urlGetAbsolute() 0 9 3
A uriGetAbsolute() 0 5 1
1
<?php namespace Comodojo\Dispatcher\Components;
2
3
/**
4
 * @package     Comodojo Dispatcher
5
 * @author      Marco Giovinazzi <[email protected]>
6
 * @author      Marco Castiello <[email protected]>
7
 * @license     GPL-3.0+
8
 *
9
 * LICENSE:
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
26
class DefaultConfiguration {
27
28
    private static $configuration = array(
29
        'enabled' => true,
30
        'encoding' => 'UTF-8',
31
        'disabled-status' => 503,
32
        'disabled-message' => 'Dispatcher offline',
33
        'supported-methods' => array('GET','PUT','POST','DELETE','OPTIONS','HEAD')
34
    );
35
36
    public static function get() {
37
38
        $config = self::$configuration;
39
40
        $config['base-path'] = getcwd();
41
42
        $config['base-url'] = self::urlGetAbsolute();
43
44
        $config['base-uri'] = self::uriGetAbsolute();
45
46
        return $config;
47
48
    }
49
50
    private static function urlGetAbsolute() {
0 ignored issues
show
Coding Style introduced by
urlGetAbsolute 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...
51
52
        $http = 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '') . '://';
53
54
        $uri = self::uriGetAbsolute();
55
56
        return ( $http . $_SERVER['HTTP_HOST'] . $uri . "/" );
57
58
    }
59
60
    private static function uriGetAbsolute() {
0 ignored issues
show
Coding Style introduced by
uriGetAbsolute 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...
61
62
        return preg_replace("/\/index.php(.*?)$/i", "", $_SERVER['PHP_SELF']);
63
64
    }
65
66
}
67