Passed
Pull Request — master (#7)
by Sandro
02:02
created

ClientOptions   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Test Coverage

Coverage 72.72%

Importance

Changes 0
Metric Value
wmc 16
eloc 54
dl 0
loc 148
ccs 32
cts 44
cp 0.7272
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaults() 0 12 1
A offsetUnset() 0 3 1
A offsetSet() 0 3 1
A getSupportedAuthTypes() 0 3 1
A validate() 0 22 6
A offsetExists() 0 3 1
A toArray() 0 3 1
A offsetGet() 0 7 2
A __construct() 0 4 1
A getSupportedConnectionTypes() 0 3 1
1
<?php
2
/**
3
 * Sandro Keil (https://sandro-keil.de)
4
 *
5
 * @link      http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
6
 * @copyright Copyright (c) 2018-2019 Sandro Keil
7
 * @license   http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
8
 */
9
10
namespace ArangoDb\Http;
11
12
use ArangoDb\Exception\LogicException;
13
14
/**
15
 * Immutable client options array container
16
 */
17
final class ClientOptions implements \ArrayAccess
18
{
19
    // connection options
20
    public const OPTION_ENDPOINT = 'endpoint';
21
    public const OPTION_DATABASE = 'database';
22
    public const OPTION_TIMEOUT = 'timeout';
23
    public const OPTION_CONNECTION = 'Connection';
24
    public const OPTION_RECONNECT = 'Reconnect';
25
26
    // connection default options
27
    public const DEFAULT_CONNECTION = 'Keep-Alive';
28
    public const DEFAULT_TIMEOUT = 30;
29
30
    // auth options
31
    public const OPTION_AUTH_USER = 'AuthUser';
32
    public const OPTION_AUTH_PASSWD = 'AuthPasswd';
33
    public const OPTION_AUTH_TYPE = 'AuthType';
34
35
    // auth default options
36
    public const DEFAULT_AUTH_TYPE = 'Basic';
37
38
    // ssl options
39
    public const OPTION_VERIFY_CERT = 'verifyCert';
40
    public const OPTION_VERIFY_CERT_NAME = 'verifyCertName';
41
    public const OPTION_ALLOW_SELF_SIGNED = 'allowSelfSigned';
42
    public const OPTION_CIPHERS = 'ciphers';
43
44
    // ssl default options
45
    public const DEFAULT_VERIFY_CERT = false;
46
    public const DEFAULT_VERIFY_CERT_NAME = false;
47
    public const DEFAULT_ALLOW_SELF_SIGNED = true;
48
    public const DEFAULT_CIPHERS = 'DEFAULT';
49
50
    /**
51
     * The current options
52
     *
53
     * @var array
54
     */
55
    private $options;
56
57 42
    public function __construct(array $options)
58
    {
59 42
        $this->options = array_merge(self::getDefaults(), $options);
60 42
        $this->validate();
61 42
    }
62
63
    public function toArray(): array
64
    {
65
        return $this->options;
66
    }
67
68
    /**
69
     * Returns supported authorization types
70
     *
71
     * @return string[]
72
     */
73
    private static function getSupportedAuthTypes(): array
74
    {
75
        return ['Basic'];
76
    }
77
78
    /**
79
     * Returns supported connection types
80
     *
81
     * @return string[]
82
     */
83 42
    private static function getSupportedConnectionTypes(): array
84
    {
85 42
        return ['Close', 'Keep-Alive'];
86
    }
87
88 42
    private static function getDefaults(): array
89
    {
90
        return [
91 42
            self::OPTION_ENDPOINT                => '',
92 42
            self::OPTION_TIMEOUT                 => self::DEFAULT_TIMEOUT,
93 42
            self::OPTION_CONNECTION              => self::DEFAULT_CONNECTION,
94 42
            self::OPTION_VERIFY_CERT             => self::DEFAULT_VERIFY_CERT,
95 42
            self::OPTION_VERIFY_CERT_NAME        => self::DEFAULT_VERIFY_CERT_NAME,
96 42
            self::OPTION_ALLOW_SELF_SIGNED       => self::DEFAULT_ALLOW_SELF_SIGNED,
97 42
            self::OPTION_CIPHERS                 => self::DEFAULT_CIPHERS,
98 42
            self::OPTION_RECONNECT               => false,
99 42
            self::OPTION_DATABASE                => '_system',
100
        ];
101
    }
102
103 42
    private function validate(): void
104
    {
105 42
        $this->options[self::OPTION_ENDPOINT] = preg_replace(
106 42
            ['/^http:/', '/^https:/'],
107 42
            ['tcp:', 'ssl:'],
108 42
            $this->options[self::OPTION_ENDPOINT]
109
        );
110
111 42
        if (! isset($this->options[self::OPTION_ENDPOINT])) {
112
            throw new LogicException('Endpoint not specified');
113
        }
114
115 42
        if (isset($this->options[self::OPTION_AUTH_TYPE])
116 42
            && ! in_array($this->options[self::OPTION_AUTH_TYPE], self::getSupportedAuthTypes(), true)
117
        ) {
118
            throw new LogicException('Unsupported authorization method: ' . $this->options[self::OPTION_AUTH_TYPE]);
119
        }
120
121 42
        if (isset($this->options[self::OPTION_CONNECTION])
122 42
            && ! in_array($this->options[self::OPTION_CONNECTION], self::getSupportedConnectionTypes(), true)
123
        ) {
124
            throw new LogicException('Unsupported connection value: ' . $this->options[self::OPTION_CONNECTION]);
125
        }
126 42
    }
127
128
    /**
129
     * @param mixed $offset
130
     * @param mixed $value
131
     */
132
    public function offsetSet($offset, $value): void
133
    {
134
        throw new LogicException('Immutable object. Value for "' . $offset . '" not set.');
135
    }
136
137
    /**
138
     * @param mixed $offset
139
     * @return bool
140
     */
141 42
    public function offsetExists($offset): bool
142
    {
143 42
        return isset($this->options[$offset]);
144
    }
145
146
    /**
147
     * @param mixed $offset
148
     */
149
    public function offsetUnset($offset): void
150
    {
151
        throw new LogicException('Immutable object. Value for "' . $offset . '" not unset.');
152
    }
153
154
    /**
155
     * @param mixed $offset
156
     * @return mixed
157
     */
158 42
    public function offsetGet($offset)
159
    {
160 42
        if (! array_key_exists($offset, $this->options)) {
161
            throw new LogicException('Invalid option ' . $offset);
162
        }
163
164 42
        return $this->options[$offset];
165
    }
166
}
167