Completed
Pull Request — 2.x (#2288)
by Guilhem
17:20
created

View::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2.0185
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 9
    public static function create($data = null, ?int $statusCode = null, array $headers = []): self
35
    {
36 9
        return new static($data, $statusCode, $headers);
37
    }
38
39 1
    public static function createRedirect(string $url, int $statusCode = Response::HTTP_FOUND, array $headers = []): self
40
    {
41 1
        $view = static::create(null, $statusCode, $headers);
42 1
        $view->setLocation($url);
43
44 1
        return $view;
45
    }
46
47 2
    public static function createRouteRedirect(
48
        string $route,
49
        array $parameters = [],
50
        int $statusCode = Response::HTTP_FOUND,
51
        array $headers = []
52
    ): self {
53 2
        $view = static::create(null, $statusCode, $headers);
54 2
        $view->setRoute($route);
55 2
        $view->setRouteParameters($parameters);
56
57 2
        return $view;
58
    }
59
60 58
    public function __construct($data = null, ?int $statusCode = null, array $headers = [])
61
    {
62 58
        $this->setData($data);
63 58
        $this->setStatusCode($statusCode);
64
65 58
        if (!empty($headers)) {
66
            $this->getResponse()->headers->replace($headers);
67
        }
68 58
    }
69
70 58
    public function setData($data): self
71
    {
72 58
        $this->data = $data;
73
74 58
        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 2
    public function setHeaders(array $headers): self
85
    {
86 2
        $this->getResponse()->headers->replace($headers);
87
88 2
        return $this;
89
    }
90
91 58
    public function setStatusCode(?int $code): self
92
    {
93 58
        if (null !== $code) {
94 14
            $this->statusCode = $code;
95
        }
96
97 58
        return $this;
98
    }
99
100
    public function setContext(Context $context): self
101
    {
102
        $this->context = $context;
103
104
        return $this;
105
    }
106
107 24
    public function setFormat(string $format): self
108
    {
109 24
        $this->format = $format;
110
111 24
        return $this;
112
    }
113
114 4
    public function setLocation(string $location): self
115
    {
116 4
        $this->location = $location;
117 4
        $this->route = null;
118
119 4
        return $this;
120
    }
121
122
    /**
123
     * Sets the route (implicitly removes the location).
124
     */
125 3
    public function setRoute(string $route): self
126
    {
127 3
        $this->route = $route;
128 3
        $this->location = null;
129
130 3
        return $this;
131
    }
132
133 3
    public function setRouteParameters(array $parameters): self
134
    {
135 3
        $this->routeParameters = $parameters;
136
137 3
        return $this;
138
    }
139
140
    public function setResponse(Response $response): self
141
    {
142
        $this->response = $response;
143
144
        return $this;
145
    }
146
147 43
    public function getData()
148
    {
149 43
        return $this->data;
150
    }
151
152 53
    public function getStatusCode(): ?int
153
    {
154 53
        return $this->statusCode;
155
    }
156
157
    public function getHeaders(): array
158
    {
159
        return $this->getResponse()->headers->all();
160
    }
161
162 29
    public function getFormat(): ?string
163
    {
164 29
        return $this->format;
165
    }
166
167 41
    public function getLocation(): ?string
168
    {
169 41
        return $this->location;
170
    }
171
172 43
    public function getRoute(): ?string
173
    {
174 43
        return $this->route;
175
    }
176
177 2
    public function getRouteParameters(): ?array
178
    {
179 2
        return $this->routeParameters;
180
    }
181
182 47
    public function getResponse(): Response
183
    {
184 47
        if (null === $this->response) {
185 47
            $this->response = new Response();
186
187 47
            if (null !== ($code = $this->getStatusCode())) {
188 11
                $this->response->setStatusCode($code);
189
            }
190
        }
191
192 47
        return $this->response;
193
    }
194
195 38
    public function getContext(): Context
196
    {
197 38
        if (null === $this->context) {
198 38
            $this->context = new Context();
199
        }
200
201 38
        return $this->context;
202
    }
203
}
204