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