AbstractKeyValuable   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 18
eloc 39
dl 0
loc 91
ccs 0
cts 36
cp 0
rs 10
c 4
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setCommonKeyDirectoryPath() 0 7 2
A getCommonKeyDirectoryPath() 0 2 1
A _getAccountIdentifier() 0 5 2
A getKeyDirectoryPath() 0 3 1
A _clearKeyDirectory() 0 8 3
A _getKeyDirectoryPath() 0 7 2
B _initKeyDirectory() 0 36 7
1
<?php
2
3
namespace LE_ACME2;
4
5
use LE_ACME2\Connector\Connector;
6
7
abstract class AbstractKeyValuable {
8
9
    const KEY_TYPE_RSA = "RSA";
10
    const KEY_TYPE_EC = "EC";
11
12
    protected $_identifier;
13
14
    protected static $_directoryPath = null;
15
16
    public static function setCommonKeyDirectoryPath(string $directoryPath) {
17
18
        if(!file_exists($directoryPath)) {
19
            throw new \RuntimeException('Common Key Directory Path does not exist');
20
        }
21
22
        self::$_directoryPath = realpath($directoryPath) . DIRECTORY_SEPARATOR;
23
    }
24
25
    public static function getCommonKeyDirectoryPath() : ?string {
26
        return self::$_directoryPath;
27
    }
28
29
    protected function _getKeyDirectoryPath(string $appendix = '') : string {
30
        
31
        if(self::$_directoryPath === null) {
32
            throw new \RuntimeException('Common Key Directory Path is not set');
33
        }
34
35
        return self::$_directoryPath . $this->_identifier . $appendix . DIRECTORY_SEPARATOR;
36
    }
37
38
    public function getKeyDirectoryPath() : string {
39
40
        return $this->_getKeyDirectoryPath('');
41
    }
42
43
    protected function _initKeyDirectory(string $keyType = self::KEY_TYPE_RSA, bool $ignoreIfKeysExist = false) {
44
45
        if(!file_exists($this->getKeyDirectoryPath())) {
46
47
            mkdir($this->getKeyDirectoryPath());
48
        }
49
50
        if(!$ignoreIfKeysExist && (
51
                file_exists($this->getKeyDirectoryPath() . 'private.pem') ||
52
                file_exists($this->getKeyDirectoryPath() . 'public.pem')
53
            )
54
        ) {
55
56
            throw new \RuntimeException(
57
                'Keys exist already. Exists the ' . get_class($this) . ' already?' . PHP_EOL .
58
                'Path: ' . $this->getKeyDirectoryPath()
59
            );
60
        }
61
62
        if($keyType == self::KEY_TYPE_RSA) {
63
64
            Utilities\KeyGenerator::RSA(
65
                $this->getKeyDirectoryPath(),
66
                'private.pem',
67
                'public.pem'
68
            );
69
        } else if($keyType == self::KEY_TYPE_EC) {
70
71
            Utilities\KeyGenerator::EC(
72
                $this->getKeyDirectoryPath(),
73
                'private.pem',
74
                'public.pem'
75
            );
76
        } else {
77
78
            throw new \RuntimeException('Key type "' . $keyType . '" not supported.');
79
        }
80
    }
81
82
    protected function _clearKeyDirectory() {
83
84
        if(file_exists($this->getKeyDirectoryPath() . 'private.pem')) {
85
            unlink($this->getKeyDirectoryPath() . 'private.pem');
86
        }
87
88
        if(file_exists($this->getKeyDirectoryPath() . 'public.pem')) {
89
            unlink($this->getKeyDirectoryPath() . 'public.pem');
90
        }
91
    }
92
93
    protected function _getAccountIdentifier(Account $account) : string {
94
95
        $staging = Connector::getInstance()->isUsingStagingServer();
0 ignored issues
show
Bug introduced by
It seems like isUsingStagingServer() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        $staging = Connector::getInstance()->/** @scrutinizer ignore-call */ isUsingStagingServer();
Loading history...
96
97
        return 'account_' . ($staging ? 'staging_' : 'live_') . $account->getEmail();
98
    }
99
}