Completed
Push — master ( 6c83b3...c25ca3 )
by Park Jong-Hun
04:06 queued 45s
created

DispatcherBootstrap   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 11 1
A getServerRequest() 0 10 1
A stripUrlPrefix() 0 10 3
A getSiteConfig() 0 4 1
A getViewEngineConfig() 0 4 1
A getViewResolvers() 0 4 1
A getRouterConfig() 0 4 1
1
<?php
2
3
namespace App\Bootstrap;
4
5
use Core\Bootstrap\BootstrapInterface;
6
use Core\Application;
7
use Core\ControllerDispatcher\Dispatcher;
8
use Zend\Diactoros\Uri;
9
use Zend\Diactoros\ServerRequestFactory;
10
11
class DispatcherBootstrap implements BootstrapInterface
12
{
13
14
    public function boot(Application $app)
15
    {
16
        $dispatcher = new Dispatcher();
17
18
        $dispatcher->setRequest($this->getServerRequest());
19
        $dispatcher->setRouterConfig($this->getRouterConfig());
20
        $dispatcher->setViewEngineConfig($this->getViewEngineConfig());
21
        $dispatcher->setViewResolver($this->getViewResolvers());
22
23
        $dispatcher->dispatch();
24
    }
25
26
    private function getServerRequest()
27
    {
28
        $request = ServerRequestFactory::fromGlobals();
29
30
        $stripedUri = new Uri(
31
            $this->stripUrlPrefix($request->getUri()->getPath())
32
        );
33
34
        return $request->withUri($stripedUri);
35
    }
36
37
    private function stripUrlPrefix($url)
38
    {
39
        $appUrl = $this->getSiteConfig()['url'];
40
41
        if (substr($url, 0, strlen($appUrl)) === $appUrl) {
42
            return '/' . substr($url, strlen($appUrl)) ?: '/';
43
        }
44
45
        return $url;
46
    }
47
48
    private function getSiteConfig()
49
    {
50
        return require __DIR__ . '/../../config/site.php';
51
    }
52
53
    private function getViewEngineConfig()
54
    {
55
        return require __DIR__ . '/../../config/viewEngine.php';
56
    }
57
58
    private function getViewResolvers()
59
    {
60
        return require __DIR__ . '/../../config/viewResolver.php';
61
    }
62
63
    private function getRouterConfig()
64
    {
65
        return require __DIR__ . '/../../config/router.php';
66
    }
67
}
68