-
-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathKey.php
69 lines (58 loc) · 1.43 KB
/
Key.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
namespace Lcobucci\JWT\Signer;
use InvalidArgumentException;
use SplFileObject;
use Throwable;
use function assert;
use function is_string;
use function strpos;
use function substr;
final class Key
{
/**
* @var string
*/
private $content;
/**
* @var string
*/
private $passphrase;
public function __construct(string $content, string $passphrase = '')
{
$this->setContent($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
{
try {
$file = new SplFileObject(substr($content, 7));
$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);
}
}
public function getContent(): string
{
return $this->content;
}
public function getPassphrase(): string
{
return $this->passphrase;
}
}