Completed
Push — 1.x ( 64141c...67746d )
by Akihito
14s queued 11s
created

Provide/Representation/CreatedResourceRenderer.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * This file is part of the BEAR.Package package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package\Provide\Representation;
8
9
use BEAR\Package\Exception\LocationHeaderRequestException;
10
use BEAR\Resource\RenderInterface;
11
use BEAR\Resource\ResourceInterface;
12
use BEAR\Resource\ResourceObject;
13
use BEAR\Sunday\Extension\Router\RouterInterface;
14
15
/**
16
 * 201 CreatedResource renderer
17
 */
18
class CreatedResourceRenderer implements RenderInterface
19
{
20
    /**
21
     * @var RouterInterface
22
     */
23
    private $router;
24
25
    /**
26
     * @var ResourceInterface
27
     */
28
    private $resource;
29
30 3
    public function __construct(RouterInterface $router, ResourceInterface $resource)
31
    {
32 3
        $this->router = $router;
33 3
        $this->resource = $resource;
34 3
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 4
    public function render(ResourceObject $ro)
40
    {
41 4
        $url = parse_url($ro->uri);
42 4
        $locationUri = sprintf('%s://%s%s', $url['scheme'], $url['host'], $ro->headers['Location']);
43
        try {
44 4
            $locatedResource = $this->resource->uri($locationUri)();
45
            /* @var $locatedResource ResourceObject */
46 1
        } catch (\Exception $e) {
47 1
            $ro->code = 500;
48 1
            $ro->view = '';
49 1
            throw new LocationHeaderRequestException($locationUri, 0, $e);
50
        }
51 3
        $this->updateHeaders($ro);
52
53 3
        return $locatedResource->toString();
54
    }
55
56 3
    private function getReverseMatchedLink(string $uri) : string
57
    {
58 3
        $urlParts = parse_url($uri);
59 3
        $routeName = $urlParts['path'];
60 3
        isset($urlParts['query']) ? parse_str($urlParts['query'], $value) : $value = [];
61 3
        if ($value === []) {
62 1
            return $uri;
63
        }
64 2
        $reverseUri = $this->router->generate($routeName, $value);
0 ignored issues
show
It seems like $value can also be of type null; however, BEAR\Sunday\Extension\Ro...erInterface::generate() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65 2
        if (\is_string($reverseUri)) {
66 1
            return $reverseUri;
67
        }
68
69 1
        return $uri;
70
    }
71
72 3
    private function updateHeaders(ResourceObject $ro)
73
    {
74 3
        $ro->headers['content-type'] = 'application/hal+json';
75 3
        if (isset($ro->headers['Location'])) {
76 3
            $ro->headers['Location'] = $this->getReverseMatchedLink($ro->headers['Location']);
77
        }
78 3
    }
79
}
80