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

Commit f7f8d36

Browse files
author
Marc Zahn
committed
Fix some minor problems
1 parent 165c048 commit f7f8d36

File tree

13 files changed

+265
-490
lines changed

13 files changed

+265
-490
lines changed

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));

src/LooplineSystems/CloseIoApiWrapper/Api/ActivityApi.php

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
use LooplineSystems\CloseIoApiWrapper\CloseIoResponse;
66
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
7-
use LooplineSystems\CloseIoApiWrapper\Library\Exception\ResourceNotFoundException;
87
use LooplineSystems\CloseIoApiWrapper\Model\Activity;
98
use LooplineSystems\CloseIoApiWrapper\Model\Call;
10-
use LooplineSystems\CloseIoApiWrapper\Model\Lead;
119

1210
class ActivityApi extends AbstractApi
1311
{
@@ -21,69 +19,79 @@ protected function initUrls()
2119
{
2220
$this->urls = [
2321
'add-note' => '/activity/note/',
24-
'get-note' => '/activity/note/[:id]/',
25-
'add-call' => '/activity/call/'
22+
'get-notes' => '/activity/note/',
23+
'add-call' => '/activity/call/',
24+
'get-calls' => '/activity/call/'
2625
];
2726
}
2827

2928
/**
3029
* @param Activity $activity
3130
*
32-
* @return CloseIoResponse
31+
* @return Activity
3332
*/
3433
public function addNote(Activity $activity)
3534
{
36-
$this->validateActivityForPost($activity);
37-
3835
$activity = json_encode($activity);
3936
$apiRequest = $this->prepareRequest('add-note', $activity);
4037

41-
return $this->triggerPost($apiRequest);
38+
$result = $this->triggerPost($apiRequest);
39+
40+
return new Activity($result->getData());
4241
}
4342

4443
/**
4544
* @param Call $call
4645
*
47-
* @return CloseIoResponse
46+
* @return Call
4847
*/
4948
public function addCall(Call $call)
5049
{
51-
$this->validateActivityForPost($call);
52-
5350
$call = json_encode($call);
5451
$apiRequest = $this->prepareRequest('add-call', $call);
5552

56-
return $this->triggerPost($apiRequest);
53+
$result = $this->triggerPost($apiRequest);
54+
55+
return new Call($result->getData());
5756
}
5857

5958
/**
60-
* @param $id
59+
* @param array $filters
6160
*
62-
* @return Lead
63-
* @throws ResourceNotFoundException
61+
* @return array
6462
*/
65-
public function getNote($id)
63+
public function getNotes(array $filters)
6664
{
67-
$apiRequest = $this->prepareRequest('get-note', null, ['id' => $id]);
65+
$apiRequest = $this->prepareRequest('get-notes', null, [], $filters);
6866

69-
/** @var CloseIoResponse $result */
7067
$result = $this->triggerGet($apiRequest);
7168

72-
if ($result->getReturnCode() == 200 && ($result->getData() !== null)) {
73-
return new Lead($result->getData());
69+
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];
70+
$notes = [];
71+
foreach ($rawData as $note) {
72+
$notes[] = new Activity($note);
7473
}
7574

76-
throw new ResourceNotFoundException();
75+
return $notes;
7776
}
7877

7978
/**
80-
* @param $activity
79+
* @param array $filters
8180
*
82-
* @return bool
81+
* @return array
8382
*/
84-
private function validateActivityForPost($activity)
83+
public function getCalls(array $filters)
8584
{
86-
return true;
87-
}
85+
$apiRequest = $this->prepareRequest('get-calls', null, [], $filters);
8886

87+
$result = $this->triggerGet($apiRequest);
88+
89+
$rawData = $result->getData()[CloseIoResponse::GET_RESPONSE_DATA_KEY];
90+
$calls = [];
91+
foreach ($rawData as $call) {
92+
$calls[] = new Activity($call);
93+
}
94+
95+
return $calls;
96+
}
8997
}

src/LooplineSystems/CloseIoApiWrapper/Api/ContactApi.php

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
use LooplineSystems\CloseIoApiWrapper\CloseIoResponse;
1313
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
1414
use LooplineSystems\CloseIoApiWrapper\Library\Curl\Curl;
15-
use LooplineSystems\CloseIoApiWrapper\Library\Exception\InvalidParamException;
16-
use LooplineSystems\CloseIoApiWrapper\Library\Exception\ResourceNotFoundException;
1715
use LooplineSystems\CloseIoApiWrapper\Model\Contact;
1816

1917
class ContactApi extends AbstractApi
@@ -39,12 +37,10 @@ protected function initUrls()
3937
*/
4038
public function getAllContacts()
4139
{
42-
/** @var Contact[] $contacts */
4340
$contacts = [];
4441

4542
$apiRequest = $this->prepareRequest('get-contacts');
4643

47-
/** @var CloseIoResponse $result */
4844
$result = $this->triggerGet($apiRequest);
4945

5046
if ($result->getReturnCode() == 200) {
@@ -59,50 +55,39 @@ public function getAllContacts()
5955
}
6056

6157
/**
62-
* @param $id
58+
* @param string $id
59+
*
6360
* @return Contact
64-
* @throws ResourceNotFoundException
6561
*/
6662
public function getContact($id)
6763
{
6864
$apiRequest = $this->prepareRequest('get-contact', null, ['id' => $id]);
6965

70-
/** @var CloseIoResponse $result */
7166
$result = $this->triggerGet($apiRequest);
7267

73-
if ($result->getReturnCode() == 200 && ($result->getData() !== null)) {
74-
$contact = new Contact($result->getData());
75-
} else {
76-
throw new ResourceNotFoundException();
77-
}
78-
79-
return $contact;
68+
return new Contact($result->getData());
8069
}
8170

8271
/**
8372
* @param Contact $contact
84-
* @return CloseIoResponse
73+
*
74+
* @return Contact
8575
*/
8676
public function addContact(Contact $contact)
8777
{
8878
$contact = json_encode($contact);
8979
$apiRequest = $this->prepareRequest('add-contact', $contact);
9080

91-
return $this->triggerPost($apiRequest);
81+
return new Contact($this->triggerPost($apiRequest)->getData());
9282
}
9383

9484
/**
9585
* @param Contact $contact
96-
* @return Contact|string
97-
* @throws InvalidParamException
98-
* @throws ResourceNotFoundException
86+
*
87+
* @return Contact
9988
*/
10089
public function updateContact(Contact $contact)
10190
{
102-
// check if contact has id
103-
if ($contact->getId() == null) {
104-
throw new InvalidParamException('When updating a contact you must provide the contact ID');
105-
}
10691
// remove id from contact since it won't be part of the patch data
10792
$id = $contact->getId();
10893
$contact->setId(null);
@@ -111,33 +96,17 @@ public function updateContact(Contact $contact)
11196
$apiRequest = $this->prepareRequest('update-contact', $contact, ['id' => $id]);
11297
$response = $this->triggerPut($apiRequest);
11398

114-
// return Contact object if successful
115-
if ($response->getReturnCode() == 200 && ($response->getData() !== null)) {
116-
$contact = new Contact($response->getData());
117-
} else {
118-
throw new ResourceNotFoundException();
119-
}
120-
121-
return $contact;
99+
return new Contact($response->getData());
122100
}
123101

124102
/**
125-
* @param $id
126-
* @return CloseIoResponse
127-
* @throws ResourceNotFoundException
103+
* @param string $id
128104
*/
129105
public function deleteContact($id)
130106
{
131107
$apiRequest = $this->prepareRequest('delete-contact', null, ['id' => $id]);
132108

133-
/** @var CloseIoResponse $result */
134-
$result = $this->triggerDelete($apiRequest);
135-
136-
if ($result->getReturnCode() == 200) {
137-
return $result;
138-
} else {
139-
throw new ResourceNotFoundException();
140-
}
109+
$this->triggerDelete($apiRequest);
141110
}
142111

143112
/**

src/LooplineSystems/CloseIoApiWrapper/Api/CustomFieldApi.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use LooplineSystems\CloseIoApiWrapper\Library\Api\AbstractApi;
77
use LooplineSystems\CloseIoApiWrapper\Library\Curl\Curl;
88
use LooplineSystems\CloseIoApiWrapper\Library\Exception\InvalidParamException;
9-
use LooplineSystems\CloseIoApiWrapper\Library\Exception\ResourceNotFoundException;
109
use LooplineSystems\CloseIoApiWrapper\Model\CustomField;
1110

1211
class CustomFieldApi extends AbstractApi
@@ -39,7 +38,6 @@ public function getAllCustomFields()
3938

4039
$apiRequest = $this->prepareRequest('get-customFields');
4140

42-
/** @var CloseIoResponse $result */
4341
$result = $this->triggerGet($apiRequest);
4442

4543
if ($result->getReturnCode() == 200) {
@@ -57,7 +55,6 @@ public function getAllCustomFields()
5755
*
5856
* @return CustomField
5957
* @throws InvalidParamException
60-
* @throws ResourceNotFoundException
6158
*/
6259
public function updateCustomField(CustomField $customField)
6360
{
@@ -71,12 +68,6 @@ public function updateCustomField(CustomField $customField)
7168
$apiRequest = $this->prepareRequest('update-customField', $customField, ['id' => $id]);
7269
$response = $this->triggerPut($apiRequest);
7370

74-
if ($response->getReturnCode() == 200 && ($response->getData() !== null)) {
75-
$customField = new CustomField($response->getData());
76-
} else {
77-
throw new ResourceNotFoundException();
78-
}
79-
80-
return $customField;
71+
return new CustomField($response->getData());
8172
}
8273
}

0 commit comments

Comments
 (0)