Skip to content

Commit

Permalink
Merge pull request #34 from vokeit-gschuster/master
Browse files Browse the repository at this point in the history
Add fluent interface
  • Loading branch information
mbrodala authored Mar 16, 2023
2 parents 8313d26 + 976aba6 commit 7c6e891
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/AuthorizedKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}

/**
Expand All @@ -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();

Expand All @@ -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;
}

/**
Expand Down
33 changes: 29 additions & 4 deletions src/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* LICENSE.txt file that was distributed with this source code.
*/

use Pagemachine\AuthorizedKeys\Exception\FilePermissionException;
use Pagemachine\AuthorizedKeys\Exception\InvalidKeyException;

/**
Expand Down Expand Up @@ -62,44 +63,68 @@ 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
{
return $this->type;
}

public function setType(string $type): void
public function setType(string $type): self
{
$this->type = $type;

return $this;
}

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
{
return $this->comment;
}

public function setComment(string $comment): void
public function setComment(string $comment): self
{
$this->comment = $comment;

return $this;
}

public function __toString(): string
Expand Down

0 comments on commit 7c6e891

Please sign in to comment.