Completed
Push — master ( e6b3d9...94ffcd )
by Park Jong-Hun
03:09
created

AppDispatcher::resolveUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Core;
4
5
use Prob\Handler\ParameterMap;
6
use Prob\Router\Dispatcher;
7
use Prob\Rewrite\Request;
8
use Prob\Router\Matcher;
9
10
class AppDispatcher
11
{
12
13
    /**
14
     * @var Map
15
     */
16
    private $routerMap;
17
    private $routerConfig = [];
18
    private $viewEngineConfig = [];
19
20
    public function setRouterConfig(array $routerConfig)
21
    {
22
        $this->routerConfig = $routerConfig;
23
    }
24
25
    public function setViewEngineConfig(array $config)
26
    {
27
        $this->viewEngineConfig = $config;
28
    }
29
30
    public function dispatch(Request $request)
31
    {
32
        $this->buildRouterMap();
33
34
        $viewModel = new ViewModel();
35
        $parameterMap = $this->getControllerParameterMap($request, $viewModel);
36
        $returnOfController = $this->executeController($request, $parameterMap);
37
38
        $this->renderView($returnOfController, $viewModel);
39
    }
40
41
    private function buildRouterMap()
42
    {
43
        $builder = new RouterMapBuilder();
44
        $builder->setRouterConfig($this->routerConfig);
45
46
        $this->routerMap = $builder->build();
0 ignored issues
show
Documentation Bug introduced by
It seems like $builder->build() of type object<Prob\Router\Map> is incompatible with the declared type object<Core\Map> of property $routerMap.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
47
    }
48
49
    private function getControllerParameterMap(Request $request, ViewModel $viewModel)
50
    {
51
        $url = $this->resolveUrl($request);
52
53
        $parameterMap = new ParameterMap();
54
        $this->bindUrlParameter($parameterMap, $url);
55
        $this->bindViewModelParameter($parameterMap, $viewModel);
56
57
        return $parameterMap;
58
    }
59
60
    private function resolveUrl(Request $request)
61
    {
62
        $matcher = new Matcher($this->routerMap);
63
        return $matcher->match($request)['urlNameMatching'] ?: [];
64
    }
65
66
    private function bindUrlParameter(ParameterMap $map, array $url)
67
    {
68
        $map->bindByNameWithType('array', 'url', $url);
69
70
        foreach ($url as $name => $value) {
71
            $map->bindByName($name, $value);
72
        }
73
    }
74
75
    private function bindViewModelParameter(ParameterMap $map, ViewModel $viewModel)
76
    {
77
        $map->bindByType(ViewModel::class, $viewModel);
78
    }
79
80
81
    private function executeController(Request $request, ParameterMap $parameterMap)
82
    {
83
        $dispatcher = new Dispatcher($this->routerMap);
84
        return $dispatcher->dispatch($request, $parameterMap);
85
    }
86
87
    private function renderView($returnOfController, ViewModel $viewModel)
88
    {
89
        $view = $this->resolveView($returnOfController);
90
91
        foreach ($viewModel->getVariables() as $key => $value) {
92
            $view->set($key, $value);
93
        }
94
95
        $view->render();
96
    }
97
98
    /**
99
     * @param  mixed $returnOfController
100
     * @return View
101
     */
102
    private function resolveView($returnOfController)
103
    {
104
        $viewResolver = new ViewResolver($returnOfController);
105
        return $viewResolver->resolve($this->viewEngineConfig);
106
    }
107
}
108