Completed
Push — 1.x ( abde83...7c6f23 )
by Akihito
13s
created

CreatedResourceRenderer::render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
crap 2
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
use Doctrine\Common\Annotations\Reader;
15
16
/**
17
 * 201 @CreatedResource renderer
18
 */
19
class CreatedResourceRenderer implements RenderInterface
20
{
21
    /**
22
     * @var Reader
23
     */
24
    private $reader;
25
26
    /**
27
     * @var RouterInterface
28
     */
29
    private $router;
30
31
    /**
32
     * @var ResourceInterface
33
     */
34
    private $resource;
35
36
    /**
37
     * @param Reader          $reader
38
     * @param RouterInterface $router
39
     */
40 5
    public function __construct(Reader $reader, RouterInterface $router, ResourceInterface $resource)
41
    {
42 5
        $this->reader = $reader;
43 5
        $this->router = $router;
44 5
        $this->resource = $resource;
45 5
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 3
    public function render(ResourceObject $ro)
51
    {
52 3
        $url = parse_url($ro->uri);
53 3
        $locationUri = sprintf('%s://%s%s', $url['scheme'], $url['host'], $ro->headers['Location']);
54
        try {
55 3
            $locatedResource = $this->resource->uri($locationUri)();
56
            /* @var $locatedResource ResourceObject */
57 1
        } catch (\Exception $e) {
58 1
            $ro->code = 500;
59 1
            $ro->view = '';
60 1
            throw new LocationHeaderRequestException($locationUri, 0, $e);
61
        }
62 2
        $this->updateHeaders($ro);
63
64 2
        return $locatedResource->toString();
65
    }
66
67
    /**
68
     * @return mixed
69
     */
70 2
    private function getReverseMatchedLink(string $uri)
71
    {
72 2
        $urlParts = parse_url($uri);
73 2
        $routeName = $urlParts['path'];
74 2
        isset($urlParts['query']) ? parse_str($urlParts['query'], $value) : $value = [];
75 2
        if ($value === []) {
76 1
            return $uri;
77
        }
78 1
        $reverseUri = $this->router->generate($routeName, (array) $value);
79 1
        if (is_string($reverseUri)) {
80
            return $reverseUri;
81
        }
82
83 1
        return $uri;
84
    }
85
86 2
    private function updateHeaders(ResourceObject $ro)
87
    {
88 2
        $ro->headers['content-type'] = 'application/hal+json';
89 2
        if (isset($ro->headers['Location'])) {
90 2
            $ro->headers['Location'] = $this->getReverseMatchedLink($ro->headers['Location']);
91
        }
92 2
    }
93
}
94