Completed
Push — master ( 6a4a32...e9c85c )
by Nicolas
02:18
created

ClientConfiguration::getAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Elastica;
4
5
use Elastica\Exception\InvalidException;
6
7
/**
8
 * Elastica client configuration.
9
 *
10
 * @author Antoine Lamirault <[email protected]>
11
 */
12
class ClientConfiguration
13
{
14
    /**
15
     * Config with defaults.
16
     *
17
     * log: Set to true, to enable logging, set a string to log to a specific file
18
     * retryOnConflict: Use in \Elastica\Client::updateDocument
19
     * bigintConversion: Set to true to enable the JSON bigint to string conversion option (see issue #717)
20
     *
21
     * @var array
22
     */
23
    protected $configuration = [
24
        'host' => null,
25
        'port' => null,
26
        'path' => null,
27
        'url' => null,
28
        'proxy' => null,
29
        'transport' => null,
30
        'persistent' => true,
31
        'timeout' => null,
32
        'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, username, password, config -> (curl, headers, url)
33
        'roundRobin' => false,
34
        'log' => false,
35
        'retryOnConflict' => 0,
36
        'bigintConversion' => false,
37
        'username' => null,
38
        'password' => null,
39
    ];
40
41
    /**
42
     * Create configuration.
43
     *
44
     * @param array $config Additional config
45
     *
46
     * @return ClientConfiguration
47
     */
48
    public static function fromArray(array $config): self
49
    {
50
        $clientConfiguration = new self();
51
        foreach ($config as $key => $value) {
52
            $clientConfiguration->set($key, $value);
53
        }
54
55
        return $clientConfiguration;
56
    }
57
58
    /**
59
     * Create configuration from Dsn string.
60
     *
61
     * @param string $dsn
62
     *
63
     * @return ClientConfiguration
64
     */
65
    public static function fromDsn(string $dsn): self
66
    {
67
        if (false === $parsedDsn = \parse_url($dsn)) {
68
            throw new InvalidException(\sprintf("DSN '%s' is invalid.", $dsn));
69
        }
70
71
        $clientConfiguration = new self();
72
73
        if (isset($parsedDsn['scheme'])) {
74
            $clientConfiguration->set('transport', $parsedDsn['scheme']);
75
        }
76
77
        if (isset($parsedDsn['host'])) {
78
            $clientConfiguration->set('host', $parsedDsn['host']);
79
        }
80
81
        if (isset($parsedDsn['user'])) {
82
            $clientConfiguration->set('username', \urldecode($parsedDsn['user']));
83
        }
84
85
        if (isset($parsedDsn['pass'])) {
86
            $clientConfiguration->set('password', \urldecode($parsedDsn['pass']));
87
        }
88
89
        if (isset($parsedDsn['port'])) {
90
            $clientConfiguration->set('port', $parsedDsn['port']);
91
        }
92
93
        if (isset($parsedDsn['path'])) {
94
            $clientConfiguration->set('path', $parsedDsn['path']);
95
        }
96
97
        $options = [];
98
        if (isset($parsedDsn['query'])) {
99
            \parse_str($parsedDsn['query'], $options);
100
        }
101
102
        foreach ($options as $optionName => $optionValue) {
0 ignored issues
show
Bug introduced by
The expression $options of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
103
            if ('false' === $optionValue) {
104
                $optionValue = false;
105
            } elseif ('true' === $optionValue) {
106
                $optionValue = true;
107
            } elseif (\is_numeric($optionValue)) {
108
                $optionValue = (int) $optionValue;
109
            }
110
111
            $clientConfiguration->set($optionName, $optionValue);
112
        }
113
114
        return $clientConfiguration;
115
    }
116
117
    /**
118
     * Returns a specific config key or the whole
119
     * config array if not set.
120
     *
121
     * @param string $key Config key
122
     *
123
     * @throws \Elastica\Exception\InvalidException
124
     *
125
     * @return mixed Config value
126
     */
127
    public function get(string $key)
128
    {
129
        if (empty($key)) {
130
            return $this->configuration;
131
        }
132
133
        if (!$this->has($key)) {
134
            throw new InvalidException('Config key is not set: '.$key);
135
        }
136
137
        return $this->configuration[$key];
138
    }
139
140
    /**
141
     * Returns boolean indicates if configuration has key.
142
     *
143
     * @param string $key Key to check
144
     *
145
     * @return bool
146
     */
147
    public function has(string $key): bool
148
    {
149
        return \array_key_exists($key, $this->configuration);
150
    }
151
152
    /**
153
     * Return all configuration.
154
     *
155
     * @return array
156
     */
157
    public function getAll(): array
158
    {
159
        return $this->configuration;
160
    }
161
162
    /**
163
     * @param string $key   Key to set
164
     * @param mixed  $value Value
165
     */
166
    public function set(string $key, $value): void
167
    {
168
        $this->configuration[$key] = $value;
169
    }
170
171
    /**
172
     * Add value to a key. If original value is not an array, value is wrapped.
173
     *
174
     * @param string $key   Key to add
175
     * @param mixed  $value Value
176
     */
177
    public function add(string $key, $value): void
178
    {
179
        if (!\array_key_exists($key, $this->configuration)) {
180
            $this->configuration[$key] = [$value];
181
        } else {
182
            if (\is_array($this->configuration[$key])) {
183
                $this->configuration[$key][] = $value;
184
            } else {
185
                $this->configuration[$key] = [$this->configuration[$key], $value];
186
            }
187
        }
188
    }
189
}
190