1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Gbowo; |
5
|
|
|
|
6
|
|
|
use InvalidArgumentException; |
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
|
|
|
protected $availableAdapters = []; |
19
|
|
|
|
20
|
6 |
|
public function __construct(array $types = []) |
21
|
|
|
{ |
22
|
6 |
|
$this->setDefaultAdapters(); |
23
|
|
|
|
24
|
6 |
|
if (!empty($types)) { |
25
|
3 |
|
foreach ($types as $type => $value) { |
26
|
3 |
|
if (!$value instanceof AdapterInterface) { |
27
|
3 |
|
throw $this->throwException("This is not a valid adapter"); |
28
|
|
|
} |
29
|
|
|
} |
30
|
2 |
|
$this->availableAdapters = array_merge($this->availableAdapters, $types); |
31
|
|
|
} |
32
|
5 |
|
} |
33
|
|
|
|
34
|
6 |
|
protected function setDefaultAdapters() |
35
|
|
|
{ |
36
|
6 |
|
$this->availableAdapters = [ |
37
|
6 |
|
self::PAYSTACK => PaystackAdapter::class, |
38
|
6 |
|
self::AMPLIFY_PAY => AmplifypayAdapter::class |
39
|
|
|
]; |
40
|
6 |
|
} |
41
|
|
|
|
42
|
2 |
|
protected function throwException(string $message) |
43
|
|
|
{ |
44
|
2 |
|
return new InvalidArgumentException($message); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $adapterIdentifier |
49
|
|
|
* @return AdapterInterface |
50
|
|
|
* @throws InvalidArgumentException |
51
|
|
|
*/ |
52
|
5 |
|
public function createAdapter(string $adapterIdentifier) |
53
|
|
|
{ |
54
|
5 |
|
if (!array_key_exists($adapterIdentifier, $this->availableAdapters)) { |
55
|
1 |
|
throw $this->throwException( |
56
|
1 |
|
"Unknown adapter. Did you forget to add this adapter on object initialization ?" |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
return new $this->availableAdapters[$adapterIdentifier]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|