Completed
Push — 4.0 ( 0ca36c...7855d8 )
by Marco
12:29
created

Dispatcher::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace Comodojo\Dispatcher;
2
3
use \Monolog\Logger;
4
use \Comodojo\Dispatcher\Components\Configuration;
5
use \Comodojo\Dispatcher\Components\DefaultConfiguration;
6
use \Comodojo\Dispatcher\Request\Model as Request;
7
use \Comodojo\Dispatcher\Router\Collector as RouteCollector;
8
use \Comodojo\Dispatcher\Response\Model as Response;
9
use \Comodojo\Dispatcher\Extra\Model as Extra;
10
use \Comodojo\Dispatcher\Components\Timestamp as TimestampTrait;
11
use \Comodojo\Dispatcher\Output\Processor;
12
use \Comodojo\Dispatcher\Events\DispatcherEvent;
13
use \Comodojo\Dispatcher\Events\ServiceEvent;
14
use \Comodojo\Dispatcher\Router\RoutingTableInterface;
15
use \Comodojo\Dispatcher\Log\DispatcherLogger;
16
use \Comodojo\Dispatcher\Cache\DispatcherCache;
17
use \Comodojo\Dispatcher\Events\EventsManager;
18
use \Comodojo\Cache\CacheManager;
19
use \League\Event\Emitter;
20
use \Comodojo\Exception\DispatcherException;
21
use \Exception;
22
23
/**
24
 * @package     Comodojo Dispatcher
25
 * @author      Marco Giovinazzi <[email protected]>
26
 * @author      Marco Castiello <[email protected]>
27
 * @license     GPL-3.0+
28
 *
29
 * LICENSE:
30
 *
31
 * This program is free software: you can redistribute it and/or modify
32
 * it under the terms of the GNU Affero General Public License as
33
 * published by the Free Software Foundation, either version 3 of the
34
 * License, or (at your option) any later version.
35
 *
36
 * This program is distributed in the hope that it will be useful,
37
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
39
 * GNU Affero General Public License for more details.
40
 *
41
 * You should have received a copy of the GNU Affero General Public License
42
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43
 */
44
45
class Dispatcher {
46
47
    use TimestampTrait;
48
49
    private $configuration;
50
51
    private $request;
52
53
    private $router;
54
55
    private $response;
56
57
    private $extra;
58
59
    private $logger;
60
61
    private $cache;
62
63
    private $events;
64
65
    public function __construct(
66
        $configuration = array(),
67
        Emitter $emitter = null,
68
        CacheManager $cache = null,
69
        Logger $logger = null
70
    ) {
71
72
        ob_start();
73
74
        $this->setTimestamp();
75
76
        $this->extra = new Extra($this->logger);
0 ignored issues
show
Unused Code introduced by
The call to Model::__construct() has too many arguments starting with $this->logger.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
77
78
        $this->configuration = new Configuration( DefaultConfiguration::get() );
79
80
        $this->configuration->merge($configuration);
81
82
        $this->events = is_null($emitter) ? new EventsManager() : $emitter;
83
84
        $this->logger = is_null($logger) ? DispatcherLogger::create($this->configuration) : $logger;
85
86
        $this->cache = is_null($cache) ? DispatcherCache::create($this->configuration, $this->logger) : $cache;
87
88
        $this->request = new Request($this->configuration, $this->logger);
89
90
        $this->router = new RouteCollector($this->configuration, $this->logger, $this->cache, $this->extra);
91
92
        $this->response = new Response($this->configuration, $this->logger);
93
94
        $this->logger->debug("Dispatcher ready, current date ".date('c', $this->getTimestamp()));
95
96
    }
97
98
    public function configuration() {
99
100
        return $this->configuration;
101
102
    }
103
104
    public function events() {
105
106
        return $this->events;
107
108
    }
109
110
    public function cache() {
111
112
        return $this->cache;
113
114
    }
115
116
    public function request() {
117
118
        return $this->request;
119
120
    }
121
122
    public function router() {
123
124
        return $this->router;
125
126
    }
127
128
    public function response() {
129
130
        return $this->response;
131
132
    }
133
134
    public function extra() {
135
136
        return $this->extra;
137
138
    }
139
140
    public function logger() {
141
142
        return $this->logger;
143
144
    }
145
146
    public function dispatch() {
147
148
        $this->logger->debug("Starting to dispatch.");
149
150
        $this->logger->debug("Emitting global dispatcher event.");
151
152
        $this->events->emit( new DispatcherEvent($this) );
153
154
        if ( $this->configuration()->get('enabled') === false ) {
155
156
            $this->logger->debug("Dispatcher disabled, shutting down gracefully.");
157
158
            $status = $this->configuration()->get('disabled-status');
159
160
            $content = $this->configuration()->get('disabled-message');
161
162
            $this->response()->status()->set($status);
163
164
            $this->response()->content()->set($content);
165
166
            return $this->shutdown();
167
168
        }
169
170
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') );
171
172
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get()) );
173
174
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') );
175
176
        $this->logger->debug("Starting router.");
177
178
        try {
179
180
            $this->router->route($this->request);
181
182
        } catch (DispatcherException $de) {
183
184
            $status = $de->getStatus();
185
186
            $message = $de->getMessage();
187
188
            $this->logger->debug("Route error ($status), shutting down dispatcher.");
189
190
            $this->response()->status()->set($status);
191
192
            $this->response()->content()->set($message);
193
194
            return $this->shutdown();
195
196
        }
197
198
        $route_type = $this->router->getType();
199
200
        $route_service = $this->router->getService();
201
202
        $this->logger->debug("Route acquired, type $route_type directed to $route_service.");
203
204
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') );
205
206
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_type) );
207
208
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$route_service) );
209
210
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') );
211
212
        // translate route to service
213
214
        $this->logger->debug("Running $route_service service.");
215
216
        try {
217
218
            $this->router->compose($this->response);
219
220
        } catch (DispatcherException $de) {
221
222
            $this->response()->status()->set( $de->getStatus() );
223
224
            $this->response()->content()->set( $de->getMessage() );
225
226
        }
227
228
        return $this->shutdown();
229
230
    }
231
232
    private function emitServiceSpecializedEvents($name) {
233
234
        $this->logger->debug("Emitting $name service-event.");
235
236
        return new ServiceEvent(
237
            $name,
238
            $this->logger,
239
            $this->request,
240
            $this->router,
241
            $this->response,
242
            $this->extra
243
        );
244
245
    }
246
247
    private function shutdown() {
248
249
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') );
250
251
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) );
252
253
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') );
254
255
        $this->logger->debug("Composing return value.");
256
257
        $return = Processor::parse($this->configuration, $this->logger, $this->response);
258
259
        $this->logger->debug("Dispatcher run-cycle ends.");
260
261
        ob_end_clean();
262
263
        return $return;
264
265
    }
266
267
}
268