Completed
Pull Request — 2.x (#2227)
by
unknown
08:36
created

View::setRouteParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\View;
13
14
use FOS\RestBundle\Context\Context;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * Default View implementation.
19
 *
20
 * @author Johannes M. Schmitt <[email protected]>
21
 * @author Lukas K. Smith <[email protected]>
22
 */
23
final class View
24
{
25
    private $data;
26
    private $statusCode;
27
    private $format;
28
    private $location;
29
    private $route;
30
    private $routeParameters;
31
    private $context;
32
    private $response;
33
34
    public static function create($data = null, ?int $statusCode = null, array $headers = []): self
35
    {
36
        return new static($data, $statusCode, $headers);
37
    }
38
39
    public static function createRedirect(string $url, int $statusCode = Response::HTTP_FOUND, array $headers = []): self
40
    {
41
        $view = static::create(null, $statusCode, $headers);
42
        $view->setLocation($url);
43
44
        return $view;
45
    }
46
47
    public static function createRouteRedirect(
48
        string $route,
49
        array $parameters = [],
50
        int $statusCode = Response::HTTP_FOUND,
51
        array $headers = []
52
    ): self {
53
        $view = static::create(null, $statusCode, $headers);
54
        $view->setRoute($route);
55
        $view->setRouteParameters($parameters);
56
57
        return $view;
58
    }
59
60
    public function __construct($data = null, ?int $statusCode = null, array $headers = [])
61
    {
62
        $this->setData($data);
63
        $this->setStatusCode($statusCode);
64
65
        if (!empty($headers)) {
66
            $this->getResponse()->headers->replace($headers);
67
        }
68
    }
69
70
    public function setData($data): self
71
    {
72
        $this->data = $data;
73
74
        return $this;
75
    }
76
77
    public function setHeader(string $name, string $value): self
78
    {
79
        $this->getResponse()->headers->set($name, $value);
80
81
        return $this;
82
    }
83
84
    public function setHeaders(array $headers): self
85
    {
86
        $this->getResponse()->headers->replace($headers);
87
88
        return $this;
89
    }
90
91
    public function setStatusCode(?int $code): self
92
    {
93 13
        if (null !== $code) {
94
            $this->statusCode = $code;
95 13
        }
96
97
        return $this;
98
    }
99
100
    public function setContext(Context $context): self
101
    {
102
        $this->context = $context;
103
104 1
        return $this;
105
    }
106 1
107 1
    public function setFormat(string $format): self
108
    {
109 1
        $this->format = $format;
110
111
        return $this;
112
    }
113
114
    public function setLocation(string $location): self
115
    {
116
        $this->location = $location;
117
        $this->route = null;
118 3
119
        return $this;
120
    }
121
122
    /**
123
     * Sets the route (implicitly removes the location).
124 3
     */
125 3
    public function setRoute(string $route): self
126 3
    {
127
        $this->route = $route;
128 3
        $this->location = null;
129
130
        return $this;
131
    }
132
133
    public function setRouteParameters(array $parameters): self
134 95
    {
135
        $this->routeParameters = $parameters;
136 95
137 95
        return $this;
138 95
    }
139
140 95
    public function setResponse(Response $response): self
141
    {
142
        $this->response = $response;
143 95
144
        return $this;
145
    }
146
147
    public function getData()
148 95
    {
149
        return $this->data;
150 95
    }
151
152 95
    public function getStatusCode(): ?int
153
    {
154
        return $this->statusCode;
155
    }
156
157
    public function getHeaders(): array
158
    {
159
        return $this->getResponse()->headers->all();
160
    }
161
162 15
    public function getFormat(): ?string
163
    {
164 15
        return $this->format;
165 6
    }
166
167
    public function getLocation(): ?string
168 15
    {
169
        return $this->location;
170 15
    }
171
172
    public function getRoute(): ?string
173
    {
174
        return $this->route;
175
    }
176
177
    public function getRouteParameters(): ?array
178
    {
179
        return $this->routeParameters;
180
    }
181
182
    public function getResponse(): Response
183
    {
184
        if (null === $this->response) {
185
            $this->response = new Response();
186
187
            if (null !== ($code = $this->getStatusCode())) {
188
                $this->response->setStatusCode($code);
189 2
            }
190
        }
191 2
192
        return $this->response;
193 2
    }
194
195
    public function getContext(): Context
196
    {
197
        if (null === $this->context) {
198
            $this->context = new Context();
199
        }
200
201 95
        return $this->context;
202
    }
203
}
204