From d2ef9f45893b753a58094a13b933a4b117298214 Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Tue, 26 Nov 2019 15:48:07 -0600 Subject: [PATCH 1/2] Adds static fromFile, removes readFile --- src/Signer/Key.php | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/Signer/Key.php b/src/Signer/Key.php index a468c342..18eddef6 100644 --- a/src/Signer/Key.php +++ b/src/Signer/Key.php @@ -8,8 +8,6 @@ use Throwable; use function assert; use function is_string; -use function strpos; -use function substr; final class Key { @@ -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 From 26411d1d77696a743d44d8a2856d7bad647d206c Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Tue, 26 Nov 2019 16:05:29 -0600 Subject: [PATCH 2/2] Update tests --- test/unit/Signer/KeyTest.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/test/unit/Signer/KeyTest.php b/test/unit/Signer/KeyTest.php index 150d0078..6fd23149 100644 --- a/test/unit/Signer/KeyTest.php +++ b/test/unit/Signer/KeyTest.php @@ -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')); } /** @@ -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()); }