Passed
Push — master ( 044b78...4447be )
by Fabian
02:11
created

Account::changeKeys()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.9
c 1
b 0
f 0
cc 2
nc 6
nop 0
crap 2
1
<?php
2
namespace LE_ACME2;
3
4
use LE_ACME2\Request;
5
use LE_ACME2\Response;
6
7
use LE_ACME2\Utilities;
8
use LE_ACME2\Exception;
9
10
class Account extends AbstractKeyValuable {
11
12
    private $_email = NULL;
13
14 7
    public function __construct(string $email) {
15
16 7
        $this->_setEmail($email);
17
18 7
        Utilities\Logger::getInstance()->add(
0 ignored issues
show
Bug introduced by
It seems like add() 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

18
        Utilities\Logger::getInstance()->/** @scrutinizer ignore-call */ add(
Loading history...
19
            Utilities\Logger::LEVEL_DEBUG,
20 7
            get_class() . '::' . __FUNCTION__ .
21
            ' email: "' . $email . '" ' .
22 7
            ' path: ' . $this->getKeyDirectoryPath()
23
        );
24
    }
25
26 7
    private function _setEmail(string $email) {
27
28 7
        $this->_email = $email;
29 7
        $this->_identifier = $this->_getAccountIdentifier($this);
30
    }
31
32 7
    public function getEmail() : string {
33
34 7
        return $this->_email;
35
    }
36
37
    /**
38
     * @param string $email
39
     * @return Account|null
40
     * @throws Exception\AbstractException
41
     */
42 3
    public static function create(string $email) : Account {
43
44 3
        Utilities\Logger::getInstance()->add(
45
            Utilities\Logger::LEVEL_INFO,
46 3
            get_class() . '::' . __FUNCTION__ .  ' email: "' . $email . '"'
47
        );
48
49 3
        $account = new self($email);
50 3
        $account->_initKeyDirectory();
51
52 3
        $request = new Request\Account\Create($account);
53
54
        try {
55 3
            $response = $request->getResponse();
56
57 2
            Cache\AccountResponse::getInstance()->set($account, $response);
0 ignored issues
show
Bug introduced by
It seems like set() 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

57
            Cache\AccountResponse::getInstance()->/** @scrutinizer ignore-call */ set($account, $response);
Loading history...
58
59 2
            return $account;
60
61 1
        } catch(Exception\AbstractException $e) {
62
63 1
            $account->_clearKeyDirectory();
64 1
            throw $e;
65
        }
66
    }
67
68 6
    public static function exists(string $email) : bool {
69
70 6
        $account = new self($email);
71
72 6
        return  file_exists($account->getKeyDirectoryPath()) &&
73 6
                file_exists($account->getKeyDirectoryPath() . 'private.pem') &&
74 6
                file_exists($account->getKeyDirectoryPath() . 'public.pem');
75
    }
76
77 6
    public static function get(string $email) : Account {
78
79 6
        Utilities\Logger::getInstance()->add(
80
            Utilities\Logger::LEVEL_INFO,
81 6
            get_class() . '::' . __FUNCTION__ .  ' email: "' . $email . '"'
82
        );
83
84 6
        $account = new self($email);
85
86 6
        if(!self::exists($email))
87 1
            throw new \RuntimeException('Keys not found - does this account exist?');
88
89 5
        return $account;
90
    }
91
92
    /**
93
     * @return Response\AbstractResponse|Response\Account\GetData
94
     * @throws Exception\InvalidResponse
95
     * @throws Exception\RateLimitReached
96
     */
97 2
    public function getData() : Response\Account\GetData {
98
99 2
        $request = new Request\Account\GetData($this);
100 2
        return $request->getResponse();
101
    }
102
103
    /**
104
     * @param string $email
105
     * @return bool
106
     * @throws Exception\RateLimitReached
107
     */
108 1
    public function update(string $email) : bool {
109
110 1
        $request = new Request\Account\Update($this, $email);
111
112
        try {
113 1
            /* $response = */ $request->getResponse();
114
115 1
            $previousKeyDirectoryPath = $this->getKeyDirectoryPath();
116
117 1
            $this->_setEmail($email);
118
119 1
            if($previousKeyDirectoryPath != $this->getKeyDirectoryPath())
120 1
                rename($previousKeyDirectoryPath, $this->getKeyDirectoryPath());
121
122 1
            return true;
123
124 1
        } catch(Exception\InvalidResponse $e) {
125 1
            return false;
126
        }
127
    }
128
129
    /**
130
     * @return bool
131
     * @throws Exception\RateLimitReached
132
     */
133 2
    public function changeKeys() : bool {
134
135 2
        Utilities\KeyGenerator::RSA($this->getKeyDirectoryPath(), 'private-replacement.pem', 'public-replacement.pem');
136
137 2
        $request = new Request\Account\ChangeKeys($this);
138
        try {
139 2
            /* $response = */ $request->getResponse();
140
141 1
            unlink($this->getKeyDirectoryPath() . 'private.pem');
142 1
            unlink($this->getKeyDirectoryPath() . 'public.pem');
143 1
            rename($this->getKeyDirectoryPath() . 'private-replacement.pem', $this->getKeyDirectoryPath() . 'private.pem');
144 1
            rename($this->getKeyDirectoryPath() . 'public-replacement.pem', $this->getKeyDirectoryPath() . 'public.pem');
145 1
            return true;
146
147 1
        } catch(Exception\InvalidResponse $e) {
148
149 1
            return false;
150
        }
151
    }
152
153
    /**
154
     * @return bool
155
     * @throws Exception\RateLimitReached
156
     */
157 1
    public function deactivate() : bool {
158
159 1
        $request = new Request\Account\Deactivate($this);
160
161
        try {
162 1
            /* $response = */ $request->getResponse();
163
164 1
            return true;
165
166 1
        } catch(Exception\InvalidResponse $e) {
167 1
            return false;
168
        }
169
    }
170
}