Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from bomoko/hotfix/fix_pagination
Browse files Browse the repository at this point in the history
Adds function to get all bulk certificates via pagination
  • Loading branch information
Tim Clifford authored Dec 15, 2020
2 parents 4b8fbbf + 950cb44 commit 3959421
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/Fastly/Types/FastlyCertificates.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

class FastlyCertificates extends FastlyRequest
{

const MAX_CERTIFICATES_FROM_ENDPOINT = 200;
const MAX_CERTIFICATE_PAGES_TO_PULL = 50; //this will allow up to 10000 certificates

public $data;
public $links;
public $meta;
Expand Down Expand Up @@ -142,6 +146,42 @@ public function getTLSBulkCertificates($options = '')
return $certificates;
}

public function getAllBulkCertificates()
{
$lastPage = false;
$options = sprintf("page[size]=%d",
self::MAX_CERTIFICATES_FROM_ENDPOINT);
$pageCount = 0;

$totalCertificateData = [];

while (!$lastPage && $pageCount++ < self::MAX_CERTIFICATES_FROM_ENDPOINT) {
$certificateData = self::getTLSBulkCertificates($options);
$totalCertificateData[] = $certificateData['data'];

$currentPage = $certificateData['meta'][0]['current_page'];
$totalPages = $certificateData['meta'][0]['total_pages'];

if(!is_numeric($currentPage) || !is_numeric($totalPages)) {
throw new \Exception("Call to getAllBulkCertificates resulted in bad results from Fastly API");
}

if ($currentPage >= $totalPages) {
$lastPage = true;
} else {
$options = sprintf("page[size]=%d&page[number]=%d",
self::MAX_CERTIFICATES_FROM_ENDPOINT, ++$currentPage);
}
}

return [
'data' => array_reduce($totalCertificateData, function ($c, $e) { return array_merge($c, $e);}, []),
'links' => [],
'meta' => [],
];

}

/**
* Upload a new certificate.
*
Expand Down
12 changes: 9 additions & 3 deletions tests/Fastly/FastlyTLSBulkCertificateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,28 @@ public function testGetFastlyCertificates()
{
$certificatesObject = $this->fastly->certificates;
$certificates = $certificatesObject->get_tls_certificates();

// Get whole response from API.
$this->assertArrayHasKey('data', $certificates);
}

public function testGetPlatformTLSCertificates()
{
$certificatesObject = $this->fastly->certificates;
$certificates = $certificatesObject->getTLSBulkCertificates();

$certificates = $certificatesObject->getTLSBulkCertificates('page[size]=200');
// Get whole response from API.
$this->assertArrayHasKey('data', $certificates);
$this->assertArrayHasKey('links', $certificates);
$this->assertArrayHasKey('meta', $certificates);
}

public function testGetAllBulkCertificates()
{
$certificates = $this->fastly->certificates->getAllBulkCertificates();
$this->assertArrayHasKey('data', $certificates);
$this->assertArrayHasKey('links', $certificates);
$this->assertArrayHasKey('meta', $certificates);
}

//public function testGetCertificateByID()
//{
// $certificatesObject = $this->fastly->certificates;
Expand Down

0 comments on commit 3959421

Please sign in to comment.