Skip to content
Open
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
43 changes: 32 additions & 11 deletions src/Models/SSHKeys/SSHKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,34 @@ class SSHKey extends Model implements Resource
*/
public $labels;

/**
* @var \DateTimeInterface
*/
public $created;

/**
* SSHKey constructor.
*
* @param int $id
* @param string $name
* @param string $fingerprint
* @param string $publicKey
* @param array $labels
*/
public function __construct(int $id, string $name, string $fingerprint, string $publicKey, array $labels = [])
public function __construct(int $id, string $name)
{
$this->id = $id;
$this->name = $name;
$this->fingerprint = $fingerprint;
$this->public_key = $publicKey;
$this->labels = $labels;

parent::__construct();

// @deprecated code
if (func_num_args() > 2) {
$this->fingerprint = func_get_arg(2);
if (func_get_arg(3)) {
$this->public_key = func_get_arg(3);
}
if (func_get_arg(4)) {
$this->labels = func_get_arg(4);
}
}
}

/**
Expand All @@ -69,14 +80,14 @@ public function __construct(int $id, string $name, string $fingerprint, string $
*
* @throws \LKDev\HetznerCloud\APIException
*/
public function update(array $data): ?self
public function update(array $data): ?static
{
$response = $this->httpClient->put('ssh_keys/'.$this->id, [
'json' => $data,

]);
if (! HetznerAPIClient::hasError($response)) {
return self::parse(json_decode((string) $response->getBody())->ssh_key);
return static::parse(json_decode((string) $response->getBody())->ssh_key);
}

return null;
Expand All @@ -94,7 +105,7 @@ public function update(array $data): ?self
*
* @deprecated 1.2.0
*/
public function changeName(string $newName): ?self
public function changeName(string $newName): ?static
{
return $this->update(['name' => $newName]);
}
Expand Down Expand Up @@ -124,7 +135,7 @@ public function delete(): bool
*/
public static function parse($input)
{
return new self($input->id, $input->name, $input->fingerprint, $input->public_key, get_object_vars($input->labels));
return (new static($input->id, $input->name))->setAdditionalData($input);
}

/**
Expand All @@ -138,4 +149,14 @@ public function reload()
{
return HetznerAPIClient::$instance->sshKeys()->get($this->id);
}

public function setAdditionalData($data): static
{
$this->fingerprint = $data->fingerprint;
$this->public_key = $data->public_key;
$this->labels = get_object_vars($data->labels);
$this->created = new \DateTime($data->created);

return $this;
}
}