Account::create()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 23
ccs 10
cts 10
cp 1
rs 9.8333
c 1
b 0
f 0
cc 2
nc 3
nop 1
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
            static::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
     * @throws Exception\AbstractException
39
     */
40
    public static function create(string $email) : Account {
41
42 3
        Utilities\Logger::getInstance()->add(
43
            Utilities\Logger::LEVEL_INFO,
44 3
            static::class . '::' . __FUNCTION__ .  ' email: "' . $email . '"'
45
        );
46 3
47
        $account = new self($email);
48
        $account->_initKeyDirectory();
49 3
50 3
        $request = new Request\Account\Create($account);
51
52 3
        try {
53
            $response = $request->getResponse();
54
55 3
            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

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