• 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

82.95
/src/EventStoreConnectionFactory.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\EventStoreConnection;
18
use Prooph\EventStore\Exception\InvalidArgumentException;
19
use Prooph\EventStoreClient\Internal\ClusterDnsEndPointDiscoverer;
20
use Prooph\EventStoreClient\Internal\EventStoreNodeConnection;
21
use Prooph\EventStoreClient\Internal\SingleEndpointDiscoverer;
22
use Prooph\EventStoreClient\Internal\StaticEndPointDiscoverer;
23

24
class EventStoreConnectionFactory
25
{
26
    public static function createFromConnectionString(
27
        string $connectionString,
28
        ?ConnectionSettings $settings = null,
29
        ?string $connectionName = null
30
    ): EventStoreConnection {
31
        $settings = ConnectionString::getConnectionSettings(
4✔
32
            $connectionString,
4✔
33
            $settings ?? ConnectionSettings::default()
4✔
34
        );
4✔
35

36
        $uri = ConnectionString::getUriFromConnectionString($connectionString);
4✔
37

38
        if (null === $uri && empty($settings->gossipSeeds())) {
4✔
39
            throw new InvalidArgumentException(
1✔
40
                'Did not find ConnectTo or GossipSeeds in the connection string'
1✔
41
            );
1✔
42
        }
43

44
        if (null !== $uri && ! empty($settings->gossipSeeds())) {
3✔
45
            throw new InvalidArgumentException(
1✔
46
                'Setting ConnectTo as well as GossipSeeds on the connection string is currently not supported'
1✔
47
            );
1✔
48
        }
49

50
        return self::createFromUri($uri, $settings, $connectionName);
2✔
51
    }
52

53
    public static function createFromUri(
54
        ?Uri $uri,
55
        ?ConnectionSettings $connectionSettings = null,
56
        ?string $connectionName = null
57
    ): EventStoreConnection {
58
        $connectionSettings ??= ConnectionSettings::default();
11✔
59

60
        if (null !== $uri) {
11✔
61
            $scheme = \strtolower($uri->scheme());
6✔
62
            $credentials = $uri->userCredentials();
6✔
63

64
            if (null !== $credentials) {
6✔
65
                $connectionSettings = $connectionSettings->withDefaultCredentials($credentials);
3✔
66
            }
67

68
            if ('discover' === $scheme) {
6✔
69
                $clusterSettings = ClusterSettings::fromClusterDns(
2✔
70
                    $uri->host(),
2✔
71
                    $connectionSettings->maxDiscoverAttempts(),
2✔
72
                    $uri->port(),
2✔
73
                    $connectionSettings->gossipTimeout(),
2✔
74
                    $connectionSettings->preferRandomNode()
2✔
75
                );
2✔
76

77
                $endPointDiscoverer = new ClusterDnsEndPointDiscoverer(
2✔
78
                    $connectionSettings->log(),
2✔
79
                    $clusterSettings->clusterDns(),
2✔
80
                    $clusterSettings->maxDiscoverAttempts(),
2✔
81
                    $clusterSettings->externalGossipPort(),
2✔
82
                    $clusterSettings->gossipSeeds(),
2✔
83
                    $clusterSettings->gossipTimeout(),
2✔
84
                    $clusterSettings->preferRandomNode()
2✔
85
                );
2✔
86

87
                return new EventStoreNodeConnection(
2✔
88
                    $connectionSettings,
2✔
89
                    $clusterSettings,
2✔
90
                    $endPointDiscoverer,
2✔
91
                    $connectionName
2✔
92
                );
2✔
93
            }
94

95
            if ('tcp' === $scheme) {
4✔
96
                return new EventStoreNodeConnection(
3✔
97
                    $connectionSettings,
3✔
98
                    null,
3✔
99
                    new SingleEndpointDiscoverer(
3✔
100
                        $uri,
3✔
101
                        $connectionSettings->useSslConnection()
3✔
102
                    ),
3✔
103
                    $connectionName
3✔
104
                );
3✔
105
            }
106

107
            throw new InvalidArgumentException(\sprintf(
1✔
108
                'Unknown scheme for connection \'%s\'',
1✔
109
                $scheme
1✔
110
            ));
1✔
111
        }
112

113
        if (! empty($connectionSettings->gossipSeeds())) {
5✔
114
            $clusterSettings = ClusterSettings::fromGossipSeeds(
3✔
115
                $connectionSettings->gossipSeeds(),
3✔
116
                $connectionSettings->maxDiscoverAttempts(),
3✔
117
                $connectionSettings->gossipTimeout(),
3✔
118
                $connectionSettings->preferRandomNode()
3✔
119
            );
3✔
120

121
            $endPointDiscoverer = new ClusterDnsEndPointDiscoverer(
3✔
122
                $connectionSettings->log(),
3✔
123
                $clusterSettings->clusterDns(),
3✔
124
                $clusterSettings->maxDiscoverAttempts(),
3✔
125
                $clusterSettings->externalGossipPort(),
3✔
126
                $clusterSettings->gossipSeeds(),
3✔
127
                $clusterSettings->gossipTimeout(),
3✔
128
                $clusterSettings->preferRandomNode()
3✔
129
            );
3✔
130

131
            return new EventStoreNodeConnection(
3✔
132
                $connectionSettings,
3✔
133
                $clusterSettings,
3✔
134
                $endPointDiscoverer,
3✔
135
                $connectionName
3✔
136
            );
3✔
137
        }
138

139
        if ('' !== $connectionSettings->clusterDns()) {
2✔
140
            $clusterSettings = ClusterSettings::fromClusterDns(
×
141
                $connectionSettings->clusterDns(),
×
142
                $connectionSettings->maxDiscoverAttempts(),
×
143
                $connectionSettings->externalGossipPort(),
×
144
                $connectionSettings->gossipTimeout(),
×
145
                $connectionSettings->preferRandomNode()
×
146
            );
×
147

148
            $endPointDiscoverer = new ClusterDnsEndPointDiscoverer(
×
149
                $connectionSettings->log(),
×
150
                $clusterSettings->clusterDns(),
×
151
                $clusterSettings->maxDiscoverAttempts(),
×
152
                $clusterSettings->externalGossipPort(),
×
153
                $clusterSettings->gossipSeeds(),
×
154
                $clusterSettings->gossipTimeout(),
×
155
                $clusterSettings->preferRandomNode()
×
156
            );
×
157

158
            return new EventStoreNodeConnection(
×
159
                $connectionSettings,
×
160
                $clusterSettings,
×
161
                $endPointDiscoverer,
×
162
                $connectionName
×
163
            );
×
164
        }
165

166
        throw new InvalidArgumentException('Must specify uri or gossip seeds');
2✔
167
    }
168

169
    public static function createFromEndPoint(
170
        EndPoint $endPoint,
171
        ?ConnectionSettings $settings = null,
172
        ?string $connectionName = null
173
    ): EventStoreConnection {
174
        $settings ??= ConnectionSettings::default();
410✔
175

176
        return new EventStoreNodeConnection(
410✔
177
            $settings,
410✔
178
            null,
410✔
179
            new StaticEndPointDiscoverer(
410✔
180
                $endPoint,
410✔
181
                $settings->useSslConnection()
410✔
182
            ),
410✔
183
            $connectionName
410✔
184
        );
410✔
185
    }
186

187
    public static function createFromSettings(
188
        ConnectionSettings $settings,
189
        ?string $connectionName = null
190
    ): EventStoreConnection {
191
        return self::createFromUri(null, $settings, $connectionName);
2✔
192
    }
193

194
    public static function createFromClusterSettings(
195
        ConnectionSettings $connectionSettings,
196
        ClusterSettings $clusterSettings,
197
        string $connectionName = ''
198
    ): EventStoreConnection {
199
        $endPointDiscoverer = new ClusterDnsEndPointDiscoverer(
2✔
200
            $connectionSettings->log(),
2✔
201
            $clusterSettings->clusterDns(),
2✔
202
            $clusterSettings->maxDiscoverAttempts(),
2✔
203
            $clusterSettings->externalGossipPort(),
2✔
204
            $clusterSettings->gossipSeeds(),
2✔
205
            $clusterSettings->gossipTimeout(),
2✔
206
            $clusterSettings->preferRandomNode()
2✔
207
        );
2✔
208

209
        return new EventStoreNodeConnection(
2✔
210
            $connectionSettings,
2✔
211
            $clusterSettings,
2✔
212
            $endPointDiscoverer,
2✔
213
            $connectionName
2✔
214
        );
2✔
215
    }
216
}
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