ModuleOptions::setTimeout()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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