ResourceHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 84
c 0
b 0
f 0
ccs 26
cts 26
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __invoke() 0 6 1
A handle() 0 15 1
A toPsr7Response() 0 13 2
1
<?php
2
/**
3
 * This file is part of the BEAR.Middleware package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Middleware\Handler;
8
9
use BEAR\Middleware\Module\StreamRenderer;
10
use BEAR\Resource\RenderInterface;
11
use BEAR\Resource\ResourceInterface;
12
use BEAR\Resource\ResourceObject;
13
use BEAR\Sunday\Extension\Router\RouterInterface;
14
use Psr\Http\Message\RequestInterface;
15
use Psr\Http\Message\ResponseInterface as Response;
16
use Psr\Http\Message\ServerRequestInterface;
17
use Zend\Diactoros\Stream;
18
19
final class ResourceHandler
20
{
21
    /**
22
     * @var ResourceInterface
23
     */
24
    private $resource;
25
26
    /**
27
     * @var RouterInterface
28
     */
29
    private $router;
30
31
    /**
32
     * @var StreamRenderer
33
     */
34
    private $render;
35
36
    /**
37
     * @param ResourceInterface $resource
38
     * @param RouterInterface   $router
39
     * @param RenderInterface   $render
40
     */
41 6
    public function __construct(
42
        ResourceInterface $resource,
43
        RouterInterface $router,
44
        RenderInterface $render
45
    ) {
46 6
        $this->resource = $resource;
47 6
        $this->router = $router;
48 6
        $this->render = $render;
0 ignored issues
show
Documentation Bug introduced by
$render is of type object<BEAR\Resource\RenderInterface>, but the property $render was declared to be of type object<BEAR\Middleware\Module\StreamRenderer>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
49 6
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 6
    public function __invoke(RequestInterface $request, Response $response, callable $next = null)
55
    {
56 6
        $response = $this->handle($request, $response);
0 ignored issues
show
Compatibility introduced by
$request of type object<Psr\Http\Message\RequestInterface> is not a sub-type of object<Psr\Http\Message\ServerRequestInterface>. It seems like you assume a child interface of the interface Psr\Http\Message\RequestInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
57
58 5
        return $next($request, $response);
59
    }
60
61
    /**
62
     * @param RequestInterface $request
63
     * @param Response         $response
64
     *
65
     * @return Response
66
     */
67 6
    public function handle(ServerRequestInterface $request, Response $response)
68
    {
69 6
        $server = $request->getServerParams();
70 6
        $server['REQUEST_METHOD'] = $request->getMethod();
71 6
        $server['REQUEST_URI'] = $request->getUri()->getPath();
72
        $globals = [
73 6
            '_GET' => $request->getQueryParams(),
74 6
            '_POST' => $request->getParsedBody()
75
        ];
76 6
        $req = $this->router->match($globals, $server);
77 6
        $resourceObject = $this->resource->{$req->method}->uri($req->path)->withQuery($req->query)->eager->request();
78 5
        $response = $this->toPsr7Response($response, $resourceObject);
79
80 5
        return $response;
81
    }
82
83
    /**
84
     * @param Response       $response
85
     * @param ResourceObject $resourceObject
86
     *
87
     * @return Response
88
     */
89 5
    private function toPsr7Response(Response $response, ResourceObject $resourceObject)
90
    {
91 5
        $bodyString = (string) $resourceObject;
92
        /** @var $response Response */
93 5
        $response = $response->withStatus($resourceObject->code);
94 5
        foreach ($resourceObject->headers as $name => $value) {
95 4
            $response = $response->withHeader($name, $value);
96
        }
97 5
        $stream = $this->render->toStream($bodyString);
98 5
        $response = $response->withBody(new Stream($stream));
99
100 5
        return $response;
101
    }
102
}
103