Completed
Pull Request — master (#1)
by John
05:30
created

FilesystemCertificateStorage::getCertificate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 FilesystemCertificateStorage implements CertificateStorageInterface
12
{
13
    private $dir;
14
15 104
    public function __construct($dir = null)
16
    {
17 104
        $this->dir = $dir ?? getcwd().DIRECTORY_SEPARATOR.'certificates';
18
19 104
        if (!is_dir($this->dir)) {
20 100
            /** @scrutinizer ignore-unhandled */ @mkdir($this->dir);
21
        }
22 104
        if (!is_writable($this->dir)) {
23 2
            throw new RuntimeException("{$this->dir} is not writable");
24
        }
25 102
    }
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 70
    private function getDomainKey($domain, $suffix)
61
    {
62 70
        return str_replace('*', 'wildcard', $domain).'.'.$suffix;
63
    }
64
    /**
65
     * @inheritdoc
66
     */
67 18
    public function getCertificate($domain)
68
    {
69 18
        return $this->getMetadata($this->getDomainKey($domain, 'crt'));
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75 28
    public function setCertificate($domain, $certificate)
76
    {
77 28
        $this->setMetadata($this->getDomainKey($domain, 'crt'), $certificate);
78 28
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83 2
    public function getFullChainCertificate($domain)
84
    {
85 2
        return $this->getMetadata($this->getDomainKey($domain, 'fullchain.crt'));
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91 28
    public function setFullChainCertificate($domain, $certificate)
92
    {
93 28
        $this->setMetadata($this->getDomainKey($domain, 'fullchain.crt'), $certificate);
94 28
    }
95
96
    /**
97
     * @inheritdoc
98
     */
99 70
    public function getPrivateKey($domain)
100
    {
101 70
        return $this->getMetadata($this->getDomainKey($domain, 'key'));
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 62
    public function setPrivateKey($domain, $key)
108
    {
109 62
        $this->setMetadata($this->getDomainKey($domain, 'key'), $key);
110 62
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115 68
    public function getPublicKey($domain)
116
    {
117 68
        return $this->getMetadata($this->getDomainKey($domain, 'public'));
118
    }
119
120
    /**
121
     * @inheritdoc
122
     */
123 60
    public function setPublicKey($domain, $key)
124
    {
125 60
        $this->setMetadata($this->getDomainKey($domain, 'public'), $key);
126 60
    }
127
128 88
    private function getMetadataFilename($key)
129
    {
130 88
        $key=str_replace('*', 'wildcard', $key);
131 88
        $file=$this->dir.DIRECTORY_SEPARATOR.$key;
132 88
        return $file;
133
    }
134
    /**
135
     * @inheritdoc
136
     */
137 84
    public function getMetadata($key)
138
    {
139 84
        $file=$this->getMetadataFilename($key);
140 84
        if (!file_exists($file)) {
141 80
            return null;
142
        }
143 54
        return file_get_contents($file);
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149 88
    public function setMetadata($key, $value)
150
    {
151 88
        $file=$this->getMetadataFilename($key);
152 88
        if (is_null($value)) {
153
            //nothing to store, ensure file is removed
154 12
            if (file_exists($file)) {
155 12
                unlink($file);
156
            }
157
        } else {
158 88
            file_put_contents($file, $value);
159
        }
160 88
    }
161
162
    /**
163
     * @inheritdoc
164
     */
165 2
    public function hasMetadata($key)
166
    {
167 2
        $file=$this->getMetadataFilename($key);
168 2
        return file_exists($file);
169
    }
170
}
171