Completed
Push — master ( 75846b...78df43 )
by Jeroen
02:32
created

HttpKernel   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 1
cbo 6
dl 0
loc 71
ccs 0
cts 33
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 0 33 2
A send() 0 4 1
A buildMiddlewareStack() 0 10 2
1
<?php
2
3
namespace Protoku\Kernel;
4
5
use League\Container\ContainerAwareInterface;
6
use League\Route\RouteCollection;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Relay\RelayBuilder;
11
use Zend\Diactoros\Response\SapiEmitter;
12
13
/**
14
 * Class HttpKernel
15
 *
16
 * @package Protoku\Kernel
17
 */
18
class HttpKernel extends AbstractKernel implements ContainerAwareInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $globalMiddleware = [
24
25
    ];
26
27
    protected $serviceProviders = [];
28
29
    /**
30
     * @param ServerRequestInterface $request
31
     * @param ResponseInterface $response
32
     *
33
     * @return RequestInterface
34
     */
35
    public function handle(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
36
    {
37
    	$container = $this->getContainer();
38
        $container->share(ServerRequestInterface::class, $request);
39
        $container->share(ResponseInterface::class, $response);
40
        $container->share('emitter', SapiEmitter::class);
41
42
        foreach ($this->serviceProviders as $serviceProvider)
43
		{
44
			$container->addServiceProvider($serviceProvider);
45
		}
46
47
		$route = new RouteCollection($container);
48
49
		$route->map('GET', '/', function (ServerRequestInterface $request, ResponseInterface $response) {
50
			$response->getBody()->write('<h1>Hello, World!</h1>');
51
52
			return $response;
53
		});
54
55
		return $route->dispatch($container->get('request'), $container->get('response'));
56
57
//        $middlewareResolver = new Resolver();
0 ignored issues
show
Unused Code Comprehensibility introduced by
46% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
59
//        $relayBuilder = new RelayBuilder($middlewareResolver);
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
60
        $relayBuilder = new RelayBuilder();
0 ignored issues
show
Unused Code introduced by
// $middlewareRes... \Relay\RelayBuilder(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
61
62
        $middlewareStack = $this->buildMiddleWareStack();
63
64
        $relay = $relayBuilder->newInstance($middlewareStack);
65
66
        return $relay($request, $response);
67
    }
68
69
    /**
70
     * @param ResponseInterface $response
71
     */
72
    public function send(ResponseInterface $response)
73
    {
74
        $this->getContainer()->get('emitter')->emit($response);
75
    }
76
77
78
    protected function buildMiddlewareStack()
79
    {
80
        $middleware = [];
81
82
        foreach ($this->globalMiddleware as $globalMiddleware) {
83
               $middleware[] = $this->getContainer()->get($globalMiddleware);
84
        }
85
86
        return $middleware;
87
    }
88
}
89