Completed
Push — master ( 46ebec...159811 )
by Gregorio
02:04
created

AbstractGateway::supports()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Gregoriohc\Moneta\Common;
4
5
use ArrayAccess;
6
use Gregoriohc\Moneta\Common\Concerns\Parametrizable;
7
use IteratorAggregate;
8
use JsonSerializable;
9
10
/**
11
 * @method bool supportsAuthorize()
12
 * @method bool supportsCapture()
13
 * @method bool supportsPurchase()
14
 * @method bool supportsRefund()
15
 * @method bool supportsVoid()
16
 * @method bool supportsCreatePaymentMethod()
17
 * @method bool supportsDeletePaymentMethod()
18
 * @method bool supportsUpdatePaymentMethod()
19
 * @method bool supportsAcceptNotification()
20
 * @property bool test_mode
21
 */
22
abstract class AbstractGateway implements GatewayInterface, ArrayAccess, IteratorAggregate, JsonSerializable
23
{
24
    use Parametrizable;
25
26
    private $requestMethods = [
27
        'authorize',
28
        'capture',
29
        'purchase',
30
        'refund',
31
        'void',
32
        'createPaymentMethod',
33
        'deletePaymentMethod',
34
        'updatePaymentMethod',
35
    ];
36
37
    private $webhookMethods = [
38
        'acceptNotification',
39
    ];
40
41
    /**
42
     * @var mixed
43
     */
44
    protected $client;
45
46
    /**
47
     * Create a new gateway instance
48
     *
49
     * @param array $parameters
50
     */
51 39
    public function __construct($parameters = [])
52
    {
53 39
        $this->bootParameters($parameters);
54 39
        $this->bootClient();
55 39
    }
56
57
    /**
58
     * @return array
59
     */
60 39
    public function defaultParameters()
61
    {
62
        return [
63 39
            'test_mode' => false,
64
        ];
65
    }
66
67
    /**
68
     * @return array
69
     */
70 27
    public function parametersValidationRules()
71
    {
72
        return [
73 27
            'test_mode' => 'required',
74
        ];
75
    }
76
77
    /**
78
     * Get the short name of the Gateway
79
     *
80
     * @return string
81
     */
82 3
    public function shortName()
83
    {
84 3
        return class_basename($this);
85
    }
86
87
    /**
88
     * @return boolean
89
     */
90 3
    public function isInTestMode()
91
    {
92 3
        return $this->test_mode;
93
    }
94
95
    /**
96
     * @return mixed
97
     */
98 3
    public function client()
99
    {
100 3
        return $this->client;
101
    }
102
103
    /**
104
     * Create and initialize a request object
105
     *
106
     * @param string $name The request name or class
107
     * @param array $parameters
108
     * @return \Gregoriohc\Moneta\Common\Messages\AbstractRequest
109
     */
110 30
    protected function createRequest($name, $parameters = [])
111
    {
112 30
        $requestClass = $name;
113 30
        if (!str_contains($requestClass, '\\')) {
114 27
            $requestClass = $this->requestClass($requestClass);
115
        }
116
117 30
        if (!class_exists($requestClass)) {
118 3
            $gatewayClass = class_basename($this);
119 3
            throw new \BadMethodCallException("Gateway '$gatewayClass' does not support '$name' method");
120
        }
121
122 27
        $this->validateParameters();
123
124 24
        return new $requestClass($this, $parameters);
125
    }
126
127
    /**
128
     * @param string $name
129
     * @return string
130
     */
131 30
    private function requestClass($name)
132
    {
133 30
        return substr(get_class($this), 0, -strlen(class_basename($this))) . 'Messages\\' . ucfirst($name) . 'Request';
134
    }
135
136 9
    public function supports($name)
137
    {
138 9
        if (in_array($name, $this->requestMethods)) {
139 9
            return method_exists($this, $name) || class_exists($this->requestClass($name));
140 3
        } elseif (in_array($name, $this->webhookMethods)) {
141 3
            return method_exists($this, $name);
142
        }
143
144 3
        return false;
145
    }
146
147
    /**
148
     * @param string $name
149
     * @param $arguments
150
     * @return mixed
151
     */
152 33
    public function __call($name, $arguments)
153
    {
154 33
        if (strpos($name, 'supports') === 0) {
155 9
            return $this->supports(lcfirst(substr($name, 8)));
156
        }
157
158 30
        if (in_array($name, $this->requestMethods)) {
159 27
            $parameters = array_key_exists(0, $arguments) ? $arguments[0] : [];
160 27
            return $this->createRequest($name, $parameters);
161
        }
162
163 3
        $class = get_class($this);
164 3
        throw new \BadMethodCallException("Method '$class::$name' does not exists");
165
    }
166
}