Passed
Push — master ( c2faf8...afa411 )
by Al3x
11:51
created

AuthOptions::isAuthorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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