Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove inline signaling from Key #339

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
24 changes: 5 additions & 19 deletions src/Signer/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Throwable;
use function assert;
use function is_string;
use function strpos;
use function substr;

final class Key
{
Expand All @@ -25,36 +23,24 @@ final class Key

public function __construct(string $content, string $passphrase = '')
{
$this->setContent($content);
$this->content = $content;
$this->passphrase = $passphrase;
}

/**
* @throws InvalidArgumentException
*/
private function setContent(string $content): void
{
if (strpos($content, 'file://') === 0) {
$content = $this->readFile($content);
}

$this->content = $content;
}

/**
* @throws InvalidArgumentException
*/
private function readFile(string $content): string
public static function fromFile(string $filename, string $passphrase = ''): self
{
try {
$file = new SplFileObject(substr($content, 7));
$file = new SplFileObject($filename);
$content = $file->fread($file->getSize());
assert(is_string($content));

return $content;
} catch (Throwable $exception) {
throw new InvalidArgumentException('You must provide a valid key file', $exception->getCode(), $exception);
}

return new self($content, $passphrase);
}

public function getContent(): string
Expand Down
12 changes: 5 additions & 7 deletions test/unit/Signer/KeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ public function configureRootDir(): void
* @test
*
* @covers \Lcobucci\JWT\Signer\Key::__construct
* @covers \Lcobucci\JWT\Signer\Key::setContent
* @covers \Lcobucci\JWT\Signer\Key::readFile
* @covers \Lcobucci\JWT\Signer\Key::fromFile
*/
public function constructShouldRaiseExceptionWhenFileDoesNotExists(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('You must provide a valid key file');

new Key('file://' . vfsStream::url('root/test2.pem'));
Key::fromFile(vfsStream::url('root/test2.pem'));
}

/**
Expand All @@ -55,13 +54,12 @@ public function getContentShouldReturnConfiguredData(): void
* @test
*
* @covers \Lcobucci\JWT\Signer\Key::__construct
* @covers \Lcobucci\JWT\Signer\Key::setContent
* @covers \Lcobucci\JWT\Signer\Key::readFile
* @covers \Lcobucci\JWT\Signer\Key::fromFile
* @covers \Lcobucci\JWT\Signer\Key::getContent
*/
public function getContentShouldReturnFileContentsWhenFilePathHasBeenPassed(): void
public function getContentShouldReturnFileContentsWhenReadFromFile(): void
{
$key = new Key('file://' . vfsStream::url('root/test.pem'));
$key = Key::fromFile(vfsStream::url('root/test.pem'));

self::assertSame('testing', $key->getContent());
}
Expand Down