Completed
Push — 4.0 ( ed2d0a...2bab8b )
by Marco
15:57
created

Dispatcher::dispatch()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
dl 0
loc 37
rs 8.8571
c 4
b 1
f 0
cc 1
eloc 17
nc 1
nop 0
1
<?php namespace Comodojo\Dispatcher;
2
3
use \Monolog\Logger;
4
use \Comodojo\Dispatcher\Components\Configuration;
5
use \Comodojo\Dispatcher\Request\Model as Request;
6
use \Comodojo\Dispatcher\Router\Collector as RouteCollector;
7
use \Comodojo\Dispatcher\Response\Model as Response;
8
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait;
9
use \Comodojo\Dispatcher\Output\Processor;
10
use \Comodojo\Dispatcher\Events\DispatcherEvent;
11
use \Comodojo\Dispatcher\Events\ServiceEvent;
12
use \Comodojo\Cache\CacheManager;
13
use \League\Event\Emitter;
14
15
/**
16
 *
17
 * @package     Comodojo dispatcher
18
 * @author      Marco Giovinazzi <[email protected]>
19
 * @license     GPL-3.0+
20
 *
21
 * LICENSE:
22
 *
23
 * This program is free software: you can redistribute it and/or modify
24
 * it under the terms of the GNU Affero General Public License as
25
 * published by the Free Software Foundation, either version 3 of the
26
 * License, or (at your option) any later version.
27
 *
28
 * This program is distributed in the hope that it will be useful,
29
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31
 * GNU Affero General Public License for more details.
32
 *
33
 * You should have received a copy of the GNU Affero General Public License
34
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35
 */
36
37
class Dispatcher {
38
39
    use TimestampTrait;
40
41
    private $configuration = null;
42
43
    private $request = null;
44
45
    private $router = null;
46
47
    private $response = null;
48
49
    private $logger = null;
50
51
    private $cache = null;
52
53
    private $events = null;
54
55
    public function __construct() {
56
57
        ob_start();
58
59
        $this->setTimestamp();
60
61
        $this->configuration = new Configuration();
62
63
        $this->events = new Emitter();
64
65
        $this->cache = new CacheManager( CacheManager::PICK_FIRST );
66
67
        $this->request = new Request($this->configuration, $this->logger);
68
69
        $this->router = new RouteCollector($this->configuration, $this->logger, $this->cache);
70
71
        $this->response = new Response($this->configuration, $this->logger);
72
73
    }
74
75
    public function configuration() {
76
77
        return $this->configuration;
78
79
    }
80
81
    public function events() {
82
83
        return $this->events;
84
85
    }
86
87
    public function cache() {
88
89
        return $this->cache;
90
91
    }
92
93
    public function request() {
94
95
        return $this->request;
96
97
    }
98
99
    public function router() {
100
101
        return $this->router;
102
103
    }
104
105
    public function response() {
106
107
        return $this->response;
108
109
    }
110
111
    public function dispatch() {
112
113
        $this->events->emit( new DispatcherEvent($this) );
114
115
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') );
116
117
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get()) );
118
119
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') );
120
121
        $this->router->route($this->request);
122
123
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') );
124
125
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->route()->type) );
126
127
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->route()->service) );
128
129
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') );
130
131
        // translate route to service
132
133
        $this->router->compose($this->response);
134
135
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') );
136
137
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) );
138
139
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') );
140
141
        $return = Processor::parse($this->configuration, $this->logger, $this->response);
142
143
        ob_end_clean();
144
145
        return $return;
146
147
    }
148
149
    private function emitServiceSpecializedEvents($name) {
150
151
        return new ServiceEvent(
152
            $name,
153
            $this->logger,
154
            $this->request,
155
            $this->router,
156
            $this->response
157
        );
158
159
    }
160
161
}
162