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

Adds function to get all bulk certificates via pagination #15

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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