| @@ 9-86 (lines=78) @@ | ||
| 6 | use Bunq\Certificate\CertificateType; |
|
| 7 | use Bunq\Certificate\DefaultCertificate; |
|
| 8 | ||
| 9 | final class FileCertificateStorage implements CertificateStorage |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * @var string |
|
| 13 | */ |
|
| 14 | private $path; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * @var array |
|
| 18 | */ |
|
| 19 | private $cache = []; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * @param string $path |
|
| 23 | */ |
|
| 24 | public function __construct($path) |
|
| 25 | { |
|
| 26 | $this->path = (string)$path . '/certificates'; |
|
| 27 | } |
|
| 28 | ||
| 29 | /** |
|
| 30 | * {@inheritdoc} |
|
| 31 | */ |
|
| 32 | public function load(CertificateType $certificateType) |
|
| 33 | { |
|
| 34 | if (!file_exists($this->path . '/' . $certificateType->toString())) { |
|
| 35 | throw new CertificateNotFoundException($certificateType, $this->path); |
|
| 36 | } |
|
| 37 | ||
| 38 | $certificate = $this->loadCertificate($certificateType); |
|
| 39 | ||
| 40 | return DefaultCertificate::fromString($certificate); |
|
| 41 | } |
|
| 42 | ||
| 43 | /** |
|
| 44 | * {@inheritdoc} |
|
| 45 | */ |
|
| 46 | public function save(Certificate $certificate, CertificateType $certificateType) |
|
| 47 | { |
|
| 48 | $certificate = trim($certificate->toString()); |
|
| 49 | ||
| 50 | if (!file_exists($this->path)) { |
|
| 51 | mkdir($this->path); |
|
| 52 | } |
|
| 53 | ||
| 54 | file_put_contents($this->path . '/' . $certificateType, $certificate); |
|
| 55 | ||
| 56 | // save server |
|
| 57 | $this->cache[$certificateType->toString()] = $certificate; |
|
| 58 | } |
|
| 59 | ||
| 60 | /** |
|
| 61 | * @param CertificateType $certificateType |
|
| 62 | * |
|
| 63 | * @return string |
|
| 64 | * |
|
| 65 | * @throws CertificateNotFoundException |
|
| 66 | */ |
|
| 67 | private function loadCertificate(CertificateType $certificateType) |
|
| 68 | { |
|
| 69 | // take from cache, filesystems are slow |
|
| 70 | if (isset($this->cache[$certificateType->toString()])) { |
|
| 71 | return $this->cache[$certificateType->toString()]; |
|
| 72 | } |
|
| 73 | ||
| 74 | // load from file system |
|
| 75 | $certificate = file_get_contents($this->path . '/' . $certificateType->toString()); |
|
| 76 | ||
| 77 | if (!$certificate) { |
|
| 78 | throw new CertificateNotFoundException($certificateType, $this->path); |
|
| 79 | } |
|
| 80 | ||
| 81 | // save the result in the cache |
|
| 82 | $this->cache[$certificateType->toString()] = $certificate; |
|
| 83 | ||
| 84 | return $certificate; |
|
| 85 | } |
|
| 86 | } |
|
| 87 | ||
| @@ 9-84 (lines=76) @@ | ||
| 6 | use Bunq\Token\Token; |
|
| 7 | use Bunq\Token\TokenType; |
|
| 8 | ||
| 9 | final class FileTokenStorage implements TokenStorage |
|
| 10 | { |
|
| 11 | /** |
|
| 12 | * @var string |
|
| 13 | */ |
|
| 14 | private $path; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * @var array |
|
| 18 | */ |
|
| 19 | private $cache = []; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * @param string $path |
|
| 23 | */ |
|
| 24 | public function __construct($path) |
|
| 25 | { |
|
| 26 | $this->path = (string)$path . '/tokens'; |
|
| 27 | } |
|
| 28 | ||
| 29 | /** |
|
| 30 | * @inheritdoc |
|
| 31 | */ |
|
| 32 | public function load(TokenType $type) |
|
| 33 | { |
|
| 34 | if (!file_exists($this->path . '/' . $type->toString())) { |
|
| 35 | throw new TokenNotFoundException($type, $this->path); |
|
| 36 | } |
|
| 37 | ||
| 38 | $token = $this->loadToken($type); |
|
| 39 | ||
| 40 | return DefaultToken::fromString($token); |
|
| 41 | } |
|
| 42 | ||
| 43 | /** |
|
| 44 | * @inheritdoc |
|
| 45 | */ |
|
| 46 | public function save(Token $token, TokenType $type) |
|
| 47 | { |
|
| 48 | $token = trim($token->toString()); |
|
| 49 | ||
| 50 | if (!file_exists($this->path)) { |
|
| 51 | mkdir($this->path); |
|
| 52 | } |
|
| 53 | ||
| 54 | file_put_contents($this->path . '/' . $type->toString(), $token); |
|
| 55 | ||
| 56 | $this->cache[$type->toString()] = $token; |
|
| 57 | } |
|
| 58 | ||
| 59 | /** |
|
| 60 | * @param TokenType $type |
|
| 61 | * |
|
| 62 | * @return string |
|
| 63 | * |
|
| 64 | * @throws TokenNotFoundException |
|
| 65 | */ |
|
| 66 | private function loadToken(TokenType $type) |
|
| 67 | { |
|
| 68 | // take from cache, filesystems are slow |
|
| 69 | if (isset($this->cache[$type->toString()])) { |
|
| 70 | return $this->cache[$type->toString()]; |
|
| 71 | } |
|
| 72 | ||
| 73 | $token = trim(file_get_contents($this->path . '/' . $type->toString())); |
|
| 74 | ||
| 75 | if (!$token) { |
|
| 76 | throw new TokenNotFoundException($type, $this->path); |
|
| 77 | } |
|
| 78 | ||
| 79 | // save the result in the cache |
|
| 80 | $this->cache[$type->toString()] = $token; |
|
| 81 | ||
| 82 | return $token; |
|
| 83 | } |
|
| 84 | } |
|
| 85 | ||