diff --git a/src/AuthorizedKeys.php b/src/AuthorizedKeys.php index 72b2d72..730048f 100644 --- a/src/AuthorizedKeys.php +++ b/src/AuthorizedKeys.php @@ -66,7 +66,7 @@ public static function fromFile(string $file): self * @param string $file path of the authorized_keys file * @throws FilePermissionException if the authorized_keys file cannot be written or permissions cannot be set */ - public function toFile(string $file): void + public function toFile(string $file): self { $result = @file_put_contents($file, (string) $this); @@ -79,6 +79,8 @@ public function toFile(string $file): void if ($result === false) { throw new FilePermissionException(sprintf('Could not change permissions of file "%s"', $file), 1486563909); } + + return $this; } /** @@ -100,7 +102,7 @@ public function getKeys(): array /** * Add a public key to the file */ - public function addKey(PublicKey $key): void + public function addKey(PublicKey $key): self { $index = $key->getKey(); @@ -109,18 +111,22 @@ public function addKey(PublicKey $key): void } $this->lines[$this->keyLines[$index]] = $key; + + return $this; } /** * Remove a public key from the file */ - public function removeKey(PublicKey $key): void + public function removeKey(PublicKey $key): self { $index = $key->getKey(); if (isset($this->keyLines[$index])) { unset($this->lines[$this->keyLines[$index]], $this->keyLines[$index]); } + + return $this; } /** diff --git a/src/PublicKey.php b/src/PublicKey.php index 9d967d6..bb4a4ea 100644 --- a/src/PublicKey.php +++ b/src/PublicKey.php @@ -15,6 +15,7 @@ * LICENSE.txt file that was distributed with this source code. */ +use Pagemachine\AuthorizedKeys\Exception\FilePermissionException; use Pagemachine\AuthorizedKeys\Exception\InvalidKeyException; /** @@ -62,14 +63,32 @@ public function __construct(string $key) } } + /** + * Creates a new instance from a file + * + * @throws FilePermissionException if the authorized_keys file cannot be read + */ + public static function fromFile(string $file): self + { + $content = @file_get_contents($file); + + if ($content === false) { + throw new FilePermissionException(sprintf('Could not read file "%s"', $file), 1678790797); + } + + return new self($content); + } + public function getOptions(): string { return $this->options; } - public function setOptions(string $options): void + public function setOptions(string $options): self { $this->options = $options; + + return $this; } public function getType(): string @@ -77,9 +96,11 @@ public function getType(): string return $this->type; } - public function setType(string $type): void + public function setType(string $type): self { $this->type = $type; + + return $this; } public function getKey(): string @@ -87,9 +108,11 @@ public function getKey(): string return $this->key; } - public function setKey(string $key): void + public function setKey(string $key): self { $this->key = $key; + + return $this; } public function getComment(): string @@ -97,9 +120,11 @@ public function getComment(): string return $this->comment; } - public function setComment(string $comment): void + public function setComment(string $comment): self { $this->comment = $comment; + + return $this; } public function __toString(): string