|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace InvoiceNinjaModule\Options; |
|
6
|
|
|
|
|
7
|
|
|
use InvoiceNinjaModule\Exception\InvalidParameterException; |
|
8
|
|
|
use InvoiceNinjaModule\Module; |
|
9
|
|
|
use InvoiceNinjaModule\Options\Interfaces\AuthOptionsInterface; |
|
10
|
|
|
use Laminas\Http\Client; |
|
11
|
|
|
|
|
12
|
|
|
use function array_key_exists; |
|
13
|
|
|
|
|
14
|
|
|
final class AuthOptions implements AuthOptionsInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private bool $isAuthorization = false; |
|
17
|
|
|
private string $authType = 'none'; |
|
18
|
|
|
private string $username = ''; |
|
19
|
|
|
private string $password = ''; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Config constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $config |
|
25
|
|
|
* |
|
26
|
|
|
* @throws InvalidParameterException |
|
27
|
|
|
*/ |
|
28
|
11 |
|
public function __construct(array $config) |
|
29
|
|
|
{ |
|
30
|
11 |
|
if (!empty($config)) { |
|
31
|
8 |
|
$this->checkAuthorization($config); |
|
32
|
6 |
|
$this->setAuthType($config); |
|
33
|
6 |
|
$this->checkCredentials($config); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function isAuthorization(): bool |
|
41
|
|
|
{ |
|
42
|
4 |
|
return $this->isAuthorization; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function getAuthType(): string |
|
49
|
|
|
{ |
|
50
|
3 |
|
return $this->authType; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function getUsername(): string |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->username; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function getPassword(): string |
|
65
|
|
|
{ |
|
66
|
2 |
|
return $this->password; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param array $config |
|
71
|
|
|
* |
|
72
|
|
|
* @throws InvalidParameterException |
|
73
|
|
|
*/ |
|
74
|
6 |
|
private function checkCredentials(array $config): void |
|
75
|
|
|
{ |
|
76
|
6 |
|
$this->checkUsername($config[$this->authType]); |
|
77
|
4 |
|
$this->checkPassword($config[$this->authType]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array $config |
|
82
|
|
|
* |
|
83
|
|
|
* @throws InvalidParameterException |
|
84
|
|
|
*/ |
|
85
|
6 |
|
private function checkUsername(array $config): void |
|
86
|
|
|
{ |
|
87
|
6 |
|
if (!array_key_exists(Module::AUTH_USER, $config) || empty($config[Module::AUTH_USER])) { |
|
88
|
2 |
|
throw new InvalidParameterException('Username must not be empty!'); |
|
89
|
|
|
} |
|
90
|
4 |
|
$this->username = $config[Module::AUTH_USER]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param array $config |
|
95
|
|
|
* |
|
96
|
|
|
* @throws InvalidParameterException |
|
97
|
|
|
*/ |
|
98
|
4 |
|
private function checkPassword(array $config): void |
|
99
|
|
|
{ |
|
100
|
4 |
|
if (!array_key_exists(Module::AUTH_PASS, $config) || empty($config[Module::AUTH_PASS])) { |
|
101
|
2 |
|
throw new InvalidParameterException('Password must not be empty!'); |
|
102
|
|
|
} |
|
103
|
2 |
|
$this->password = $config[Module::AUTH_PASS]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param array $config |
|
108
|
|
|
*/ |
|
109
|
6 |
|
private function setAuthType(array $config): void |
|
110
|
|
|
{ |
|
111
|
6 |
|
$this->authType = Client::AUTH_BASIC; |
|
112
|
6 |
|
if (array_key_exists(Client::AUTH_DIGEST, $config)) { |
|
113
|
1 |
|
$this->authType = Client::AUTH_DIGEST; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param array $config |
|
119
|
|
|
* |
|
120
|
|
|
* @throws InvalidParameterException |
|
121
|
|
|
*/ |
|
122
|
8 |
|
private function checkAuthorization(array $config): void |
|
123
|
|
|
{ |
|
124
|
8 |
|
if (count($config) > 1) { |
|
125
|
1 |
|
throw new InvalidParameterException('Only one authorization config allowed!'); |
|
126
|
|
|
} |
|
127
|
7 |
|
if (!array_key_exists(Client::AUTH_BASIC, $config) && !array_key_exists(Client::AUTH_DIGEST, $config)) { |
|
128
|
1 |
|
throw new InvalidParameterException('Only BASIC or DIGEST authorization allowed!'); |
|
129
|
|
|
} |
|
130
|
6 |
|
$this->isAuthorization = true; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|