Redirect::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nxmad\Larapay\Units;
6
7
class Redirect
8
{
9
    const GET = 'GET';
10
    const POST = 'POST';
11
12
    /**
13
     * @var string
14
     */
15
    public $url;
16
17
    /**
18
     * @var string
19
     */
20
    public $method;
21
22
    /**
23
     * @var array
24
     */
25
    public $query = [];
26
27
    /**
28
     * Redirect constructor.
29
     *
30
     * @param $url
31
     * @param string $method
32
     * @param array $query
33
     */
34
    public function __construct($url, $method = self::GET, $query = [])
35
    {
36
        $this->url = $url;
37
        $this->query = $query;
38
        $this->method = $method;
39
    }
40
41
    /**
42
     * Convert to string.
43
     *
44
     * @return string
45
     */
46
    public function __toString()
47
    {
48
        return $this->toJson();
49
    }
50
51
    /**
52
     * Convert to JSON.
53
     *
54
     * @return false|string
55
     */
56
    public function toJson()
57
    {
58
        return json_encode($this->raw());
59
    }
60
61
    /**
62
     * Convert to response.
63
     *
64
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
0 ignored issues
show
Bug introduced by
The type Illuminate\View\View was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
65
     */
66
    public function toResponse()
67
    {
68
        if ($this->method === self::GET) {
69
            return redirect($this->raw()->url);
0 ignored issues
show
Bug introduced by
The function redirect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
            return /** @scrutinizer ignore-call */ redirect($this->raw()->url);
Loading history...
70
        }
71
72
        return view('larapay::form', [
0 ignored issues
show
Bug introduced by
The function view was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        return /** @scrutinizer ignore-call */ view('larapay::form', [
Loading history...
73
            'method' => 'POST',
74
            'action' => $this->url,
75
            'data'   => $this->query,
76
        ]);
77
    }
78
79
    /**
80
     * Get redirect summary.
81
     *
82
     * @return object
83
     */
84
    public function raw()
85
    {
86
        $url = $this->url;
87
88
        if ($this->method === self::GET) {
89
            $url = $this->url . '?' . http_build_query($this->query);
90
        }
91
92
        return (object) [
93
            'url' => $url,
94
            'query' => $this->query,
95
            'method' => $this->method,
96
            'url_original' => $this->url,
97
        ];
98
    }
99
}
100