Completed
Push — master ( 269997...7f9d27 )
by Alexis
01:43
created

Application   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 8
dl 0
loc 125
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getCacheDir() 0 4 1
A getConfigurationDir() 0 8 2
A getEnvironment() 0 4 1
A getLogDir() 0 4 1
A getRootDir() 0 8 2
A loadConfiguration() 0 9 2
A loadRoutes() 0 5 1
A registerControllers() 0 11 3
A registerHandlers() 0 5 1
A registerProviders() 0 9 4
1
<?php
2
3
namespace App;
4
5
use Silex\Application as App;
6
7
class Application extends App
8
{
9
    use App\FormTrait;
10
    use App\MonologTrait;
11
    use App\SecurityTrait;
12
    use App\SwiftmailerTrait;
13
    use App\TranslationTrait;
14
    use App\TwigTrait;
15
    use App\UrlGeneratorTrait;
16
17
    /**
18
     * @var string
19
     */
20
    protected $configurationDir;
21
22
    /**
23
     * @var string
24
     */
25
    protected $environment;
26
27
    /**
28
     * @var string
29
     */
30
    protected $rootDir;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param string $environment
36
     * @param bool   $debug
37
     * @param array  $values
38
     */
39
    public function __construct(string $environment, bool $debug = false, array $values = [])
40
    {
41
        parent::__construct($values);
42
43
        $this['debug'] = $debug;
44
45
        $this->environment = $environment;
46
        $this->rootDir = $this->getRootDir();
47
48
        $this->registerProviders();
49
        $this->loadConfiguration();
50
        $this->registerControllers();
51
        $this->registerHandlers();
52
        $this->loadRoutes();
53
    }
54
55
    public function getCacheDir()
56
    {
57
        return $this->getRootDir().'/var/cache/'.$this->environment;
58
    }
59
60
    public function getConfigurationDir()
61
    {
62
        if (null === $this->configurationDir) {
63
            $this->configurationDir = $this->getRootDir().'/config';
64
        }
65
66
        return $this->configurationDir;
67
    }
68
69
    public function getEnvironment()
70
    {
71
        return $this->environment;
72
    }
73
74
    public function getLogDir()
75
    {
76
        return $this->getRootDir().'/var/log';
77
    }
78
79
    public function getRootDir()
80
    {
81
        if (null === $this->rootDir) {
82
            $this->rootDir = dirname(__DIR__);
83
        }
84
85
        return $this->rootDir;
86
    }
87
88
    public function loadConfiguration()
89
    {
90
        $app = $this;
91
        if (file_exists($this->getConfigurationDir().'/container.'.$app->getEnvironment().'.php')) {
92
            require $this->getConfigurationDir().'/container.'.$app->getEnvironment().'.php';
93
        } else {
94
            require $this->getConfigurationDir().'/container.php';
95
        }
96
    }
97
98
    public function loadRoutes()
99
    {
100
        $app = $this;
101
        require $this->getConfigurationDir().'/routes.php';
102
    }
103
104
    public function registerControllers()
105
    {
106
        if (file_exists($this->getConfigurationDir().'/controllers.php')) {
107
            $controllers = require $this->getConfigurationDir().'/controllers.php';
108
            foreach ($controllers as $key => $class) {
109
                $this[$key] = function ($app) use ($class) {
110
                    return new $class($app);
111
                };
112
            }
113
        }
114
    }
115
116
    public function registerHandlers()
117
    {
118
        $app = $this;
119
        require $this->getConfigurationDir().'/handlers.php';
120
    }
121
122
    public function registerProviders()
123
    {
124
        $providers = require $this->getConfigurationDir().'/providers.php';
125
        foreach ($providers as $class => $environments) {
126
            if (isset($environments['all']) || isset($environments[$this->environment])) {
127
                $this->register(new $class());
128
            }
129
        }
130
    }
131
}
132