Skip to content

Commit

Permalink
Refactor solution to use SplFileObject
Browse files Browse the repository at this point in the history
Which is simpler and doesn't require the use of the `@` operator.
  • Loading branch information
lcobucci committed Jul 29, 2018
1 parent 8ba278b commit 8e80bec
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/Signer/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

namespace Lcobucci\JWT\Signer;

use Exception;
use InvalidArgumentException;
use SplFileObject;

/**
* @author Luís Otávio Cobucci Oblonczyk <[email protected]>
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 8e80bec

Please sign in to comment.