RequestSubscriber   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 0%
Metric Value
wmc 8
lcom 1
cbo 9
dl 0
loc 91
ccs 0
cts 43
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getEvents() 0 9 1
A initRequest() 0 7 1
A initCollectors() 0 18 2
A handleRequest() 0 16 2
A sendResponse() 0 14 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 41 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
declare(strict_types = 1);
3
/**
4
 * Copyright (C) 2015  Alexander Schmidt
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 * @author     Alexander Schmidt <[email protected]>
19
 * @copyright  Copyright (c) 2016, Alexander Schmidt
20
 * @date       07.02.16
21
 */
22
23
namespace Bonefish\Request;
24
25
26
use AValnar\EventDispatcher\EventDispatcher;
27
use AValnar\EventDispatcher\EventSubscriber;
28
use AValnar\FileToClassMapper\Mapper;
29
use Bonefish\Events;
30
use Bonefish\Injection\Container\Container;
31
use Bonefish\Reflection\ReflectionService;
32
use Bonefish\Request\Event\RequestEvent;
33
use Bonefish\Request\Event\ResponseEvent;
34
use Bonefish\RouteCollectors\RestRouteCollector;
35
use Bonefish\Router\Collectors\CombinedRouteCollector;
36
use Bonefish\Router\Collectors\RouteCollector;
37
use Bonefish\Router\Request\RequestHandlerInterface;
38
use Symfony\Component\HttpFoundation\Request;
39
use Symfony\Component\HttpFoundation\Response;
40
41
final class RequestSubscriber implements EventSubscriber
42
{
43
    /**
44
     * @var EventDispatcher
45
     */
46
    private $eventDispatcher;
47
48
    /**
49
     * @var Container
50
     */
51
    private $container;
52
53
    public function __construct(EventDispatcher $eventDispatcher, Container $container)
54
    {
55
        $this->eventDispatcher = $eventDispatcher;
56
        $this->container = $container;
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getEvents() : array
63
    {
64
        return [
65
            'initRequest' => [[Events::REQUEST_INIT]],
66
            'initCollectors' => [[Events::REQUEST_INIT]],
67
            'handleRequest' => [[Events::REQUEST_BEFORE_HANDLE, Events::COLLECTORS_INIT], EventDispatcher::USE_LAST],
68
            'sendResponse' => [[Events::RESPONSE_BEFORE_SEND], EventDispatcher::USE_LAST]
69
        ];
70
    }
71
72
    public function initRequest()
73
    {
74
        $request = Request::createFromGlobals();
75
76
        $event = new RequestEvent($request);
77
        $this->eventDispatcher->dispatch(Events::REQUEST_BEFORE_HANDLE, $event);
78
    }
79
80
    public function initCollectors()
81
    {
82
        $routeCollector = $this->container->get(RouteCollector::class);
83
84
        if ($routeCollector instanceof CombinedRouteCollector) {
85
            $routeCollector->addCollector(
86
                new RestRouteCollector(
87
                    $this->container->get(Mapper::class),
88
                    $this->container->get(ReflectionService::class),
89
                    $this->container,
90
                    BONEFISH_PACKAGE_PATH,
91
                    BONEFISH_VENDOR_PATH
92
                )
93
            );
94
        }
95
96
        $this->eventDispatcher->dispatch(Events::COLLECTORS_INIT);
97
    }
98
99
    public function handleRequest(array $events)
100
    {
101
        /** @var Request $request */
102
        $request = $events[Events::REQUEST_BEFORE_HANDLE]->getRequest();
103
        /** @var RequestHandlerInterface $requestHandler */
104
        $requestHandler = $this->container->get(RequestHandlerInterface::class);
105
106
        $response = $requestHandler->handleRequest($request);
107
108
        if (!$response instanceof Response) {
109
            $response = new Response($response);
110
        }
111
112
        $event = new ResponseEvent($request, $response);
113
        $this->eventDispatcher->dispatch(Events::RESPONSE_BEFORE_SEND, $event);
114
    }
115
116
    public function sendResponse(array $events)
117
    {
118
        /** @var Response $response */
119
        $response = $events[Events::RESPONSE_BEFORE_SEND]->getResponse();
120
        /** @var Request $request */
121
        $request = $events[Events::RESPONSE_BEFORE_SEND]->getRequest();
122
123
        $response->headers->set('x-bonefish', true);
124
        $response->prepare($request);
125
        $response->sendHeaders();
126
        $response->sendContent();
127
128
        $this->eventDispatcher->dispatch(Events::RESPONSE_AFTER_SEND, $events[Events::RESPONSE_BEFORE_SEND]);
129
    }
130
131
}