Completed
Push — 4.0 ( b7ae43...1bebd1 )
by Marco
16:17
created

Dispatcher::emitServiceSpecializedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 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\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\Cache\CacheManager;
16
use \League\Event\Emitter;
17
18
/**
19
 *
20
 * @package     Comodojo dispatcher
21
 * @author      Marco Giovinazzi <[email protected]>
22
 * @license     GPL-3.0+
23
 *
24
 * LICENSE:
25
 *
26
 * This program is free software: you can redistribute it and/or modify
27
 * it under the terms of the GNU Affero General Public License as
28
 * published by the Free Software Foundation, either version 3 of the
29
 * License, or (at your option) any later version.
30
 *
31
 * This program is distributed in the hope that it will be useful,
32
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34
 * GNU Affero General Public License for more details.
35
 *
36
 * You should have received a copy of the GNU Affero General Public License
37
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38
 */
39
40
class Dispatcher {
41
42
    use TimestampTrait;
43
44
    private $configuration;
45
46
    private $request;
47
48
    private $router;
49
50
    private $response;
51
    
52
    private $extra;
53
54
    private $logger;
55
56
    private $cache;
57
58
    private $events;
59
60
    public function __construct(
61
        Emitter $events = null,
62
        CacheManager $cache = null,
63
        Logger $logger = null
64
    ) {
65
66
        ob_start();
67
68
        $this->setTimestamp();
69
70
        $this->configuration = new Configuration();
71
        
72
        $this->events = is_null($events) ? new Emitter() : $emitter;
0 ignored issues
show
Bug introduced by
The variable $emitter does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
73
        
74
        $this->logger = is_null($logger) ? DispatcherLogger::create($this->configuration) : $logger;
75
        
76
        $this->cache = is_null($cache) ? new CacheManager( CacheManager::PICK_FIRST, $this->logger ) : $cache;
77
        
78
        $this->request = new Request($this->configuration, $this->logger);
79
        
80
        $this->router = new RouteCollector($this->configuration, $this->logger, $this->cache);
81
        
82
        $this->response = new Response($this->configuration, $this->logger);
83
        
84
        $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...
85
        
86
        $this->router->load( $routing_table );
0 ignored issues
show
Bug introduced by
The variable $routing_table does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
87
88
    }
89
90
    public function configuration() {
91
92
        return $this->configuration;
93
94
    }
95
96
    public function events() {
97
98
        return $this->events;
99
100
    }
101
102
    public function cache() {
103
104
        return $this->cache;
105
106
    }
107
108
    public function request() {
109
110
        return $this->request;
111
112
    }
113
114
    public function router() {
115
116
        return $this->router;
117
118
    }
119
120
    public function response() {
121
122
        return $this->response;
123
124
    }
125
    
126
    public function extra() {
127
128
        return $this->extra;
129
130
    }
131
132
    public function dispatch() {
133
134
        $this->events->emit( new DispatcherEvent($this) );
135
136
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request') );
137
138
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.'.$this->request->method()->get()) );
139
140
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.request.#') );
141
142
        $this->router->route($this->request);
143
144
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route') );
145
146
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getType()) );
147
148
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.'.$this->router->getService()) );
149
150
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.route.#') );
151
152
        // translate route to service
153
154
        $this->router->compose($this->response);
155
156
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response') );
157
158
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.'.$this->response->status()->get()) );
159
160
        $this->events->emit( $this->emitServiceSpecializedEvents('dispatcher.response.#') );
161
162
        $return = Processor::parse($this->configuration, $this->logger, $this->response);
163
164
        ob_end_clean();
165
166
        return $return;
167
168
    }
169
170
    private function emitServiceSpecializedEvents($name) {
171
172
        return new ServiceEvent(
173
            $name,
174
            $this->logger,
175
            $this->request,
176
            $this->router,
177
            $this->response,
178
            $this->extra
179
        );
180
181
    }
182
183
}
184