Response::redirect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the Scrawler package.
4
 *
5
 * (c) Pranjal Pandey <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Scrawler\Http;
14
15
/**
16
 * Response class adds magic to the Symfony response.
17
 */
18
class Response extends \Symfony\Component\HttpFoundation\Response
19
{
20
    /**
21
     * Redirect to url.
22
     */
23
    public function redirect(string $url): RedirectResponse
24
    {
25
        return new RedirectResponse($url);
26
    }
27
28
    /**
29
     * Return json response.
30
     *
31
     * @param string|array<mixed> $data
32
     * @param array<mixed>        $headers
33
     */
34
    public function json(string|array $data, array $headers = []): Response
35
    {
36
        if (is_array($data)) {
0 ignored issues
show
introduced by
The condition is_array($data) is always true.
Loading history...
37
            $data = \Safe\json_encode($data);
38
        }
39
        $this->setContent($data);
40
        $this->headers->set('Content-Type', 'application/json');
41
        foreach ($headers as $key => $value) {
42
            $this->headers->set($key, $value);
43
        }
44
45
        return $this;
46
    }
47
}
48