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

Commit

Permalink
Adds function to get all bulk certificates via pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Dec 15, 2020
1 parent 4b8fbbf commit 29182a7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
36 changes: 36 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,38 @@ 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 ($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 29182a7

Please sign in to comment.