Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit f69b4c4

Browse files
authored
Merge pull request #23 from loopline-systems/merge_base
Fix minor problems; Add console runner
2 parents aafabcd + f7f8d36 commit f69b4c4

31 files changed

+1381
-2304
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ config/
33
vendor/
44
tests/LooplineSystems/CloseIoApi/Api/LiveApiTest.php
55
tests/build/logs/
6+
/composer.lock

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,21 @@ $customFieldApi = $this->apiWrapper->getCustomFieldApi();
9898
$result = $customFieldApi->updateCustomField($customField);
9999
```
100100

101+
Console runner
102+
-
103+
For testing purposes there is a simple console runner.
104+
105+
```
106+
bin/console -a <api name> -r <request name> -k <api key> [-d payload as json]
107+
```
108+
Examples:
109+
```
110+
bin/console -k your_api_key -r getAllLeads -a LeadApi
111+
112+
bin/console -k your_api_key -r addLead -a LeadApi -d '{...}'
113+
```
114+
The is output will be displayed on the console as json.
115+
101116
Info
102117
------------
103118
Right now just a few request are implemented, because the main need was to create leads.
@@ -108,7 +123,7 @@ We use https://github.com/btford/adj-noun for our release names, so don`t worry
108123
Requirements
109124
------------
110125

111-
PHP 5.4.0 or above
126+
PHP 5.6.0 or above
112127

113128
Authors
114129
-------

bin/console

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

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@
1111
{
1212
"name": "Marco Roßdeutscher",
1313
"email": "[email protected]"
14+
},
15+
{
16+
"name": "Marc Zahn",
17+
"email": "[email protected]"
1418
}
1519
],
1620
"require": {
17-
"php": "^5.6.0",
21+
"php": ">=5.6.0",
1822
"symfony/yaml": "^2.5",
1923
"zendframework/zend-filter": "^2.3.3",
2024
"doctrine/inflector": "^1.0"
2125
},
2226
"require-dev": {
2327
"phpunit/phpunit": "^5.0.0",
24-
"codeclimate/php-test-reporter": "^0.1.2"
28+
"codeclimate/php-test-reporter": "^0.1.2",
29+
"symfony/console": "^3.1"
2530
},
2631
"autoload": {
2732
"psr-0": {

0 commit comments

Comments
 (0)