From 8e80bec6ad37421b93e1e9b088fbf3767ab6840b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Sun, 29 Jul 2018 16:33:04 +0200 Subject: [PATCH] Refactor solution to use SplFileObject Which is simpler and doesn't require the use of the `@` operator. --- src/Signer/Key.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Signer/Key.php b/src/Signer/Key.php index b01dff6b..7b727aff 100644 --- a/src/Signer/Key.php +++ b/src/Signer/Key.php @@ -7,7 +7,9 @@ namespace Lcobucci\JWT\Signer; +use Exception; use InvalidArgumentException; +use SplFileObject; /** * @author Luís Otávio Cobucci Oblonczyk @@ -58,18 +60,13 @@ private function setContent($content) */ private function readFile($content) { - $file = substr($content, 7); + try { + $file = new SplFileObject(substr($content, 7)); - if (!is_readable($file)) { - throw new InvalidArgumentException('You must inform a valid key file'); + return $file->fread($file->getSize()); + } catch (Exception $exception) { + throw new InvalidArgumentException('You must inform a valid key file', 0, $exception); } - - $content = @file_get_contents($file); - if ($content === false) { - throw new InvalidArgumentException('You must inform a valid key file'); - } - - return $content; } /**