Completed
Push — 4.0 ( f0f3ee...7cd9cd )
by Marco
15:15
created

Dispatcher::shutdown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
165
166
            $this->response()->content()->set( $de->getContent() );
0 ignored issues
show
Bug introduced by
The method getContent() does not seem to exist on object<Comodojo\Exception\DispatcherException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
168
            return $this->shutdown();
169
170
        }
171
172
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') );
173
174
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getType()) );
175
176
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getService()) );
177
178
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') );
179
180
        // translate route to service
181
182
        try {
183
184
            $this->router->compose($this->response);
185
186
        } catch (DispatcherException $de) {
187
188
            $this->response()->status()->set( $de->getStatus() );
0 ignored issues
show
Bug introduced by
The method getStatus() does not seem to exist on object<Comodojo\Exception\DispatcherException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
189
190
            $this->response()->content()->set( $de->getContent() );
0 ignored issues
show
Bug introduced by
The method getContent() does not seem to exist on object<Comodojo\Exception\DispatcherException>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
191
192
        }
193
194
        return $this->shutdown();
195
196
    }
197
198
    private function emitServiceSpecializedEvents($name) {
199
200
        return new ServiceEvent(
201
            $name,
202
            $this->logger,
203
            $this->request,
204
            $this->router,
205
            $this->response,
206
            $this->extra
207
        );
208
209
    }
210
211
    private function shutdown() {
212
213
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') );
214
215
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) );
216
217
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') );
218
219
        $return = Processor::parse($this->configuration, $this->logger, $this->response);
220
221
        ob_end_clean();
222
223
        return $return;
224
225
    }
226
227
}
228