Passed
Pull Request — master (#1)
by John
02:11
created

setFullChainCertificate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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