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
|
|
|
*/ |
15
|
|
|
class Api |
16
|
|
|
{ |
17
|
|
|
private static $callbacks = []; |
18
|
|
|
protected $logger; |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param LoggerInterface $logger |
23
|
|
|
* @param Authorization $auth |
24
|
|
|
*/ |
25
|
2 |
|
public function __construct(Authorization $auth, LoggerInterface $logger = null) |
26
|
|
|
{ |
27
|
2 |
|
if ($logger === null) { |
28
|
2 |
|
$logger = new DummyLogger(); |
29
|
|
|
} |
30
|
2 |
|
$this->logger = $logger; |
31
|
|
|
|
32
|
2 |
|
$this->client = new HttpClient($auth); |
33
|
2 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param $name |
37
|
|
|
* @param \Closure $func |
38
|
|
|
* |
39
|
|
|
* @throws \BadFunctionCallException |
40
|
|
|
*/ |
41
|
|
|
public static function doCallback($name, \Closure $func) |
42
|
|
|
{ |
43
|
|
|
if (array_key_exists($name, self::$callbacks)) { |
44
|
|
|
if (!is_callable($func)) { |
45
|
|
|
throw new \BadFunctionCallException(); |
46
|
|
|
} |
47
|
|
|
call_user_func($func); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $data |
53
|
|
|
* |
54
|
|
|
* @throws MethodNotFound | MissingMandatoryField |
55
|
|
|
* |
56
|
|
|
* @return bool|CallbackMethods |
57
|
|
|
*/ |
58
|
1 |
|
public function getCallbackMethod(string $data) |
59
|
|
|
{ |
60
|
1 |
|
$data = \GuzzleHttp\json_decode($data); |
61
|
1 |
|
if (empty($data->method)) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$class = '\leocata\M1\Methods\Callback\\'.ucfirst($data->method); |
66
|
|
|
|
67
|
1 |
|
if (!class_exists($class)) { |
68
|
|
|
throw new MethodNotFound(sprintf( |
69
|
|
|
'The method "%s" not found, please correct', |
70
|
|
|
$class |
71
|
|
|
)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** @var \leocata\M1\Abstracts\CallbackMethods $method */ |
75
|
1 |
|
$method = new $class(); |
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
|
|
|
* |
87
|
|
|
* @return RequestMethods |
88
|
|
|
*/ |
89
|
|
|
public function sendApiRequest(RequestMethods $method) |
90
|
|
|
{ |
91
|
|
|
$response = $this->client->getResponseContent($method->getRequestString()); |
92
|
|
|
if (!empty($response)) { |
93
|
|
|
$method->setResult($response); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $method; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|