Completed
Push — 4.0 ( 6e7e32...0ca36c )
by Marco
12:30
created

Dispatcher::getDefaultConfiguration()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 18
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
    }
95
96
    public function configuration() {
97
98
        return $this->configuration;
99
100
    }
101
102
    public function events() {
103
104
        return $this->events;
105
106
    }
107
108
    public function cache() {
109
110
        return $this->cache;
111
112
    }
113
114
    public function request() {
115
116
        return $this->request;
117
118
    }
119
120
    public function router() {
121
122
        return $this->router;
123
124
    }
125
126
    public function response() {
127
128
        return $this->response;
129
130
    }
131
132
    public function extra() {
133
134
        return $this->extra;
135
136
    }
137
138
    public function dispatch() {
139
140
        $this->events->emit( new DispatcherEvent($this) );
141
142
        if ( $this->configuration()->get('enabled') === false ) {
143
144
            $status = $this->configuration()->get('disabled-status');
145
146
            $content = $this->configuration()->get('disabled-message');
147
148
            $this->response()->status()->set($status);
149
150
            $this->response()->content()->set($content);
151
152
            return $this->shutdown();
153
154
        }
155
156
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') );
157
158
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get()) );
159
160
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') );
161
162
        try {
163
164
            $this->router->route($this->request);
165
166
        } catch (DispatcherException $de) {
167
168
            $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...
169
170
            $this->response()->content()->set( $de->getMessage() );
171
172
            return $this->shutdown();
173
174
        }
175
176
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') );
177
178
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getType()) );
179
180
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getService()) );
181
182
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') );
183
184
        // translate route to service
185
186
        try {
187
188
            $this->router->compose($this->response);
189
190
        } catch (DispatcherException $de) {
191
192
            $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...
193
194
            $this->response()->content()->set( $de->getMessage() );
195
196
        }
197
198
        return $this->shutdown();
199
200
    }
201
202
    private function emitServiceSpecializedEvents($name) {
203
204
        return new ServiceEvent(
205
            $name,
206
            $this->logger,
207
            $this->request,
208
            $this->router,
209
            $this->response,
210
            $this->extra
211
        );
212
213
    }
214
215
    private function shutdown() {
216
217
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') );
218
219
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) );
220
221
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') );
222
223
        $return = Processor::parse($this->configuration, $this->logger, $this->response);
224
225
        ob_end_clean();
226
227
        return $return;
228
229
    }
230
231
}
232