Passed
Push — master ( b91050...6fb96f )
by Jens
02:59
created

Application   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 173
rs 10
wmc 18
lcom 1
cbo 11

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A config() 0 10 2
A storage() 0 4 1
A unlockApplicationComponentParameters() 0 7 2
A getTemplateDir() 0 4 1
A getStorageDir() 0 4 1
A getApplicationComponents() 0 4 1
A getRootDir() 0 4 1
A setExceptionHandler() 0 6 1
A urlMatching() 0 6 1
A run() 0 6 1
A render() 0 6 1
A startServices() 0 6 1
A getAllApplicationComponentParameters() 0 9 2
A addMatchedSitemapItem() 0 4 1
1
<?php
2
3
namespace CloudControl\Cms\cc;
4
5
use CloudControl\Cms\cc\application\ApplicationRenderer;
6
use CloudControl\Cms\cc\application\ApplicationRunner;
7
use CloudControl\Cms\cc\application\UrlMatcher;
8
use CloudControl\Cms\services\FileService;
9
use CloudControl\Cms\services\ImageService;
10
use CloudControl\Cms\services\ValuelistService;
11
use CloudControl\Cms\storage\Storage;
12
use Whoops\Handler\PrettyPageHandler;
13
use Whoops\Run;
14
15
class Application
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $rootDir;
21
    /**
22
     * @var string
23
     */
24
    protected $configPath;
25
    /**
26
     * @var \stdClass
27
     */
28
    private $config;
29
    /**
30
     * @var \CloudControl\Cms\storage\Storage
31
     */
32
    private $storage;
33
34
    /**
35
     * @var \CloudControl\Cms\cc\Request
36
     */
37
    private $request;
38
39
    /**
40
     * @var array
41
     */
42
    private $matchedSitemapItems = array();
43
44
    /**
45
     * @var array
46
     */
47
    private $applicationComponents = array();
48
49
    /**
50
     * Application constructor.
51
     * @param string $rootDir
52
     * @param string $configPath
53
     * @throws \Exception
54
     */
55
    public function __construct($rootDir, $configPath)
56
    {
57
        $this->rootDir = $rootDir;
58
        $this->configPath = $configPath;
59
60
        $this->config();
61
        $this->storage();
62
63
        $this->request = new Request();
64
65
        $this->setExceptionHandler();
66
67
        $this->startServices();
68
69
        $this->urlMatching();
70
71
        $this->getApplicationComponents();
72
73
        $this->run();
74
        $this->render();
75
    }
76
77
    /**
78
     * Initialize the config
79
     *
80
     * @throws \Exception
81
     */
82
    private function config()
83
    {
84
        if (realpath($this->configPath) !== false) {
85
            $json = file_get_contents($this->configPath);
86
            $this->config = json_decode($json);
87
            $this->config->rootDir = $this->rootDir;
88
        } else {
89
            throw new \RuntimeException('Framework not initialized yet. Consider running composer install');
90
        }
91
    }
92
93
    /**
94
     * Initialize the storage
95
     */
96
    private function storage()
97
    {
98
        $this->storage = new Storage($this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->storageDir, $this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->imagesDir, $this->config->filesDir);
99
    }
100
101
    public function getAllApplicationComponentParameters()
102
    {
103
        $allParameters = array();
104
        foreach ($this->applicationComponents as $applicationComponent) {
105
            $parameters = $applicationComponent->{'object'}->getParameters();
106
            $allParameters[] = $parameters;
107
        }
108
        return $allParameters;
109
    }
110
111
    public function unlockApplicationComponentParameters()
112
    {
113
        foreach ($this->applicationComponents as $applicationComponent) {
114
            $parameters = $applicationComponent->{'object'}->getParameters();
115
            extract($parameters, EXTR_OVERWRITE);
116
        }
117
    }
118
119
    /**
120
     * @return string
121
     */
122
    public function getTemplateDir()
123
    {
124
        return $this->config->templateDir;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getStorageDir()
131
    {
132
        return $this->config->storageDir;
133
    }
134
135
    public function getApplicationComponents()
136
    {
137
        $this->applicationComponents = $this->storage->getApplicationComponents()->getApplicationComponents();
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getRootDir()
144
    {
145
        return $this->config->rootDir;
146
    }
147
148
    private function setExceptionHandler()
149
    {
150
        $whoops = new Run;
151
        $whoops->pushHandler(new PrettyPageHandler);
0 ignored issues
show
Documentation introduced by
new \Whoops\Handler\PrettyPageHandler() is of type object<Whoops\Handler\PrettyPageHandler>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
152
        $whoops->register();
153
    }
154
155
    private function urlMatching()
156
    {
157
        $urlMatcher = new UrlMatcher($this, $this->storage);
158
        $urlMatcher->redirectMatching($this->request);
159
        $urlMatcher->sitemapMatching($this->request);
160
    }
161
162
    private function run()
163
    {
164
        $applicationRunner = new ApplicationRunner($this->storage, $this->request);
165
        $applicationRunner->runApplicationComponents($this->applicationComponents);
166
        $applicationRunner->runSitemapComponents($this->matchedSitemapItems);
167
    }
168
169
    private function render()
170
    {
171
        $applicationRenderer = new ApplicationRenderer($this, $this->storage, $this->request);
172
        $applicationRenderer->renderApplicationComponents($this->applicationComponents);
173
        $applicationRenderer->renderSitemapComponents($this->matchedSitemapItems);
174
    }
175
176
    private function startServices()
177
    {
178
        FileService::getInstance()->init($this->storage);
179
        ImageService::getInstance()->init($this->storage);
180
        ValuelistService::getInstance()->init($this->storage);
181
    }
182
183
    public function addMatchedSitemapItem($matchedClone)
184
    {
185
        $this->matchedSitemapItems[] = $matchedClone;
186
    }
187
}