Skip to content

Commit 880749a

Browse files
committed
Add createResponse method to decrase wrapper code size and complexity
1 parent bfe1837 commit 880749a

File tree

2 files changed

+47
-40
lines changed

2 files changed

+47
-40
lines changed

src/BillingoApiV3Service.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,27 @@ protected function classExists(string $className): void
8282
}
8383
}
8484

85+
/**
86+
* Create response
87+
*
88+
* @param string $methodName
89+
* @param array $params
90+
* @param boolean $customResponse
91+
*
92+
* @return void
93+
*/
94+
protected function createResponse(string $methodName, array $params, bool $methodSuffix = false, bool $customResponse = false)
95+
{
96+
$this->response =
97+
$customResponse ?: \call_user_func_array(
98+
array(
99+
$this->api,
100+
$this->setMethodName($methodName, $methodSuffix)
101+
),
102+
$params
103+
);
104+
}
105+
85106
/**
86107
* Check if data is present
87108
*

src/BillingoApiV3Wrapper.php

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public function __construct()
3939
*/
4040
public function cancelInvoice(int $id): self
4141
{
42-
$methodName = $this->setMethodName('cancel', true);
43-
44-
$this->response = $this->api->$methodName($id);
42+
$this->createResponse('cancel', [$id], true);
4543

4644
return $this;
4745
}
@@ -55,9 +53,7 @@ public function cancelInvoice(int $id): self
5553
*/
5654
public function checkTaxNumber(string $taxNumber): self
5755
{
58-
$methodName = $this->setMethodName('checkTaxNumber');
59-
60-
$this->response = $this->api->$methodName($taxNumber);
56+
$this->createResponse('checkTaxNumber', [$taxNumber]);
6157

6258
return $this;
6359
}
@@ -70,9 +66,7 @@ public function checkTaxNumber(string $taxNumber): self
7066
*/
7167
public function create(): self
7268
{
73-
$methodName = $this->setMethodName('create', true);
74-
75-
$this->response = $this->api->$methodName($this->model);
69+
$this->createResponse('create', [$this->model], true);
7670

7771
return $this;
7872
}
@@ -86,9 +80,7 @@ public function create(): self
8680
*/
8781
public function createInvoiceFromProforma(int $id): self
8882
{
89-
$methodName = $this->setMethodName('createDocumentFromProforma');
90-
91-
$this->response = $this->api->$methodName($id);
83+
$this->createResponse('createDocumentFromProforma', [$id]);
9284

9385
return $this;
9486
}
@@ -102,16 +94,14 @@ public function createInvoiceFromProforma(int $id): self
10294
*/
10395
public function downloadInvoice(int $id, string $path = null, string $extension = null): self
10496
{
105-
$methodName = $this->setMethodName('download', true);
106-
10797
$filename = $id . ($extension ?? $this->extension);
10898

10999
Storage::put(
110100
($path ?? $this->downloadPath) . $filename,
111-
$this->api->$methodName($id)
101+
$this->createResponse('download', [$id], true, true)
112102
);
113103

114-
$this->response['path'] = ($path ?? $this->downloadPath) . $filename;
104+
$this->response = ['path' => ($path ?? $this->downloadPath) . $filename];
115105

116106
return $this;
117107
}
@@ -125,9 +115,7 @@ public function downloadInvoice(int $id, string $path = null, string $extension
125115
*/
126116
public function getPublicUrl(int $id): self
127117
{
128-
$methodName = $this->setMethodName('getPublicUrl');
129-
130-
$this->response = $this->api->$methodName($id);
118+
$this->createResponse('getPublicUrl', [$id]);
131119

132120
return $this;
133121
}
@@ -141,21 +129,23 @@ public function getPublicUrl(int $id): self
141129
*/
142130
public function list(array $conditions): self
143131
{
144-
$methodName = $this->setMethodName('list', true);
145-
146-
$this->response = $this->api->$methodName(
147-
$conditions['page'] ?? null,
148-
$conditions['per_page'] ?? 25,
149-
$conditions['block_id'] ?? null,
150-
$conditions['partner_id'] ?? null,
151-
$conditions['payment_method'] ?? null,
152-
$conditions['payment_status'] ?? null,
153-
$conditions['start_date'] ?? null,
154-
$conditions['end_date'] ?? null,
155-
$conditions['start_number'] ?? null,
156-
$conditions['end_number'] ?? null,
157-
$conditions['start_year'] ?? null,
158-
$conditions['end_year'] ?? null
132+
$this->createResponse(
133+
'list',
134+
[
135+
$conditions['page'] ?? null,
136+
$conditions['per_page'] ?? 25,
137+
$conditions['block_id'] ?? null,
138+
$conditions['partner_id'] ?? null,
139+
$conditions['payment_method'] ?? null,
140+
$conditions['payment_status'] ?? null,
141+
$conditions['start_date'] ?? null,
142+
$conditions['end_date'] ?? null,
143+
$conditions['start_number'] ?? null,
144+
$conditions['end_number'] ?? null,
145+
$conditions['start_year'] ?? null,
146+
$conditions['end_year'] ?? null
147+
],
148+
true
159149
);
160150

161151
return $this;
@@ -170,9 +160,7 @@ public function list(array $conditions): self
170160
*/
171161
public function update(int $id): self
172162
{
173-
$methodName = $this->setMethodName('update', true);
174-
175-
$this->response = $this->api->$methodName($this->model, $id);
163+
$this->createResponse('update', [$this->model, $id], true);
176164

177165
return $this;
178166
}
@@ -186,9 +174,7 @@ public function update(int $id): self
186174
*/
187175
public function sendInvoice(int $id): self
188176
{
189-
$methodName = $this->setMethodName('send', true);
190-
191-
$this->response = $this->api->$methodName($id);
177+
$this->createResponse('send', [$id], true);
192178

193179
return $this;
194180
}

0 commit comments

Comments
 (0)