1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace InvoiceNinjaModule\Options; |
5
|
|
|
|
6
|
|
|
use InvoiceNinjaModule\Exception\InvalidParameterException; |
7
|
|
|
use InvoiceNinjaModule\Options\Interfaces\AuthOptionsInterface; |
8
|
|
|
use InvoiceNinjaModule\Options\Interfaces\ModuleOptionsInterface; |
9
|
|
|
use InvoiceNinjaModule\Module; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ModuleOptions |
13
|
|
|
*/ |
14
|
|
|
final class ModuleOptions implements ModuleOptionsInterface |
15
|
|
|
{ |
16
|
|
|
private AuthOptionsInterface $authOptions; |
17
|
|
|
private string $token; |
18
|
|
|
private string $tokenType; |
19
|
|
|
private int $timeout; |
20
|
|
|
private string $hostUrl; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* ModuleOptions constructor. |
24
|
|
|
* |
25
|
|
|
* @param array $config |
26
|
|
|
* @param AuthOptionsInterface $authOptions |
27
|
|
|
* |
28
|
|
|
* @throws InvalidParameterException |
29
|
|
|
*/ |
30
|
13 |
|
public function __construct(array $config, AuthOptionsInterface $authOptions) |
31
|
|
|
{ |
32
|
13 |
|
if (empty($config)) { |
33
|
1 |
|
throw new InvalidParameterException('No config provided!'); |
34
|
|
|
} |
35
|
12 |
|
$this->setToken($config); |
36
|
10 |
|
$this->setTokenType($config); |
37
|
8 |
|
$this->setTimeout($config); |
38
|
5 |
|
$this->setHostUrl($config); |
39
|
3 |
|
$this->authOptions = $authOptions; |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
1 |
|
public function getToken() :string |
46
|
|
|
{ |
47
|
1 |
|
return $this->token; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
1 |
|
public function getTokenType() :string |
54
|
|
|
{ |
55
|
1 |
|
return $this->tokenType; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
2 |
|
public function getTimeout() :int |
62
|
|
|
{ |
63
|
2 |
|
return $this->timeout; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
1 |
|
public function getHostUrl() :string |
70
|
|
|
{ |
71
|
1 |
|
return $this->hostUrl; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return AuthOptionsInterface |
76
|
|
|
*/ |
77
|
2 |
|
public function getAuthOptions() : AuthOptionsInterface |
78
|
|
|
{ |
79
|
2 |
|
return $this->authOptions; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $config |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
* @throws InvalidParameterException |
87
|
|
|
*/ |
88
|
12 |
|
private function setToken(array $config) :void |
89
|
|
|
{ |
90
|
12 |
|
if (!\array_key_exists(Module::TOKEN, $config) || empty($config[Module::TOKEN])) { |
91
|
2 |
|
throw new InvalidParameterException('No or empty token provided!'); |
92
|
|
|
} |
93
|
10 |
|
$this->token = $config[Module::TOKEN]; |
94
|
10 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $config |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
* @throws InvalidParameterException |
101
|
|
|
*/ |
102
|
10 |
|
private function setTokenType(array $config) :void |
103
|
|
|
{ |
104
|
10 |
|
if (!\array_key_exists(Module::TOKEN_TYPE, $config) || empty($config[Module::TOKEN_TYPE])) { |
105
|
2 |
|
throw new InvalidParameterException('No or empty token type provided!'); |
106
|
|
|
} |
107
|
8 |
|
$this->tokenType = $config[Module::TOKEN_TYPE]; |
108
|
8 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array $config |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
* @throws InvalidParameterException |
115
|
|
|
*/ |
116
|
8 |
|
private function setTimeout(array $config) :void |
117
|
|
|
{ |
118
|
8 |
|
if (!\array_key_exists(Module::API_TIMEOUT, $config) |
119
|
7 |
|
|| !\is_int($config[Module::API_TIMEOUT]) |
120
|
8 |
|
|| $config[Module::API_TIMEOUT] < 0) { |
121
|
3 |
|
throw new InvalidParameterException('No or negative timeout provided!'); |
122
|
|
|
} |
123
|
5 |
|
$this->timeout = $config[Module::API_TIMEOUT]; |
124
|
5 |
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $config |
128
|
|
|
* |
129
|
|
|
* @return void |
130
|
|
|
* @throws InvalidParameterException |
131
|
|
|
*/ |
132
|
5 |
|
private function setHostUrl(array $config) :void |
133
|
|
|
{ |
134
|
5 |
|
if (!\array_key_exists(Module::HOST_URL, $config) || empty($config[Module::HOST_URL])) { |
135
|
2 |
|
throw new InvalidParameterException('No or empty host url provided!'); |
136
|
|
|
} |
137
|
3 |
|
$this->hostUrl = $config[Module::HOST_URL]; |
138
|
3 |
|
} |
139
|
|
|
} |
140
|
|
|
|