Completed
Push — 4.0 ( 7cd9cd...61462a )
by Marco
12:15
created

Dispatcher   B

Complexity

Total Complexity 18

Size/Duplication

Total Lines 195
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 17

Importance

Changes 17
Bugs 4 Features 3
Metric Value
wmc 18
c 17
b 4
f 3
lcom 1
cbo 17
dl 0
loc 195
rs 7.8571

12 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 30 4
A configuration() 0 5 1
A events() 0 5 1
A cache() 0 5 1
A request() 0 5 1
A table() 0 5 1
A router() 0 5 1
A response() 0 5 1
A extra() 0 5 1
B dispatch() 0 63 4
A emitServiceSpecializedEvents() 0 12 1
A shutdown() 0 15 1
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\Routes\RoutingTable;
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\Cache\CacheManager;
18
use \League\Event\Emitter;
19
use \Comodojo\Exception\DispatcherException;
20
use \Exception;
21
22
/**
23
 * @package     Comodojo Dispatcher
24
 * @author      Marco Giovinazzi <[email protected]>
25
 * @author      Marco Castiello <[email protected]>
26
 * @license     GPL-3.0+
27
 *
28
 * LICENSE:
29
 *
30
 * This program is free software: you can redistribute it and/or modify
31
 * it under the terms of the GNU Affero General Public License as
32
 * published by the Free Software Foundation, either version 3 of the
33
 * License, or (at your option) any later version.
34
 *
35
 * This program is distributed in the hope that it will be useful,
36
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38
 * GNU Affero General Public License for more details.
39
 *
40
 * You should have received a copy of the GNU Affero General Public License
41
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
42
 */
43
44
class Dispatcher {
45
46
    use TimestampTrait;
47
48
    private $configuration;
49
50
    private $request;
51
52
    private $router;
53
54
    private $table;
55
56
    private $response;
57
58
    private $extra;
59
60
    private $logger;
61
62
    private $cache;
63
64
    private $events;
65
66
    public function __construct(
67
        $configuration = array(),
68
        Emitter $emitter = null,
69
        CacheManager $cache = null,
70
        Logger $logger = null
71
    ) {
72
73
        ob_start();
74
75
        $this->setTimestamp();
76
77
        $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...
78
79
        $this->configuration = new Configuration($configuration);
80
81
        $this->events = is_null($emitter) ? new Emitter() : $emitter;
82
83
        $this->logger = is_null($logger) ? DispatcherLogger::create($this->configuration) : $logger;
84
85
        $this->cache = is_null($cache) ? DispatcherCache::create($this->configuration, $this->logger) : $cache;
86
87
        $this->request = new Request($this->configuration, $this->logger);
88
89
        $this->table = new RoutingTable();
90
91
        $this->router = new RouteCollector($this->table, $this->configuration, $this->logger, $this->cache, $this->extra);
92
93
        $this->response = new Response($this->configuration, $this->logger);
94
95
    }
96
97
    public function configuration() {
98
99
        return $this->configuration;
100
101
    }
102
103
    public function events() {
104
105
        return $this->events;
106
107
    }
108
109
    public function cache() {
110
111
        return $this->cache;
112
113
    }
114
115
    public function request() {
116
117
        return $this->request;
118
119
    }
120
121
    public function table() {
122
123
        return $this->table;
124
125
    }
126
127
    public function router() {
128
129
        return $this->router;
130
131
    }
132
133
    public function response() {
134
135
        return $this->response;
136
137
    }
138
139
    public function extra() {
140
141
        return $this->extra;
142
143
    }
144
145
    public function dispatch() {
146
147
        $this->events->emit( new DispatcherEvent($this) );
148
149
        if ( $this->configuration()->get('dispatcher-enabled') === false ) {
150
151
            $status = $this->configuration()->get('dispatcher-disabled-status');
152
153
            $content = $this->configuration()->get('dispatcher-disabled-message');
154
155
            $this->response()->status()->set($status);
156
157
            $this->response()->content()->set($content);
158
159
            return $this->shutdown();
160
161
        }
162
163
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') );
164
165
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get()) );
166
167
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') );
168
169
        try {
170
171
            $this->router->route($this->request);
172
173
        } catch (DispatcherException $de) {
174
175
            $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...
176
177
            $this->response()->content()->set( $de->getMessage() );
178
179
            return $this->shutdown();
180
181
        }
182
183
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') );
184
185
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getType()) );
186
187
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getService()) );
188
189
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') );
190
191
        // translate route to service
192
193
        try {
194
195
            $this->router->compose($this->response);
196
197
        } catch (DispatcherException $de) {
198
199
            $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...
200
201
            $this->response()->content()->set( $de->getMessage() );
202
203
        }
204
205
        return $this->shutdown();
206
207
    }
208
209
    private function emitServiceSpecializedEvents($name) {
210
211
        return new ServiceEvent(
212
            $name,
213
            $this->logger,
214
            $this->request,
215
            $this->router,
216
            $this->response,
217
            $this->extra
218
        );
219
220
    }
221
222
    private function shutdown() {
223
224
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') );
225
226
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) );
227
228
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') );
229
230
        $return = Processor::parse($this->configuration, $this->logger, $this->response);
231
232
        ob_end_clean();
233
234
        return $return;
235
236
    }
237
238
}
239