Completed
Push — 1.x ( 9371e0...47e006 )
by Akihito
9s
created

HalRenderer::updateHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the BEAR.Resource package
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Package\Provide\Representation;
8
9
use BEAR\Resource\AbstractUri;
10
use BEAR\Resource\Annotation\Link;
11
use BEAR\Resource\RenderInterface;
12
use BEAR\Resource\RequestInterface;
13
use BEAR\Resource\ResourceObject;
14
use BEAR\Resource\Uri;
15
use BEAR\Sunday\Extension\Router\RouterInterface;
16
use Doctrine\Common\Annotations\Reader;
17
use Nocarrier\Hal;
18
19
/**
20
 * HAL(Hypertext Application Language) renderer
21
 */
22
class HalRenderer implements RenderInterface
23
{
24
    /**
25
     * @var Reader
26
     */
27
    private $reader;
28
29
    /**
30
     * @var RouterInterface
31
     */
32
    private $router;
33
34
    /**
35
     * @param Reader          $reader
36
     * @param RouterInterface $router
37
     */
38 8
    public function __construct(Reader $reader, RouterInterface $router)
39
    {
40 8
        $this->reader = $reader;
41 8
        $this->router = $router;
42 8
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 7
    public function render(ResourceObject $ro)
48
    {
49 7
        list($ro, $body) = $this->valuate($ro);
50 7
        $method = 'on' . ucfirst($ro->uri->method);
51 7
        $hasMethod = method_exists($ro, $method);
52 7
        if (! $hasMethod) {
53 1
            $ro->view = ''; // OPTIONS request no view
54
55 1
            return '';
56
        }
57 6
        $annotations = ($hasMethod) ? $this->reader->getMethodAnnotations(new \ReflectionMethod($ro, $method)) : [];
58
        /* @var $annotations Link[] */
59
        /* @var $ro ResourceObject */
60 6
        $hal = $this->getHal($ro->uri, $body, $annotations);
61 6
        $ro->view = $hal->asJson(true) . PHP_EOL;
62 6
        $this->updateHeaders($ro);
63
64 6
        return $ro->view;
65
    }
66
67
    /**
68
     * @param \BEAR\Resource\ResourceObject $ro
69
     */
70 3
    private function valuateElements(ResourceObject &$ro)
71
    {
72 3
        foreach ($ro->body as $key => &$element) {
73 3
            if ($element instanceof RequestInterface) {
74 1
                unset($ro->body[$key]);
75 1
                $view = $this->render($element());
76 3
                $ro->body['_embedded'][$key] = json_decode($view);
77
            }
78
        }
79 3
    }
80
81
    /**
82
     * @param Uri   $uri
83
     * @param array $body
84
     * @param array $annotations
85
     *
86
     * @return Hal
87
     */
88 6
    private function getHal(AbstractUri $uri, array $body, array $annotations)
89
    {
90 6
        $query = $uri->query ? '?' . http_build_query($uri->query) : '';
91 6
        $path = $uri->path . $query;
92 6
        $selfLink = $this->getReverseMatchedLink($path);
93 6
        $hal = new Hal($selfLink, $body);
94 6
        $this->getHalLink($body, $annotations, $hal);
95
96 6
        return $hal;
97
    }
98
99
    /**
100
     * @param string $uri
101
     *
102
     * @return mixed
103
     */
104 6
    private function getReverseMatchedLink($uri)
105
    {
106 6
        $urlParts = parse_url($uri);
107 6
        $routeName = $urlParts['path'];
108 6
        isset($urlParts['query']) ? parse_str($urlParts['query'], $value) : $value = [];
109 6
        if ($value === []) {
110 3
            return $uri;
111
        }
112 4
        $reverseUri = $this->router->generate($routeName, (array) $value);
113 4
        if (is_string($reverseUri)) {
114 1
            return $reverseUri;
115
        }
116
117 3
        return $uri;
118
    }
119
120
    /**
121
     * @param ResourceObject $ro
122
     *
123
     * @return array [ResourceObject, array]
124
     */
125 7
    private function valuate(ResourceObject $ro)
126
    {
127
        // evaluate all request in body.
128 7
        if (is_array($ro->body)) {
129 3
            $this->valuateElements($ro);
130
        }
131
        // HAL
132 7
        $body = $ro->body ?: [];
133 7
        if (is_scalar($body)) {
134 1
            $body = ['value' => $body];
135
136 1
            return [$ro, $body];
137
        }
138
139 6
        return[$ro, (array) $body];
140
    }
141
142
    /**
143
     * @param array $body
144
     * @param array $links
145
     * @param Hal   $hal
146
     *
147
     * @internal param Uri $uri
148
     */
149 6
    private function getHalLink(array $body, array $links, Hal $hal)
150
    {
151 6
        foreach ($links as $link) {
152 3
            if (! $link instanceof Link) {
153 1
                continue;
154
            }
155 3
            $uri = uri_template($link->href, $body);
156 3
            $reverseUri = $this->getReverseMatchedLink($uri);
157 3
            $hal->addLink($link->rel, $reverseUri);
158
        }
159 6
    }
160
161
    /**
162
     * @param ResourceObject $ro
163
     */
164 6
    private function updateHeaders(ResourceObject $ro)
165
    {
166 6
        $ro->headers['content-type'] = 'application/hal+json';
167 6
        if (isset($ro->headers['Location'])) {
168 1
            $ro->headers['Location'] = $this->getReverseMatchedLink($ro->headers['Location']);
169
        }
170 6
    }
171
}
172