Passed
Branch master (267be1)
by Eugene
03:10
created

ResourceResponseMiddlewareAbstract::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
namespace Staticus\Resources\Middlewares;
3
4
use Staticus\Diactoros\Response\FileContentResponse;
5
use Staticus\Diactoros\Response\FileUploadedResponse;
6
use Staticus\Diactoros\Response\ResourceDoResponse;
7
use Staticus\Middlewares\MiddlewareAbstract;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Staticus\Resources\ResourceDOInterface;
11
use Zend\Diactoros\Response\EmptyResponse;
12
use Zend\Diactoros\Response\JsonResponse;
13
14
abstract class ResourceResponseMiddlewareAbstract extends MiddlewareAbstract
15
{
16
    /**
17
     * @var ResourceDOInterface
18
     */
19
    protected $resourceDO;
20
    
21
    public function __construct(ResourceDOInterface $resourceDO)
22
    {
23
        $this->resourceDO = $resourceDO;
24
    }
25
26
    public function __invoke(
27
        ServerRequestInterface $request,
28
        ResponseInterface $response,
29
        callable $next = null
30
    )
31
    {
32
        parent::__invoke($request, $response, $next);
33
        if ($this->isSupportedResponse($response)) {
34
            $uri = $this->getUri($this->resourceDO);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
35
            $data = [
36
                'resource' => $this->resourceDO->toArray(),
37
                'uri' => $uri,
38
            ];
39
            if (!empty($data)) {
40
                $headers = $response->getHeaders();
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
41
                $headers['Content-Type'] = 'application/json';
42
43
                // here is relative link without host url
44
                $headers['Link'] = '<' . rawurlencode($uri) . '>; rel="canonical"';
45
                $response = $this->getResponseObject($data, $response->getStatusCode(), $headers);
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
46
            }
47
        }
48
49
        return $next($request, $response);
50
    }
51
52
    /**
53
     * @return ResponseInterface
54
     */
55
    protected function getResponseObject($data, $status, array $headers)
56
    {
57
        $return = new JsonResponse($data, $status, $headers);
58
59
        return $return;
60
    }
61
62
    protected function getUri(ResourceDOInterface $resourceDO)
63
    {
64
        $uri = $resourceDO->getName() . '.' . $resourceDO->getType();
1 ignored issue
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
65
        $query = [];
66
        if (ResourceDOInterface::DEFAULT_VARIANT !== $resourceDO->getVariant()) {
67
            $query['var'] = $resourceDO->getVariant();
68
        }
69
        if (ResourceDOInterface::DEFAULT_VERSION !== $resourceDO->getVersion()) {
70
            $query['v'] = $resourceDO->getVersion();
71
        }
72
        $query = http_build_query($query);
73
        if ($query) {
74
            $uri .= '?' . $query;
75
        }
76
77
        return $uri;
78
    }
79
80
    /**
81
     * @param ResponseInterface $response
82
     * @return bool
83
     */
84
    protected function isSupportedResponse(ResponseInterface $response)
85
    {
86
        return $response instanceof EmptyResponse
87
        || $response instanceof FileContentResponse
88
        || $response instanceof FileUploadedResponse
89
        || $response instanceof ResourceDoResponse;
90
    }
91
}
92