Passed
Push — master ( 753ee1...d2633a )
by mahdi
02:17
created

RedirectionForm::toJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 5
c 2
b 1
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Shetabit\Multipay;
4
5
use Exception;
6
use JsonSerializable;
7
use Illuminate\Support\Facades\Response;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Facades\Response 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...
8
9
class RedirectionForm implements JsonSerializable
10
{
11
    /**
12
     * Form's method
13
     *
14
     * @var string
15
     */
16
    protected $method = 'POST';
17
18
    /**
19
     * Form's inputs
20
     *
21
     * @var array
22
     */
23
    protected $inputs = [];
24
25
    /**
26
     * Form's action
27
     *
28
     * @var string
29
     */
30
    protected $action;
31
32
    /**
33
     * Redirection form view's path
34
     *
35
     * @var string
36
     */
37
    protected static $viewPath;
38
39
    /**
40
     * The callable function that renders the given view
41
     *
42
     * @var callable
43
     */
44
    protected static $viewRenderer;
45
46
    /**
47
     * Redirection form constructor.
48
     *
49
     * @param string $action
50
     * @param array $inputs
51
     * @param string $method
52
     */
53
    public function __construct(string $action, array $inputs = [], string $method = 'POST')
54
    {
55
        $this->action = $action;
56
        $this->inputs = $inputs;
57
        $this->method = $method;
58
    }
59
60
    /**
61
     * Retrieve default view path.
62
     *
63
     * @return string
64
     */
65
    public static function getDefaultViewPath() : string
66
    {
67
        return dirname(__DIR__).'/resources/views/redirect-form.php';
68
    }
69
70
    /**
71
     * Set view path
72
     *
73
     * @param string $path
74
     *
75
     * @return void
76
     */
77
    public static function setViewPath(string $path)
78
    {
79
        static::$viewPath = $path;
80
    }
81
82
    /**
83
     * Retrieve view path.
84
     *
85
     * @return string
86
     */
87
    public static function getViewPath() : string
88
    {
89
        return static::$viewPath ?? static::getDefaultViewPath();
90
    }
91
92
    /**
93
     * Set view renderer
94
     *
95
     * @param callable $renderer
96
     */
97
    public static function setViewRenderer(callable $renderer)
98
    {
99
        static::$viewRenderer = $renderer;
100
    }
101
102
    /**
103
     * Retrieve default view renderer.
104
     *
105
     * @return callable
106
     */
107
    protected function getDefaultViewRenderer() : callable
108
    {
109
        return function (string $view, string $action, array $inputs, string $method) {
110
            ob_start();
111
112
            require($view);
113
114
            return ob_get_clean();
115
        };
116
    }
117
118
    /**
119
     * Retrieve associated method.
120
     *
121
     * @return string
122
     */
123
    public function getMethod() : string
124
    {
125
        return $this->method;
126
    }
127
128
    /**
129
     * Retrieve associated inputs
130
     *
131
     * @return array
132
     */
133
    public function getInputs() : array
134
    {
135
        return $this->inputs;
136
    }
137
138
    /**
139
     * Retrieve associated action
140
     *
141
     * @return string
142
     */
143
    public function getAction() : string
144
    {
145
        return $this->action;
146
    }
147
148
    /**
149
     * Alias for getAction method.
150
     *
151
     * @alias getAction
152
     *
153
     * @return string
154
     */
155
    public function getUrl() : string
156
    {
157
        return $this->getAction();
158
    }
159
160
    /**
161
     * Render form.
162
     *
163
     * @return string
164
     */
165
    public function render() : string
166
    {
167
        $data = [
168
            "view" => static::getViewPath(),
169
            "action" => $this->getAction(),
170
            "inputs" => $this->getInputs(),
171
            "method" => $this->getMethod(),
172
        ];
173
174
        $renderer = is_callable(static::$viewRenderer) ? static::$viewRenderer : $this->getDefaultViewRenderer();
175
176
        return call_user_func_array($renderer, $data);
177
    }
178
179
    /**
180
     * Retrieve JSON format of redirection form.
181
     *
182
     * @param $options
183
     *
184
     * @throws Exception
185
     * @return string
186
     */
187
    public function toJson($options = JSON_UNESCAPED_UNICODE)
188
    {
189
        $this->sendJsonHeader();
190
191
        $json = json_encode($this, $options);
192
193
        if (json_last_error() != JSON_ERROR_NONE) {
194
            throw new Exception(json_last_error_msg());
195
        }
196
197
        return $json;
198
    }
199
200
    /**
201
     * Retrieve JSON format of redirection form.
202
     *
203
     * @return string
204
     */
205
    public function toString() : string
206
    {
207
        return $this->render();
208
    }
209
210
    /**
211
     * Serialize to json
212
     *
213
     * @return mixed
214
     */
215
    public function jsonSerialize()
216
    {
217
        return [
218
            'action' => $this->getAction(),
219
            'inputs' => $this->getInputs(),
220
            'method' => $this->getMethod(),
221
        ];
222
    }
223
224
    /**
225
     * Retrieve string format of redirection form.
226
     *
227
     * @return string
228
     */
229
    public function __toString()
230
    {
231
        return $this->toString();
232
    }
233
234
    /**
235
     * Send application/json header
236
     *
237
     * @return void
238
     */
239
    private function sendJsonHeader()
240
    {
241
        header('Content-Type: application/json');
242
    }
243
}
244