Dispatcher::triggerEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Core\ControllerDispatcher;
4
5
use Prob\Handler\ProcInterface;
6
use Prob\Handler\ParameterMap;
7
use Prob\Router\Exception\RoutePathNotFound;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Prob\Router\Dispatcher as RouterDispatcher;
10
use Prob\Router\Matcher;
11
use Prob\Router\Map;
12
13
class Dispatcher
14
{
15
    /**
16
     * @var Map
17
     */
18
    private $routerMap;
19
20
    /**
21
     * @var ParameterMap
22
     */
23
    private $parameterMap;
24
25
    /**
26
     * @var ServerRequestInterface
27
     */
28
    private $request;
29
30
    public function setRouterMap(Map $routerMap)
31
    {
32
        $this->routerMap = $routerMap;
33
    }
34
35
    public function setRequest(ServerRequestInterface $request)
36
    {
37
        $this->request = $request;
38
    }
39
40
    public function setParameterMap(ParameterMap $parameterMap)
41
    {
42
        $this->parameterMap = $parameterMap;
43
    }
44
45
    public function dispatch()
46
    {
47
        $dispatcher = new RouterDispatcher($this->routerMap);
48
49
        /**
50
         * TODO 클로저와 일반함수 형태에서도 컨트롤러 이벤트가 작동하도록 수정해야함.
51
         */
52
53
        $this->triggerEvent('before');
54
55
        $result = $dispatcher->dispatch($this->request, $this->parameterMap);
56
57
        $this->triggerEvent('after');
58
59
        return $result;
60
    }
61
62
    private function triggerEvent($operation)
63
    {
64
        $this->validateRoutePath();
65
66
        ControllerEvent::triggerEvent(
67
            RequestMatcher::getControllerProc()->getName(),
68
            $operation,
69
            [$this->parameterMap]
70
        );
71
    }
72
73
    private function validateRoutePath() {
74
        if(RequestMatcher::getControllerProc() === null) {
75
            throw new RoutePathNotFound(
76
                sprintf('Route path not found: %s (%s)',
77
                    $this->request->getUri()->getPath(),
78
                    $this->request->getMethod()
79
                )
80
            );
81
        }
82
    }
83
}
84