This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
/
console
84 lines (65 loc) · 2.42 KB
/
console
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env php
<?php
use LooplineSystems\CloseIoApiWrapper\CloseIoApiWrapper;
use LooplineSystems\CloseIoApiWrapper\CloseIoConfig;
use LooplineSystems\CloseIoApiWrapper\Library\Api\ApiInterface;
use LooplineSystems\CloseIoApiWrapper\Library\Exception\ApiNotFoundException;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
require_once __DIR__ . '/../vendor/autoload.php';
$input = new ArgvInput();
$output = new ConsoleOutput();
$io = new Symfony\Component\Console\Style\SymfonyStyle($input, $output);
$key = $input->getParameterOption(['--key', '-k'], null);
$apiName = $input->getParameterOption(['--api', '-a'], null);
$request = $input->getParameterOption(['--request', '-r'], null);
$dataJson = $input->getParameterOption(['--data', '-d'], null);
$data = null;
if (!empty($dataJson)) {
$data = json_decode($dataJson, true);
if (json_last_error() !== JSON_ERROR_NONE) {
$io->error(sprintf('Invalid json string for data: "%s"', json_last_error_msg()));
exit(1);
}
}
if (empty($key)) {
$io->error('No api key set (-k)');
exit(1);
}
if (empty($request) || empty($request)) {
$io->error('No request set (-r)');
exit(1);
}
$config = new CloseIoConfig();
$config->setApiKey($key);
$wrapper = new CloseIoApiWrapper($config);
$handler = $wrapper->getApiHandler();
try {
$api = $handler->getApi($apiName);
} catch (ApiNotFoundException $exception) {
$apiNames = array_map(
function(ApiInterface $api) {
return $api->getName();
},
$handler->getApis()
);
$io->error(sprintf('API "%s" not found. Available APIs are:' . PHP_EOL . '%s', $apiName, implode(PHP_EOL, $apiNames)));
exit(1);
}
$reflection = new ReflectionClass(get_class($api));
$method = $reflection->getMethod($request);
$argumentType = array_reduce(
$method->getParameters(),
function ($carry, ReflectionParameter $parameter) {
return $carry !== null || $parameter->getClass() === null ? $carry : $parameter->getClass()->getName();
}
);
$argument = $argumentType !== null ? new $argumentType($data) : $data;
try {
$io->note(sprintf('Request "%s", with data "%s" will be done...', $apiName . '::' . $request, $dataJson));
$response = call_user_func([$api, $request], $argument);
} catch (\Exception $exception) {
$io->error($exception->getMessage());
exit(1);
}
$io->success(json_encode($response, JSON_PRETTY_PRINT));