AbstractGateway::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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