|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: lukaskammerling |
| 5 | + * Date: 28.01.18 |
| 6 | + * Time: 21:00. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace LKDev\HetznerCloud\Models\Certificates; |
| 10 | + |
| 11 | +use LKDev\HetznerCloud\APIResponse; |
| 12 | +use LKDev\HetznerCloud\HetznerAPIClient; |
| 13 | +use LKDev\HetznerCloud\Models\Contracts\Resources; |
| 14 | +use LKDev\HetznerCloud\Models\Meta; |
| 15 | +use LKDev\HetznerCloud\Models\Model; |
| 16 | +use LKDev\HetznerCloud\RequestOpts; |
| 17 | +use LKDev\HetznerCloud\Traits\GetFunctionTrait; |
| 18 | + |
| 19 | +class Certificates extends Model implements Resources |
| 20 | +{ |
| 21 | + use GetFunctionTrait; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + protected $certificates; |
| 27 | + |
| 28 | + /** |
| 29 | + * Creates a new SSH Key with the given name and certificate. |
| 30 | + * |
| 31 | + * @see https://docs.hetzner.cloud/#certificates-create-a-certificate |
| 32 | + * @param string $name |
| 33 | + * @param string $certificate |
| 34 | + * @param string $privateKey |
| 35 | + * @return \LKDev\HetznerCloud\Models\Certificates\Certificate |
| 36 | + * @throws \LKDev\HetznerCloud\APIException |
| 37 | + */ |
| 38 | + public function create( |
| 39 | + string $name, |
| 40 | + string $certificate, |
| 41 | + string $privateKey |
| 42 | + ): ?Certificate { |
| 43 | + $response = $this->httpClient->post('certificates', [ |
| 44 | + 'json' => [ |
| 45 | + 'name' => $name, |
| 46 | + 'certificate' => $certificate, |
| 47 | + 'private_key' => $privateKey, |
| 48 | + ], |
| 49 | + ]); |
| 50 | + if (! HetznerAPIClient::hasError($response)) { |
| 51 | + return Certificate::parse(json_decode((string) $response->getBody())->certificate); |
| 52 | + } |
| 53 | + |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns all certificate objects. |
| 59 | + * |
| 60 | + * @see https://docs.hetzner.cloud/#certificates-get-all-certificates |
| 61 | + * @param RequestOpts $requestOpts |
| 62 | + * @return array |
| 63 | + * @throws \LKDev\HetznerCloud\APIException |
| 64 | + */ |
| 65 | + public function all(RequestOpts $requestOpts = null): array |
| 66 | + { |
| 67 | + if ($requestOpts == null) { |
| 68 | + $requestOpts = new RequestOpts(); |
| 69 | + } |
| 70 | + |
| 71 | + return $this->_all($requestOpts); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Returns all certificate objects. |
| 76 | + * |
| 77 | + * @see https://docs.hetzner.cloud/#certificates-get-all-certificates |
| 78 | + * @param RequestOpts $requestOpts |
| 79 | + * @return APIResponse |
| 80 | + * @throws \LKDev\HetznerCloud\APIException |
| 81 | + */ |
| 82 | + public function list(RequestOpts $requestOpts = null): ?APIResponse |
| 83 | + { |
| 84 | + if ($requestOpts == null) { |
| 85 | + $requestOpts = new RequestOpts(); |
| 86 | + } |
| 87 | + $response = $this->httpClient->get('certificates'.$requestOpts->buildQuery()); |
| 88 | + if (! HetznerAPIClient::hasError($response)) { |
| 89 | + $resp = json_decode((string) $response->getBody()); |
| 90 | + |
| 91 | + return APIResponse::create([ |
| 92 | + 'meta' => Meta::parse($resp->meta), |
| 93 | + $this->_getKeys()['many'] => self::parse($resp->{$this->_getKeys()['many']})->{$this->_getKeys()['many']}, |
| 94 | + ], $response->getHeaders()); |
| 95 | + } |
| 96 | + |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @param $input |
| 102 | + * @return $this |
| 103 | + */ |
| 104 | + public function setAdditionalData($input) |
| 105 | + { |
| 106 | + $this->certificates = collect($input)->map(function ($certificate, $key) { |
| 107 | + return Certificate::parse($certificate); |
| 108 | + })->toArray(); |
| 109 | + |
| 110 | + return $this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @param $input |
| 115 | + * @return $this|static |
| 116 | + */ |
| 117 | + public static function parse($input) |
| 118 | + { |
| 119 | + return (new self())->setAdditionalData($input); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns a specific certificate object. |
| 124 | + * |
| 125 | + * @see https://docs.hetzner.cloud/#certificates-get-a-certificate |
| 126 | + * @param int $id |
| 127 | + * @return \LKDev\HetznerCloud\Models\Certificates\Certificate |
| 128 | + * @throws \LKDev\HetznerCloud\APIException |
| 129 | + */ |
| 130 | + public function getById(int $id) |
| 131 | + { |
| 132 | + $response = $this->httpClient->get('certificates/'.$id); |
| 133 | + if (! HetznerAPIClient::hasError($response)) { |
| 134 | + return Certificate::parse(json_decode((string) $response->getBody())->{$this->_getKeys()['one']}); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns a specific certificate object. |
| 140 | + * |
| 141 | + * @see https://docs.hetzner.cloud/#certificates-get-a-certificate |
| 142 | + * @param string $name |
| 143 | + * @return \LKDev\HetznerCloud\Models\Certificates\Certificate |
| 144 | + * @throws \LKDev\HetznerCloud\APIException |
| 145 | + */ |
| 146 | + public function getByName(string $name): ?Certificate |
| 147 | + { |
| 148 | + $certificates = $this->list(new CertificateRequestOpts($name)); |
| 149 | + |
| 150 | + return (count($certificates->{$this->_getKeys()['many']}) > 0) ? $certificates->{$this->_getKeys()['many']}[0] : null; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @return array |
| 155 | + */ |
| 156 | + public function _getKeys(): array |
| 157 | + { |
| 158 | + return ['one' => 'certificate', 'many' => 'certificates']; |
| 159 | + } |
| 160 | +} |
0 commit comments