• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

prooph / event-store-client / 9555551525

17 Jun 2024 10:16PM UTC coverage: 70.262% (-1.1%) from 71.395%
9555551525

push

github

prolic
update coveralls repo token

3466 of 4933 relevant lines covered (70.26%)

67.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/src/ConnectionString.php
1
<?php
2

3
/**
4
 * This file is part of `prooph/event-store-client`.
5
 * (c) 2018-2024 Alexander Miertsch <kontakt@codeliner.ws>
6
 * (c) 2018-2024 Sascha-Oliver Prolic <saschaprolic@googlemail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace Prooph\EventStoreClient;
15

16
use Prooph\EventStore\EndPoint;
17
use Prooph\EventStore\Exception\InvalidArgumentException;
18
use Prooph\EventStore\UserCredentials;
19
use ReflectionObject;
20
use UnexpectedValueException;
21

22
class ConnectionString
23
{
24
    private static array $allowedValues = [
25
        'verboselogging' => 'bool',
26
        'maxqueuesize' => 'int',
27
        'maxconcurrentitems' => 'int',
28
        'maxretries' => 'int',
29
        'maxreconnections' => 'int',
30
        'requiremaster' => 'bool',
31
        'reconnectiondelay' => 'int',
32
        'operationtimeout' => 'int',
33
        'operationtimeoutcheckperiod' => 'int',
34
        'defaultusercredentials' => UserCredentials::class,
35
        'usesslconnection' => 'bool',
36
        'targethost' => 'string',
37
        'validateserver' => 'bool',
38
        'failonnoserverresponse' => 'bool',
39
        'heartbeatinterval' => 'int',
40
        'heartbeattimeout' => 'int',
41
        'clusterdns' => 'string',
42
        'maxdiscoverattempts' => 'int',
43
        'externalgossipport' => 'int',
44
        'gossipseeds' => GossipSeed::class,
45
        'gossiptimeout' => 'int',
46
        'preferrandomNode' => 'bool',
47
        'clientconnectiontimeout' => 'int',
48
    ];
49

50
    public static function getConnectionSettings(
51
        string $connectionString,
52
        ?ConnectionSettings $settings = null
53
    ): ConnectionSettings {
54
        $settings ??= ConnectionSettings::default();
12✔
55
        $reflection = new ReflectionObject($settings);
12✔
56
        $properties = $reflection->getProperties();
12✔
57
        $values = self::getParts($connectionString);
12✔
58

59
        foreach ($values as $value) {
12✔
60
            [$key, $value] = \explode('=', $value);
12✔
61
            $key = \strtolower($key);
12✔
62

63
            if ('connectto' === $key) {
12✔
64
                continue;
2✔
65
            }
66

67
            if (! \array_key_exists($key, self::$allowedValues)) {
12✔
68
                throw new InvalidArgumentException(\sprintf(
×
69
                    'Key %s is not an allowed key in %s',
×
70
                    $key,
×
71
                    self::class
×
72
                ));
×
73
            }
74

75
            $type = self::$allowedValues[$key];
12✔
76

77
            switch ($type) {
78
                case 'bool':
12✔
79
                    $filteredValue = \filter_var($value, \FILTER_VALIDATE_BOOLEAN);
2✔
80

81
                    break;
2✔
82
                case 'int':
10✔
83
                    $filteredValue = \filter_var($value, \FILTER_VALIDATE_INT);
6✔
84

85
                    if (false === $filteredValue) {
6✔
86
                        throw new InvalidArgumentException(\sprintf(
×
87
                            'Expected type for key %s is %s, but %s given',
×
88
                            $key,
×
89
                            $type,
×
90
                            $value
×
91
                        ));
×
92
                    }
93

94
                    break;
6✔
95
                case 'string':
6✔
96
                    $filteredValue = $value;
2✔
97

98
                    break;
2✔
99
                case UserCredentials::class:
100
                    $exploded = \explode(':', $value);
1✔
101

102
                    if (\count($exploded) !== 2) {
1✔
103
                        throw new InvalidArgumentException(\sprintf(
×
104
                            'Expected user credentials in format user:pass, %s given',
×
105
                            $value
×
106
                        ));
×
107
                    }
108

109
                    $filteredValue = new UserCredentials($exploded[0], $exploded[1]);
1✔
110

111
                    break;
1✔
112
                case GossipSeed::class:
113
                    $gossipSeeds = [];
3✔
114

115
                    foreach (\explode(',', $value) as $v) {
3✔
116
                        $exploded = \explode(':', $v);
3✔
117

118
                        if (\count($exploded) !== 2) {
3✔
119
                            throw new InvalidArgumentException(\sprintf(
×
120
                                'Expected user credentials in format user:pass, %s given',
×
121
                                $value
×
122
                            ));
×
123
                        }
124

125
                        $host = $exploded[0];
3✔
126
                        $port = \filter_var($exploded[1], \FILTER_VALIDATE_INT);
3✔
127

128
                        if (false === $port) {
3✔
129
                            throw new InvalidArgumentException(\sprintf(
×
130
                                'Expected type for port of gossip seed is int, but %s given',
×
131
                                $exploded[1]
×
132
                            ));
×
133
                        }
134

135
                        $gossipSeeds[] = new GossipSeed(new EndPoint($host, $port));
3✔
136
                    }
137

138
                    $filteredValue = $gossipSeeds;
3✔
139

140
                    break;
3✔
141
                default:
142
                    throw new UnexpectedValueException('Invalid connection config "' . $type . '" receveid');
×
143
            }
144

145
            foreach ($properties as $property) {
12✔
146
                if (\strtolower($property->getName()) === $key) {
12✔
147
                    $property->setAccessible(true);
12✔
148
                    $property->setValue($settings, $filteredValue);
12✔
149

150
                    break;
12✔
151
                }
152
            }
153
        }
154

155
        return $settings;
12✔
156
    }
157

158
    public static function getUriFromConnectionString(string $connectionString): ?Uri
159
    {
160
        $values = self::getParts($connectionString);
4✔
161

162
        foreach ($values as $value) {
4✔
163
            [$key, $value] = \explode('=', $value);
4✔
164

165
            if (\strtolower($key) === 'connectto') {
4✔
166
                return Uri::fromString($value);
2✔
167
            }
168
        }
169

170
        return null;
2✔
171
    }
172

173
    /**
174
     * @param string $connectionString
175
     *
176
     * @return string[]
177
     */
178
    private static function getParts(string $connectionString): array
179
    {
180
        return \explode(';', \str_replace(' ', '', $connectionString));
12✔
181
    }
182
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc