Config   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 28.95%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 97
ccs 11
cts 38
cp 0.2895
rs 10
1
<?php
2
3
namespace App;
4
5
class Config
6
{
7
8
    protected static $config = false;
9
10 11
    public function __construct()
11
    {
12 11
        if (!is_file(CONFIG_FILE)) {
13
            throw new \Exception("Config file not found: '" . CONFIG_FILE . "'");
14
        }
15 11
        if (self::$config === false) {
16 1
            self::$config = require CONFIG_FILE;
17
        }
18 11
    }
19
20 11
    protected function get($name, $default = null)
21
    {
22 11
        if (isset(self::$config[$name])) {
23 11
            return self::$config[$name];
24
        }
25
        return $default;
26
    }
27
28
    public function debug()
29
    {
30
        return (bool) $this->get('debug', false);
31
    }
32
33
    public function mode()
34
    {
35
        return $this->get('mode', 'production');
36
    }
37
38
    public function mail()
39
    {
40
        return $this->get('mail', ['transport' => 'mail']);
41
    }
42
43 11
    public function filesystems()
44
    {
45
        $default = [
46 11
            'cache' => [
47
                'type' => 'local',
48
                'root' => __DIR__ . '/../data/cache',
49
            ],
50
            'storage' => [
51
                'type' => 'local',
52
                'root' => __DIR__ . '/../data/storage',
53
            ],
54
        ];
55 11
        return $this->get('filesystems', $default);
56
    }
57
58
    /**
59
     * Get the Base URL of the application. Never has a trailing slash.
60
     * @return string
61
     */
62
    public function baseUrl($absolute = false)
0 ignored issues
show
Coding Style introduced by
baseUrl 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...
63
    {
64
        $calculatedBaseUrl = substr($_SERVER['SCRIPT_NAME'], 0, -(strlen('index.php')));
65
        $baseUrl = $this->get('base_url', $calculatedBaseUrl);
66
        $baseUrlTrimmed = rtrim($baseUrl, ' /');
67
        $protocol = (!empty($_SERVER['HTTPS'])) ? 'https://' : 'http://';
68
        $host = (isset($_SERVER['HTTP_HOST'])) ? $_SERVER['HTTP_HOST'] : 'localhost';
69
        return $absolute ? $protocol . $host . $baseUrlTrimmed : $baseUrlTrimmed;
70
    }
71
72
    public function siteTitle()
73
    {
74
        return $this->get('site_title', 'A ' . App::name() . ' website');
75
    }
76
77
    public function siteEmail()
78
    {
79
        return $this->get('site_email', '[email protected]');
80
    }
81
82
    public function databaseHost()
83
    {
84
        return self::get('databaseHost', 'localhost');
85
    }
86
87
    public function databaseName()
88
    {
89
        return self::get('databaseName', 'taggedfiles');
90
    }
91
92
    public function databaseUser()
93
    {
94
        return self::get('databaseUser', 'root');
95
    }
96
97
    public function databasePassword()
98
    {
99
        return self::get('databasePassword', '');
100
    }
101
}
102