Completed
Push — master ( 7a3efc...f79493 )
by Al3x
02:58
created

ModuleOptions::setTimeout()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

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