Skip to content

Commit 90646d8

Browse files
author
Ian Miller
authored
Merge pull request #8 from wildlink/resend-commission-callback
Added CommissionList
2 parents 4f7eaa2 + b9f9199 commit 90646d8

File tree

5 files changed

+111
-16
lines changed

5 files changed

+111
-16
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "wildlink/wildlink-api-php",
33
"description": "A simple PHP interface for the Wildlink API (learn more at https://www.wildlink.me/)",
44
"license": "MIT",
5-
"version": "1.0.8",
5+
"version": "1.0.9",
66
"type": "library",
77
"authors": [
88
{

composer.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WildlinkApi/CommissionList.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace WildlinkApi;
4+
5+
use WildlinkApi\WildlinkClient;
6+
7+
class CommissionList {
8+
private $commissions = [];
9+
private $commissionCount = 0;
10+
protected $currentCommissionIndex = 0;
11+
protected $wildlinkClient;
12+
13+
public function __construct(WildlinkClient $wildlinkClient, $modified_start_date){
14+
$this->wildlinkClient = $wildlinkClient;
15+
$this->commissions = $wildlinkClient->getAppCommissionsSince($modified_start_date);
16+
$this->commissionCount = count($this->commissions);
17+
}
18+
19+
public function getCommissionCount(){
20+
return $this->commissionCount;
21+
}
22+
23+
public function getCurrentCommission(){
24+
return $this->commissions[$this->currentCommissionIndex];
25+
}
26+
27+
public function getNextCommission(){
28+
if ($this->hasNextCommission()){
29+
// if we have the commissions in memory, just increase the index and return the record
30+
$this->currentCommissionIndex++;
31+
return $this->getCurrentCommission();
32+
} else {
33+
return false;
34+
}
35+
}
36+
37+
public function hasNextCommission(){
38+
if ($this->currentCommissionIndex < $this->commissionCount - 1){
39+
return true;
40+
} else {
41+
// we don't have another record in memory, so let's fetch another page from the server
42+
$this->commissions = $this->wildlinkClient->getPagedCommissions();
43+
$this->commissionCount = count($this->commissions);
44+
45+
// check to see if we got an empty page (the last page)
46+
if ($this->commissionCount == 0){
47+
return false;
48+
} else {
49+
$this->currentCommissionIndex = -1; // -1 since we know getNextCommission is about to be called and we don't want to skip the 0th record from the newly returned page of commissions
50+
return true;
51+
}
52+
}
53+
}
54+
}

src/WildlinkApi/WildlinkClient.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public function getEndpointInfo($function){
9292
$api_info->method = 'GET';
9393
}
9494

95+
if ($function == 'getAppCommissionsByCursor'){
96+
$api_info->endpoint = '/v2/commission?cursor=:cursor&limit=:limit';
97+
$api_info->method = 'GET';
98+
}
99+
95100
if ($function == 'resendCommissionCallback'){
96101
$api_info->endpoint = '/v2/commission/:id/send-callback';
97102
$api_info->method = 'GET';
@@ -209,7 +214,7 @@ public function getAllEnabledMerchants()
209214
while ($result->NextCursor){
210215
$result = $this->request('getEnabledMerchants', [
211216
'cursor' => $result->NextCursor,
212-
'limit' => 500
217+
'limit' => 500 // max limit = 500
213218
]);
214219
if ($result->Merchants){
215220
$merchants = array_merge($merchants, $result->Merchants);
@@ -234,7 +239,7 @@ public function getAllDisabledMerchants()
234239
while ($result->NextCursor){
235240
$result = $this->request('getDisabledMerchants', [
236241
'cursor' => $result->NextCursor,
237-
'limit' => 500
242+
'limit' => 500 // max limit = 500
238243
]);
239244
if ($result->Merchants){
240245
$disabledMerchants = array_merge($disabledMerchants, $result->Merchants);
@@ -254,7 +259,7 @@ public function getPagedEnabledMerchants()
254259

255260
$result = $this->request('getEnabledMerchants', [
256261
'cursor' => $this->merchantListCursor,
257-
'limit' => 500
262+
'limit' => 500 // max limit = 500
258263
]);
259264
if (!isset($result->Merchants)){
260265
return $result;
@@ -272,7 +277,7 @@ public function getPagedDisabledMerchants()
272277

273278
$result = $this->request('getDisabledMerchants', [
274279
'cursor' => $this->merchantListCursor,
275-
'limit' => 500
280+
'limit' => 500 // max limit = 500
276281
]);
277282
if (!isset($result->Merchants)){
278283
return $result;
@@ -297,11 +302,20 @@ public function getCommissionDetails()
297302

298303
public function getAppCommissionsSince($modified_since, $limit = '')
299304
{
305+
if (!isset($this->commissionListCursor)){
306+
$this->commissionListCursor = '';
307+
}
308+
300309
$result = $this->request('getAppCommissionsSince', [
301310
'modified_since' => $modified_since,
302311
'limit' => $limit
303312
]);
304-
return $result;
313+
if (!isset($result->Commissions)){
314+
return $result;
315+
} else {
316+
$this->commissionListCursor = $result->NextCursor;
317+
return $result->Commissions;
318+
}
305319
}
306320

307321
public function resendCommissionCallback($commission_id)
@@ -312,6 +326,24 @@ public function resendCommissionCallback($commission_id)
312326
return $result;
313327
}
314328

329+
public function getPagedCommissions()
330+
{
331+
if (!isset($this->commissionListCursor)){
332+
$this->commissionListCursor = '';
333+
}
334+
335+
$result = $this->request('getAppCommissionsByCursor', [
336+
'cursor' => $this->commissionListCursor,
337+
'limit' => 100 // max limit = 100
338+
]);
339+
if (!isset($result->Commissions)){
340+
return $result;
341+
} else {
342+
$this->commissionListCursor = $result->NextCursor;
343+
return $result->Commissions;
344+
}
345+
}
346+
315347

316348
// CLICKS functions
317349
public function getClickStats($start, $end = '')

tests/test.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use WildlinkApi\WildlinkClient;
66
use WildlinkApi\MerchantList;
7+
use WildlinkApi\CommissionList;
78
use WildlinkApi\DisabledMerchantList;
89

910
function out($str, $type = ''){
@@ -114,16 +115,24 @@ function out($str, $type = ''){
114115
break;
115116
}
116117
}
117-
118118
out("total DISABLED merchant count: " . $merchantCounter);
119119

120-
121120
// get all commissions for app (across all devices)
122-
$commissions_since_date = '2018-07-01';
123-
$commissions_since_limit = 10;
124-
out("testing get all comissions for app since $commissions_since_date, limit $commissions_since_limit");
125-
$allCommissionsSince = $wfClient->getAppCommissionsSince($commissions_since_date, $commissions_since_limit);
126-
out($allCommissionsSince);
121+
$commissions_since_date = '2018-01-01';
122+
out("testing get all comissions for app since $commissions_since_date");
123+
$commissionList = new CommissionList($wfClient, $commissions_since_date);
124+
125+
$commissionCounter = 0;
126+
while ($commission = $commissionList->getCurrentCommission()){
127+
out($commissionCounter);
128+
out($commission);
129+
$commissionCounter++;
130+
if ($commissionList->hasNextCommission()){
131+
$commissionList->getNextCommission();
132+
} else {
133+
break;
134+
}
135+
}
127136

128137
// re-send most recent commission record callback
129138
$commission_id = $allCommissionsSince->Commissions[0]->ID;

0 commit comments

Comments
 (0)