|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Gbowo; |
|
5
|
|
|
|
|
6
|
|
|
use Gbowo\Exception\UnknownAdapterException; |
|
7
|
|
|
use Gbowo\Adapter\Paystack\PaystackAdapter; |
|
8
|
|
|
use Gbowo\Contract\Adapter\AdapterInterface; |
|
9
|
|
|
use Gbowo\Adapter\Amplifypay\AmplifypayAdapter; |
|
10
|
|
|
|
|
11
|
|
|
class GbowoFactory |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
const PAYSTACK = "paystack"; |
|
15
|
|
|
|
|
16
|
|
|
const AMPLIFY_PAY = "amplifypay"; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var AdapterInterface[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $availableAdapters = []; |
|
22
|
|
|
|
|
23
|
7 |
|
public function __construct(array $types = []) |
|
24
|
|
|
{ |
|
25
|
7 |
|
$this->setDefaultAdapters(); |
|
26
|
7 |
|
$this->addCustomAdapters($types); |
|
27
|
5 |
|
} |
|
28
|
|
|
|
|
29
|
7 |
|
protected function addCustomAdapters(array $types) |
|
30
|
|
|
{ |
|
31
|
7 |
|
if (!empty($types)) { |
|
32
|
4 |
|
$this->validateCustomAdapters($types); |
|
33
|
2 |
|
$this->availableAdapters = array_merge($this->availableAdapters, $types); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
5 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
7 |
|
protected function setDefaultAdapters() |
|
39
|
|
|
{ |
|
40
|
7 |
|
$this->availableAdapters = [ |
|
|
|
|
|
|
41
|
7 |
|
self::PAYSTACK => new PaystackAdapter(), |
|
42
|
7 |
|
self::AMPLIFY_PAY => new AmplifypayAdapter() |
|
43
|
|
|
]; |
|
44
|
7 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param array $types |
|
48
|
|
|
* @throws \Gbowo\Exception\UnknownAdapterException |
|
49
|
|
|
*/ |
|
50
|
4 |
|
protected function validateCustomAdapters(array $types) |
|
51
|
|
|
{ |
|
52
|
4 |
|
foreach ($types as $type => $value) { |
|
53
|
4 |
|
if (array_key_exists($type, $this->availableAdapters)) { |
|
54
|
1 |
|
throw $this->throwException( |
|
55
|
1 |
|
"You cannot override an internal adapter" |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
if (!$value instanceof AdapterInterface) { |
|
60
|
3 |
|
throw $this->throwException("This is not a valid adapter"); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
2 |
|
} |
|
64
|
|
|
|
|
65
|
3 |
|
protected function throwException(string $message) |
|
66
|
|
|
{ |
|
67
|
3 |
|
return new UnknownAdapterException($message); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $adapterIdentifier |
|
72
|
|
|
* @return \Gbowo\Contract\Adapter\AdapterInterface |
|
73
|
|
|
* @throws \Gbowo\Exception\UnknownAdapterException |
|
74
|
|
|
*/ |
|
75
|
5 |
|
public function createAdapter(string $adapterIdentifier) |
|
76
|
|
|
{ |
|
77
|
5 |
|
if (!array_key_exists($adapterIdentifier, $this->availableAdapters)) { |
|
78
|
1 |
|
throw $this->throwException( |
|
79
|
1 |
|
"Unknown adapter. Did you forget to add this adapter on object initialization ?" |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
return $this->availableAdapters[$adapterIdentifier]; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..