Skip to content

Commit

Permalink
handle pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
edbizarro committed Oct 10, 2018
1 parent 9b98218 commit 50cfd19
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 54 deletions.
24 changes: 9 additions & 15 deletions src/FormatResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,23 @@

trait FormatResponse
{
protected function format($response)
protected function format(Cursor $response)
{
$data = new Collection;

switch (true) {
case is_array($response):
foreach ($response as $responseObject) {
foreach ($responseObject->getLastResponse()->getContent() as $items) {
foreach ($items as $item) {
$data->push($item);
}
}
}
break;
case $response instanceof Cursor:
foreach ($response->getLastResponse()->getContent() as $items) {
foreach ($items as $item) {
$data->push($item);
}
while ($response->getNext()) {
$response->fetchAfter();
}

while ($response->valid()) {
$data->push($response->current()->exportAllData());
$response->next();
}
break;
case $response instanceof AbstractObject:
$data->push($response->getLastResponse()->getContent());
$data->push($response->getLastResponse()->getContent()['data']);
break;
}

Expand Down
10 changes: 2 additions & 8 deletions src/Insights.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,16 @@ public function get(
$fields = $params['fields'];

$params['time_increment'] = '1';
$params['limit'] = $params['limit'] ?? 100;
$params['level'] = $level;
$params['time_range'] = [
'since' => $period->startDate->format('Y-m-d'),
'until' => $period->endDate->format('Y-m-d'),
];

$response = [];
$apiResponse = $levelClass->getInsights($fields, $params);

$response[] = $apiResponse;

while ($apiResponse->getNext()) {
$response[] = $apiResponse->fetchAfter();
}

return $this->format($response);
return $this->format($apiResponse);
}

/**
Expand Down
35 changes: 4 additions & 31 deletions tests/LaravelFacebookAds/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace LaravelFacebookAds\Tests;

use Edbizarro\LaravelFacebookAds\Facades\FacebookAds;
use Edbizarro\LaravelFacebookAds\Providers\LaravelFacebookServiceProvider;
use FacebookAds\Logger\CurlLogger;
use Orchestra\Testbench\TestCase;
use Mockery as m;
use Edbizarro\LaravelFacebookAds\Period;
use FacebookAds\Object\Values\AdsInsightsLevelValues;

/**
* Class BaseTest.
Expand All @@ -24,37 +28,6 @@ public function tearDown()
public function setUp()
{
parent::setUp();

dd(FacebookAds::insights(
Period::days(30),
$this->etlJob['params']['account_id'],
AdsInsightsLevelValues::AD,
[
'fields' => [
'account_id',
'account_name',
'campaign_id',
'campaign_name',
'ad_id',
'ad_name',
'adset_id',
'adset_name',
'actions',
'impressions',
'clicks',
'unique_clicks',
'spend',
'social_spend',
'reach',
'video_10_sec_watched_actions',
'video_30_sec_watched_actions',
'video_p25_watched_actions',
'video_p50_watched_actions',
'video_p75_watched_actions',
'video_p100_watched_actions'
]
]
));
}

protected function getPackageProviders($app)
Expand Down

0 comments on commit 50cfd19

Please sign in to comment.