FilesystemCertificateStorage::getMetadata()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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