Passed
Push — master ( b8b2bd...fcde1f )
by Alex
02:15
created

Gateway::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Nxmad\Larapay\Abstracts;
4
5
use RuntimeException;
6
use Illuminate\Config\Repository;
7
use Nxmad\Larapay\Models\Transaction;
0 ignored issues
show
Bug introduced by
The type Nxmad\Larapay\Models\Transaction 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
use Nxmad\Larapay\Contracts\Gateway as GatewayContract;
9
10
abstract class Gateway implements GatewayContract
11
{
12
    /**
13
     * The list of aliases for payment gateway.
14
     *
15
     * @var array
16
     */
17
    protected $aliases = [];
18
19
    /**
20
     * The list of required request attributes.
21
     *
22
     * @var array
23
     */
24
    protected $required = [];
25
26
    /**
27
     * The general settings for payment processor, like secret key or merchant id.
28
     *
29
     * @var Repository
30
     */
31
    protected $config;
32
33
    /**
34
     * The temporary settings for payment processor, actual only in for this request, like payment description, etc.
35
     *
36
     * @var Repository
37
     */
38
    protected $custom;
39
40
    /**
41
     * Payment process method.
42
     * Possible values are below.
43
     *
44
     * @var string
45
     */
46
    protected $method = self::LARAPAY_GET_REDIRECT;
47
48
    /**
49
     * Classic redirection with GET parameters.
50
     */
51
    const LARAPAY_GET_REDIRECT = 'GET';
52
53
    /**
54
     * Redirect with POST data for old gateways (using hack with form).
55
     */
56
    const LARAPAY_POST_REDIRECT = 'POST';
57
58
    /**
59
     * No redirect method (e.g. for card payments)
60
     */
61
    const LARAPAY_NO_REDIRECT = '_';
62
63
    /**
64
     * Sign outcome request (insert request signature in request parameters).
65
     *
66
     * @param array $data
67
     *
68
     * @return string
69
     */
70
    abstract public function sign(array $data): string;
71
72
    /**
73
     * Gateway constructor.
74
     *
75
     * @param array $config
76
     */
77
    public function __construct(array $config = [])
78
    {
79
        $this->custom = new Repository;
80
        $this->config = new Repository($config);
81
    }
82
83
    /**
84
     * Process payment.
85
     *
86
     * @param Transaction $transaction
87
     *
88
     * @return mixed
89
     */
90
    public function interact(Transaction $transaction)
91
    {
92
        $this->prepare($transaction);
93
        $this->fill([
94
            'amount'      => $transaction->getAmount(),
95
            'description' => $transaction->getDescription(),
96
            'id'          => $transaction->getPrimaryValue(),
97
        ]);
98
99
        $this->signature = $this->sign($this->custom->all());
0 ignored issues
show
Bug Best Practice introduced by
The property signature does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
100
101
        foreach ($this->required as $field) {
102
            if (! $this->custom->has($this->getAlias($field))) {
103
                throw new RuntimeException("Required field [{$field}] is not presented.");
104
            }
105
        }
106
107
        if ($this->method == self::LARAPAY_NO_REDIRECT) {
108
            return $this->customBehavior();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->customBehavior() targeting Nxmad\Larapay\Abstracts\Gateway::customBehavior() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
The call to Nxmad\Larapay\Abstracts\Gateway::customBehavior() has too few arguments starting with transaction. ( Ignorable by Annotation )

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

108
            return $this->/** @scrutinizer ignore-call */ customBehavior();

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...
109
        }
110
111
        if ($this->method == self::LARAPAY_GET_REDIRECT) {
112
            return redirect()->away($this->getInteractionUrl() . '?' . http_build_query($this->custom->all()));
113
        }
114
115
        return view('larapay::form', [
116
            'method' => 'POST',
117
            'data'   => $this->custom->all(),
118
            'action' => $this->getInteractionUrl(),
119
        ]);
120
    }
121
122
    /**
123
     * Custom gateway logic instead redirect.
124
     * You can override this method in children class.
125
     *
126
     * @param Transaction $transaction
127
     */
128
    public function customBehavior(Transaction $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed. ( Ignorable by Annotation )

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

128
    public function customBehavior(/** @scrutinizer ignore-unused */ Transaction $transaction)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
129
    {
130
    }
131
132
    /**
133
     * Prepare Transaction.
134
     * You can override this method in children class.
135
     *
136
     * @param Transaction $transaction
137
     */
138
    public function prepare(Transaction $transaction)
0 ignored issues
show
Unused Code introduced by
The parameter $transaction is not used and could be removed. ( Ignorable by Annotation )

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

138
    public function prepare(/** @scrutinizer ignore-unused */ Transaction $transaction)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
139
    {
140
    }
141
142
    /**
143
     * Determine if this gateway needs redirect.
144
     *
145
     * @return bool
146
     */
147
    public function needRedirect()
148
    {
149
        return $this->method != self::LARAPAY_NO_REDIRECT;
150
    }
151
152
    /**
153
     * Fill custom parameters.
154
     *
155
     * @param array $parameters
156
     *
157
     * @return Gateway
158
     */
159
    public function fill(array $parameters): self
160
    {
161
        foreach ($parameters as $key => $value) {
162
            $this->set($key, $value);
163
        }
164
165
        return $this;
166
    }
167
168
    /**
169
     * Set custom field value respecting aliases.
170
     *
171
     * @param $field
172
     * @param $value
173
     *
174
     * @return self
175
     */
176
    public function set($field, $value = null): self
177
    {
178
        if (is_array($field)) {
179
            return $this->fill($field);
180
        }
181
182
        $this->custom->set($this->getAlias($field), $value);
183
184
        return $this;
185
    }
186
187
    /**
188
     * Magic set method.
189
     *
190
     * @param $name
191
     * @param $value
192
     *
193
     * @return Gateway
194
     */
195
    public function __set($name, $value)
196
    {
197
        return self::set(...func_get_args());
0 ignored issues
show
Bug Best Practice introduced by
The method Nxmad\Larapay\Abstracts\Gateway::set() is not static, but was called statically. ( Ignorable by Annotation )

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

197
        return self::/** @scrutinizer ignore-call */ set(...func_get_args());
Loading history...
Bug introduced by
func_get_args() is expanded, but the parameter $field of Nxmad\Larapay\Abstracts\Gateway::set() does not expect variable arguments. ( Ignorable by Annotation )

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

197
        return self::set(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
198
    }
199
200
    /**
201
     * Get custom field value respecting aliases.
202
     *
203
     * @param string  $field
204
     * @param mixed   $default
205
     *
206
     * @return mixed
207
     */
208
    public function get(string $field, $default = null)
209
    {
210
        return $this->custom->get($this->getAlias($field), $default);
211
    }
212
213
    /**
214
     * Magic get method.
215
     *
216
     * @param $name
217
     *
218
     * @return mixed
219
     */
220
    public function __get($name)
221
    {
222
        return self::get(...func_get_args());
0 ignored issues
show
Bug Best Practice introduced by
The method Nxmad\Larapay\Abstracts\Gateway::get() is not static, but was called statically. ( Ignorable by Annotation )

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

222
        return self::/** @scrutinizer ignore-call */ get(...func_get_args());
Loading history...
Bug introduced by
func_get_args() is expanded, but the parameter $field of Nxmad\Larapay\Abstracts\Gateway::get() does not expect variable arguments. ( Ignorable by Annotation )

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

222
        return self::get(/** @scrutinizer ignore-type */ ...func_get_args());
Loading history...
223
    }
224
225
    /**
226
     * Get slug of gateway based on class name.
227
     *
228
     * @return string
229
     */
230
    public function getSlug(): string
231
    {
232
        if (isset($this->slug)) {
0 ignored issues
show
Bug Best Practice introduced by
The property slug does not exist on Nxmad\Larapay\Abstracts\Gateway. Since you implemented __get, consider adding a @property annotation.
Loading history...
233
            return $this->slug;
234
        }
235
236
        return str_slug(array_last(explode('\\', get_class($this))));
237
    }
238
239
    /**
240
     * Determine if field has alias for payment gateway.
241
     * E.g., some gateways can accept description in $_GET parameter vendorPrefix_desc,
242
     * so we need conversion: 'description' => 'vendorPrefix_desc'.
243
     *
244
     * @param string $field
245
     *
246
     * @return string
247
     */
248
    protected function getAlias(string $field): string
249
    {
250
        if (isset($this->aliases[$field])) {
251
            return $this->aliases[$field];
252
        }
253
254
        return $field;
255
    }
256
}
257