Passed
Push — master ( 86569f...5a8755 )
by mahdi
02:33
created

RedirectionForm::setViewRenderer()   A

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 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Shetabit\Multipay;
4
5
use JsonSerializable;
6
7
class RedirectionForm implements JsonSerializable
8
{
9
    /**
10
     * Form's method
11
     *
12
     * @var string
13
     */
14
    protected $method = 'POST';
15
16
    /**
17
     * Form's inputs
18
     *
19
     * @var array
20
     */
21
    protected $inputs = [];
22
23
    /**
24
     * Form's action
25
     *
26
     * @var string
27
     */
28
    protected $action;
29
30
    /**
31
     * Redirection form view's path
32
     *
33
     * @var string
34
     */
35
    protected static $viewPath;
36
37
    /**
38
     * The callable function that renders the given view
39
     *
40
     * @var callable
41
     */
42
    protected static $viewRenderer;
43
44
    /**
45
     * Redirection form constructor.
46
     *
47
     * @param string $action
48
     * @param array $inputs
49
     * @param string $method
50
     */
51
    public function __construct(string $action, array $inputs = [], string $method = 'POST')
52
    {
53
        $this->action = $action;
54
        $this->inputs = $inputs;
55
        $this->method = $method;
56
    }
57
58
    /**
59
     * Retrieve default view path.
60
     *
61
     * @return string
62
     */
63
    public static function getDefaultViewPath() : string
64
    {
65
        return dirname(__DIR__).'/resources/views/redirect-form.php';
66
    }
67
68
    /**
69
     * Set view path
70
     *
71
     * @param string $path
72
     *
73
     * @return void
74
     */
75
    public static function setViewPath(string $path)
76
    {
77
        static::$viewPath = $path;
78
    }
79
80
    /**
81
     * Retrieve view path.
82
     *
83
     * @return string
84
     */
85
    public static function getViewPath() : string
86
    {
87
        return static::$viewPath ?? static::getDefaultViewPath();
88
    }
89
90
    /**
91
     * Set view renderer
92
     *
93
     * @param callable $renderer
94
     */
95
    public static function setViewRenderer(callable $renderer)
96
    {
97
        static::$viewRenderer = $renderer;
98
    }
99
100
    /**
101
     * Retrieve default view renderer.
102
     *
103
     * @param string $method
104
     * @param array $inputs
105
     * @param string $action
106
     * @param string $view
107
     *
108
     * @return callable
109
     */
110
    protected function getDefaultViewRenderer(string $view, string $method, array $inputs, string $action) : callable
111
    {
112
        return function() use($view, $method, $inputs, $action) {
113
            ob_start();
114
115
            require($view);
116
117
            return ob_get_clean();
118
        };
119
    }
120
121
    /**
122
     * Retrieve associated method.
123
     *
124
     * @return string
125
     */
126
    public function getMethod() : string
127
    {
128
        return $this->method;
129
    }
130
131
    /**
132
     * Retrieve associated inputs
133
     *
134
     * @return array
135
     */
136
    public function getInputs() : array
137
    {
138
        return $this->inputs;
139
    }
140
141
    /**
142
     * Retrieve associated action
143
     *
144
     * @return string
145
     */
146
    public function getAction() : string
147
    {
148
        return $this->action;
149
    }
150
151
    /**
152
     * Alias for getAction method.
153
     *
154
     * @alias getAction
155
     *
156
     * @return string
157
     */
158
    public function getUrl() : string
159
    {
160
        return $this->getAction();
161
    }
162
163
    /**
164
     * Render form.
165
     *
166
     * @return string
167
     */
168
    public function render() : string
169
    {
170
        $data = [
171
            'method' => $this->getMethod(),
172
            'inputs' => $this->getInputs(),
173
            'action' => $this->getAction(),
174
            'view'   => static::getViewPath(),
175
        ];
176
177
        $renderer = is_callable(static::$viewRenderer) ? static::$viewRenderer : $this->getDefaultViewRenderer();
0 ignored issues
show
Bug introduced by
The call to Shetabit\Multipay\Redire...etDefaultViewRenderer() has too few arguments starting with view. ( Ignorable by Annotation )

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

177
        $renderer = is_callable(static::$viewRenderer) ? static::$viewRenderer : $this->/** @scrutinizer ignore-call */ getDefaultViewRenderer();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
178
179
        return call_user_func_array($renderer, $data);
180
    }
181
182
    /**
183
     * Retrieve JSON format of redirection form.
184
     *
185
     * @param $options
186
     *
187
     * @return string
188
     */
189
    public function toJson($options = JSON_UNESCAPED_UNICODE) : string
190
    {
191
        return json_encode($this, $options);
192
    }
193
194
    /**
195
     * Retrieve JSON format of redirection form.
196
     *
197
     * @return string
198
     */
199
    public function toString() : string
200
    {
201
        return $this->render();
202
    }
203
204
    /**
205
     * Serialize to json
206
     *
207
     * @return mixed
208
     */
209
    public function jsonSerialize()
210
    {
211
        return [
212
            'method' => $this->getMethod(),
213
            'inputs' => $this->getInputs(),
214
            'action' => $this->getAction(),
215
        ];
216
    }
217
218
    /**
219
     * Retrieve string format of redirection form.
220
     *
221
     * @return string
222
     */
223
    public function __toString()
224
    {
225
        return $this->toString();
226
    }
227
}
228