1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace leocata\M1; |
4
|
|
|
|
5
|
|
|
use leocata\M1\Abstracts\CallbackMethods; |
6
|
|
|
use leocata\M1\Abstracts\RequestMethods; |
7
|
|
|
use leocata\M1\Exceptions\MethodNotFound; |
8
|
|
|
use leocata\M1\Exceptions\MissingMandatoryField; |
9
|
|
|
use leocata\M1\InternalFunctionality\DummyLogger; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Api |
14
|
|
|
* @package leocata\M1 |
15
|
|
|
*/ |
16
|
|
|
class Api |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
private static $callbacks = []; |
20
|
|
|
protected $logger; |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param LoggerInterface $logger |
25
|
|
|
* @param HttpClientAuthorization $auth |
26
|
|
|
*/ |
27
|
2 |
|
public function __construct(HttpClientAuthorization $auth, LoggerInterface $logger = null) |
28
|
|
|
{ |
29
|
2 |
|
if ($logger === null) { |
30
|
2 |
|
$logger = new DummyLogger(); |
31
|
|
|
} |
32
|
2 |
|
$this->logger = $logger; |
33
|
|
|
|
34
|
2 |
|
$this->client = new HttpClient($auth); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $name |
39
|
|
|
* @param \Closure $func |
40
|
|
|
* @throws \BadFunctionCallException |
41
|
|
|
*/ |
42
|
|
|
public static function doCallback($name, \Closure $func) |
43
|
|
|
{ |
44
|
|
|
if (array_key_exists($name, self::$callbacks)) { |
45
|
|
|
if (!is_callable($func)) { |
46
|
|
|
throw new \BadFunctionCallException(); |
47
|
|
|
} |
48
|
|
|
call_user_func($func); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $data |
54
|
|
|
* @return bool|CallbackMethods |
55
|
|
|
* @throws MethodNotFound | MissingMandatoryField |
56
|
|
|
*/ |
57
|
1 |
|
public function getApiCallbackMethod(string $data) |
58
|
|
|
{ |
59
|
1 |
|
$data = \GuzzleHttp\json_decode($data); |
60
|
1 |
|
if (empty($data->method)) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$class = '\leocata\M1\Methods\Callback\\' . ucfirst($data->method); |
65
|
|
|
|
66
|
1 |
|
if (!class_exists($class)) { |
67
|
|
|
throw new MethodNotFound(sprintf( |
68
|
|
|
'The method "%s" not found, please correct', |
69
|
|
|
$data->method |
70
|
|
|
)); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
class_alias($class, $data->method); |
74
|
|
|
/** @var \leocata\M1\Abstracts\CallbackMethods $method */ |
75
|
1 |
|
$method = new $data->method(); |
76
|
1 |
|
$method->import($data->params ?? new \stdClass()); |
77
|
1 |
|
self::$callbacks += ['after' . $method->getMethodName() => $method]; |
78
|
|
|
|
79
|
1 |
|
return $method; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Performs the request to the Api servers |
84
|
|
|
* |
85
|
|
|
* @param RequestMethods $method |
86
|
|
|
* @return RequestMethods |
87
|
|
|
*/ |
88
|
|
|
public function sendApiRequest(RequestMethods $method) |
89
|
|
|
{ |
90
|
|
|
$response = $this->client->getResponseContent($method->getRequestString()); |
91
|
|
|
if (!empty($response)) { |
92
|
|
|
$method->setResult($response); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $method; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|