Passed
Pull Request — master (#47)
by
unknown
02:13
created

RedirectionForm::getDefaultViewRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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