Issues (31)

src/Abstracts/Gateway.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nxmad\Larapay\Abstracts;
6
7
use Illuminate\Config\Repository;
8
use Nxmad\Larapay\Models\Transaction;
9
use Nxmad\Larapay\Requests\PaymentRequest;
10
use Nxmad\Larapay\Requests\CallbackRequest;
11
use Illuminate\Http\Request as HttpRequest;
12
use Nxmad\Larapay\Contracts\Gateway as GatewayContract;
13
14
abstract class Gateway implements GatewayContract
15
{
16
    /**
17
     * The list of gateway settings.
18
     *
19
     * @var Repository
20
     */
21
    protected $config;
22
23
    /**
24
     * The list of gateway requests.
25
     *
26
     * @var array
27
     */
28
    protected $requests = [
29
        'payment' => PaymentRequest::class,
30
        'callback' => CallbackRequest::class,
31
    ];
32
33
    /**
34
     * Sign request.
35
     *
36
     * @param Request $request
37
     *
38
     * @return string
39
     */
40
    abstract public function sign(Request $request): string;
41
42
    /**
43
     * Gateway constructor.
44
     *
45
     * @param array $config
46
     */
47
    public function __construct(array $config = [])
48
    {
49
        $this->config = new Repository($config);
50
    }
51
52
    /**
53
     * Override gateway config values.
54
     *
55
     * @param array|string $key
56
     * @param mixed $value
57
     *
58
     * @return mixed
59
     */
60
    public function config($key, $value = null)
61
    {
62
        if (is_array($key)) {
63
            foreach ($key as $k => $v) {
64
                $this->config($k, $v);
65
            }
66
        } elseif (! is_null($value)) {
67
            return $this->config->set($key, $value);
0 ignored issues
show
Are you sure the usage of $this->config->set($key, $value) targeting Illuminate\Config\Repository::set() 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...
68
        }
69
70
        return $this->config->get($key);
71
    }
72
73
    /**
74
     * @param string $request
75
     * @return bool
76
     */
77
    public function hasRequest(string $request)
78
    {
79
        return array_key_exists($request, $this->requests);
80
    }
81
82
    /**
83
     * @param string $request
84
     * @return mixed|string
85
     */
86
    public function getRequest(string $request)
87
    {
88
        return $this->hasRequest($request) ? $this->requests[$request] : $request;
89
    }
90
91
    /**
92
     * @param array|string $name
93
     * @param null|Request $implementation
94
     *
95
     * @return self
96
     */
97
    public function addRequest($name, $implementation = null): self
98
    {
99
        if (is_array($name) && is_null($implementation)) {
100
            foreach ($name as $key => $value) {
101
                $this->addRequest($key, $value);
102
            }
103
104
            return $this;
105
        }
106
107
        $this->requests[$name] = $implementation;
108
109
        return $this;
110
    }
111
112
    /**
113
     * @param string $request
114
     * @param mixed $data
115
     *
116
     * @return mixed
117
     */
118
    public function call(string $request, $data = []): Request
119
    {
120
        $callable = $this->getRequest($request);
121
122
        /**
123
         * @var Request $request
124
         */
125
        $request = new $callable($this, $data);
126
127
        return $request;
128
    }
129
130
    /**
131
     * @param $transaction
132
     *
133
     * @return mixed
134
     */
135
    public function payment(Transaction $transaction)
136
    {
137
        return $this->call('payment', [
138
            Request::AMOUNT => $transaction->getAmount(),
139
            Request::ID => $transaction->getPrimaryValue(),
140
            Request::DESCRIPTION => $transaction->getDescription(),
141
        ]);
142
    }
143
144
    /**
145
     * @param HttpRequest $request
146
     *
147
     * @return Request
148
     */
149
    public function callback(HttpRequest $request)
150
    {
151
        return $this->call('callback', $request);
152
    }
153
}
154