Test Failed
Push — master ( 9ed1f2...960543 )
by Fabian
02:06
created

Account   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 95.31%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
eloc 64
c 2
b 0
f 0
dl 0
loc 158
ccs 61
cts 64
cp 0.9531
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getEmail() 0 3 1
A exists() 0 7 3
A _setEmail() 0 4 1
A update() 0 18 3
A getData() 0 4 1
A changeKeys() 0 17 2
A create() 0 23 2
A __construct() 0 9 1
A get() 0 13 2
A deactivate() 0 11 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 5
    public function __construct(string $email) {
15
16 5
        $this->_setEmail($email);
17
18 5
        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 5
            get_class() . '::' . __FUNCTION__ .
21
            ' email: "' . $email . '" ' .
22 5
            ' path: ' . $this->getKeyDirectoryPath()
23
        );
24
    }
25
26 5
    private function _setEmail(string $email) {
27
28 5
        $this->_email = $email;
29 5
        $this->_identifier = $this->_getAccountIdentifier($this);
30
    }
31
32 5
    public function getEmail() : string {
33
34 5
        return $this->_email;
35
    }
36
37
    /**
38
     * @param string $email
39
     * @return Account|null
40
     * @throws Exception\AbstractException
41
     */
42 2
    public static function create(string $email) : Account {
43
44 2
        Utilities\Logger::getInstance()->add(
45
            Utilities\Logger::LEVEL_INFO,
46 2
            get_class() . '::' . __FUNCTION__ .  ' email: "' . $email . '"'
47
        );
48
49 2
        $account = new self($email);
50 2
        $account->_initKeyDirectory();
51
52 2
        $request = new Request\Account\Create($account);
53
54
        try {
55 2
            $response = $request->getResponse();
56
57 1
            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 1
            return $account;
60
61 1
        } catch(Exception\AbstractException $e) {
62
63 1
            $account->_clearKeyDirectory();
64 1
            throw $e;
65
        }
66
    }
67
68 4
    public static function exists(string $email) : bool {
69
70 4
        $account = new self($email);
71
72 4
        return  file_exists($account->getKeyDirectoryPath()) &&
73 4
                file_exists($account->getKeyDirectoryPath() . 'private.pem') &&
74 4
                file_exists($account->getKeyDirectoryPath() . 'public.pem');
75
    }
76
77 4
    public static function get(string $email) : Account {
78
79 4
        Utilities\Logger::getInstance()->add(
80
            Utilities\Logger::LEVEL_INFO,
81 4
            get_class() . '::' . __FUNCTION__ .  ' email: "' . $email . '"'
82
        );
83
84 4
        $account = new self($email);
85
86 4
        if(!self::exists($email))
87 1
            throw new \RuntimeException('Keys not found - does this account exist?');
88
89 3
        return $account;
90
    }
91
92
    /**
93
     * @return Response\AbstractResponse|Response\Account\GetData
94
     * @throws Exception\InvalidResponse
95
     * @throws Exception\RateLimitReached
96
     */
97 1
    public function getData() : Response\Account\GetData {
98
99 1
        $request = new Request\Account\GetData($this);
100 1
        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 1
    public function changeKeys() : bool {
134
135 1
        Utilities\KeyGenerator::RSA($this->getKeyDirectoryPath(), 'private-replacement.pem', 'public-replacement.pem');
136
137 1
        $request = new Request\Account\ChangeKeys($this);
138
        try {
139 1
            /* $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
        } catch(Exception\InvalidResponse $e) {
148
149
            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
            return true;
165
166 1
        } catch(Exception\InvalidResponse $e) {
167 1
            return false;
168
        }
169
    }
170
}