FilesystemAccountStorage   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 90.63%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 80
ccs 29
cts 32
cp 0.9063
rs 10
c 1
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setAccountPrivateKey() 0 3 1
A getAccountPublicKey() 0 3 1
A setAccountPublicKey() 0 3 1
A getMetadataFilename() 0 5 1
A getAccountPrivateKey() 0 3 1
A getMetadata() 0 7 2
A setMetadata() 0 10 3
A __construct() 0 9 3
1
<?php
2
3
namespace Zwartpet\PHPCertificateToolbox;
4
5
use Zwartpet\PHPCertificateToolbox\Exception\RuntimeException;
6
7
/**
8
 * A default storage implementation which stores information in a local filesystem
9
 * @package Zwartpet\PHPCertificateToolbox
10
 */
11
class FilesystemAccountStorage implements AccountStorageInterface
12
{
13
    private $dir;
14
15 100
    public function __construct($dir = null)
16
    {
17 100
        $this->dir = $dir ?? getcwd().DIRECTORY_SEPARATOR.'account';
18
19 100
        if (!is_dir($this->dir)) {
20 96
            /** @scrutinizer ignore-unhandled */ @mkdir($this->dir);
21
        }
22 100
        if (!is_writable($this->dir)) {
23
            throw new RuntimeException("{$this->dir} is not writable");
24
        }
25 100
    }
26
27
28
    /**
29
     * @inheritdoc
30
     */
31 14
    public function getAccountPublicKey()
32
    {
33 14
        return $this->getMetadata('account.public');
34
    }
35
36
    /**
37
     * @inheritdoc
38
     */
39 88
    public function setAccountPublicKey($key)
40
    {
41 88
        $this->setMetadata('account.public', $key);
42 88
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47 16
    public function getAccountPrivateKey()
48
    {
49 16
        return $this->getMetadata('account.key');
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55 88
    public function setAccountPrivateKey($key)
56
    {
57 88
        $this->setMetadata('account.key', $key);
58 88
    }
59
60 88
    private function getMetadataFilename($key)
61
    {
62 88
        $key=str_replace('*', 'wildcard', $key);
63 88
        $file=$this->dir.DIRECTORY_SEPARATOR.$key;
64 88
        return $file;
65
    }
66
    /**
67
     * @inheritdoc
68
     */
69 22
    public function getMetadata($key)
70
    {
71 22
        $file=$this->getMetadataFilename($key);
72 22
        if (!file_exists($file)) {
73 14
            return null;
74
        }
75 16
        return file_get_contents($file);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81 88
    public function setMetadata($key, $value)
82
    {
83 88
        $file=$this->getMetadataFilename($key);
84 88
        if (is_null($value)) {
85
            //nothing to store, ensure file is removed
86
            if (file_exists($file)) {
87
                unlink($file);
88
            }
89
        } else {
90 88
            file_put_contents($file, $value);
91
        }
92 88
    }
93
}
94