Files
ploi-core/app/Services/Ploi/Resources/Certificate.php
2021-07-29 09:59:20 +02:00

91 lines
2.1 KiB
PHP

<?php
namespace App\Services\Ploi\Resources;
use stdClass;
use App\Services\Ploi\Exceptions\Http\NotValid;
class Certificate extends Resource
{
private $server;
private $site;
public function __construct(Server $server, Site $site, int $id = null)
{
parent::__construct($server->getPloi(), $id);
$this->setServer($server);
$this->setSite($site);
$this->buildEndpoint();
}
public function buildEndpoint(): self
{
$this->setEndpoint($this->getServer()->getEndpoint() . '/' . $this->getServer()->getId() . '/sites/' . $this->getSite()->getId() . '/certificates');
if ($this->getId()) {
$this->setEndpoint($this->getEndpoint() . '/' . $this->getId());
}
return $this;
}
public function get(int $id = null)
{
if ($id) {
$this->setId($id);
}
// Make sure the endpoint is built
$this->buildEndpoint();
return $this->getPloi()->makeAPICall($this->getEndpoint());
}
public function create(string $certificate, string $type = 'letsencrypt', string $private = null): stdClass
{
// Remove the id
$this->setId(null);
// Set the options
$options = [
'body' => json_encode([
'type' => $type,
'certificate' => $certificate,
'private' => $private,
]),
];
// Build the endpoint
$this->buildEndpoint();
// Make the request
try {
$response = $this->getPloi()->makeAPICall($this->getEndpoint(), 'post', $options);
} catch (NotValid $exception) {
return json_decode($exception->getMessage());
}
$data = $response->getData();
$this->setId($data->id);
// Return the data
return $data;
}
public function delete(int $id): bool
{
if ($id) {
$this->setId($id);
}
$this->buildEndpoint();
$response = $this->getPloi()->makeAPICall($this->getEndpoint(), 'delete');
return $response->getResponse()->getStatusCode() === 200;
}
}