1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AcquiroPay; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use AcquiroPay\Services\Pay; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use AcquiroPay\Contracts\Mapper; |
12
|
|
|
use AcquiroPay\Contracts\Service; |
13
|
|
|
use AcquiroPay\Services\Entities; |
14
|
|
|
|
15
|
|
|
class API |
16
|
|
|
{ |
17
|
|
|
const SERVICE_ENTITIES = 'entities'; |
18
|
|
|
const SERVICE_PAY = 'pay'; |
19
|
|
|
|
20
|
|
|
private $guzzle; |
21
|
|
|
private $mapper; |
22
|
|
|
|
23
|
|
|
/** @var Service[] */ |
24
|
|
|
private $services = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Closure |
28
|
|
|
*/ |
29
|
|
|
private $resolver; |
30
|
|
|
|
31
|
|
|
public function __construct(Client $guzzle, Mapper $mapper) |
32
|
|
|
{ |
33
|
|
|
$this->guzzle = $guzzle; |
34
|
|
|
$this->mapper = $mapper; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Closure $resolver |
39
|
|
|
*/ |
40
|
|
|
public function setResolver(Closure $resolver): void |
41
|
|
|
{ |
42
|
|
|
$this->resolver = $resolver; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get entities service. |
47
|
|
|
* |
48
|
|
|
* @return Service|Entities |
49
|
|
|
* |
50
|
|
|
* @throws InvalidArgumentException |
51
|
|
|
*/ |
52
|
|
|
public function entities(): Entities |
53
|
|
|
{ |
54
|
|
|
return $this->service(self::SERVICE_ENTITIES); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get pay service. |
59
|
|
|
* |
60
|
|
|
* @return Pay|Service |
61
|
|
|
* |
62
|
|
|
* @throws InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
public function pay(): Pay |
65
|
|
|
{ |
66
|
|
|
return $this->service(self::SERVICE_PAY); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get service instance by name. |
71
|
|
|
* |
72
|
|
|
* @param string $name |
73
|
|
|
* |
74
|
|
|
* @return Service |
75
|
|
|
* |
76
|
|
|
* @throws InvalidArgumentException |
77
|
|
|
*/ |
78
|
|
|
public function service(string $name) |
79
|
|
|
{ |
80
|
|
|
if (array_key_exists($name, $this->services)) { |
81
|
|
|
return $this->services[$name]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->services[$name] = $this->resolve($name); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Resolve service instance by name. |
89
|
|
|
* |
90
|
|
|
* @param string $name |
91
|
|
|
* @return Service |
92
|
|
|
* @throws InvalidArgumentException |
93
|
|
|
*/ |
94
|
|
|
private function resolve(string $name): Service |
95
|
|
|
{ |
96
|
|
|
if (is_callable($this->resolver)) { |
97
|
|
|
$resolved = call_user_func($this->resolver, $name, $this->guzzle, $this->mapper); |
98
|
|
|
if ($resolved !== null) { |
99
|
|
|
return $resolved; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
switch ($name) { |
104
|
|
|
case self::SERVICE_ENTITIES: |
105
|
|
|
return new Entities($this->guzzle, $this->mapper); |
106
|
|
|
case self::SERVICE_PAY: |
107
|
|
|
return new Pay($this->guzzle, $this->mapper); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
throw new InvalidArgumentException('Неизвестный сервис `'.$name.'`'); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|