Skip to content

Commit d436eb1

Browse files
committed
added driver factory
1 parent 7dd98ac commit d436eb1

File tree

2 files changed

+130
-47
lines changed

2 files changed

+130
-47
lines changed

src/Client.php

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@
1414
namespace Laudis\Neo4j;
1515

1616
use Ds\Map;
17-
use function in_array;
1817
use InvalidArgumentException;
1918
use function is_array;
2019
use Laudis\Neo4j\Authentication\Authenticate;
21-
use Laudis\Neo4j\Bolt\BoltDriver;
2220
use Laudis\Neo4j\Common\Uri;
2321
use Laudis\Neo4j\Contracts\AuthenticateInterface;
2422
use Laudis\Neo4j\Contracts\ClientInterface;
@@ -29,10 +27,7 @@
2927
use Laudis\Neo4j\Databags\DriverConfiguration;
3028
use Laudis\Neo4j\Databags\Statement;
3129
use Laudis\Neo4j\Databags\TransactionConfiguration;
32-
use Laudis\Neo4j\Http\HttpDriver;
33-
use Laudis\Neo4j\Neo4j\Neo4jDriver;
3430
use Laudis\Neo4j\Types\CypherList;
35-
use Psr\Http\Message\UriInterface;
3631
use function sprintf;
3732

3833
/**
@@ -135,24 +130,6 @@ public function transaction(callable $tsxHandler, ?string $alias = null, ?Transa
135130
return $this->startSession($alias)->transaction($tsxHandler, $config);
136131
}
137132

138-
/**
139-
* @param ParsedUrl $parsedUrl
140-
*
141-
* @return HttpDriver<T>
142-
*/
143-
private function makeHttpDriver(Uri $uri, AuthenticateInterface $authenticate): HttpDriver
144-
{
145-
return HttpDriver::createWithFormatter($uri, $this->formatter, $this->configuration, $authenticate);
146-
}
147-
148-
/**
149-
* @return BoltDriver<T>
150-
*/
151-
private function makeBoltDriver(Uri $uri, AuthenticateInterface $authenticate): BoltDriver
152-
{
153-
return BoltDriver::createWithFormatter($uri, $this->formatter, $this->configuration, $authenticate);
154-
}
155-
156133
/**
157134
* @template U as DriverInterface
158135
*
@@ -171,38 +148,15 @@ private function cacheDriver(string $alias, callable $factory): DriverInterface
171148
return $tbr;
172149
}
173150

174-
/**
175-
* @return Neo4jDriver<T>
176-
*/
177-
private function makeNeo4jDriver(UriInterface $uri, AuthenticateInterface $authenticate): Neo4jDriver
178-
{
179-
return Neo4jDriver::createWithFormatter($uri, $this->formatter, $this->configuration, $authenticate);
180-
}
181-
182151
/**
183152
* @param ParsedUrl $parsedUrl
184153
*
185154
* @return DriverInterface<T>
186155
*/
187156
private function makeDriver(Uri $uri, string $alias, AuthenticateInterface $authentication): DriverInterface
188157
{
189-
$scheme = $uri->getScheme();
190-
$scheme = $scheme === '' ? 'bolt' : $scheme;
191-
192-
if (in_array($scheme, ['bolt', 'bolt+s', 'bolt+ssc'])) {
193-
return $this->cacheDriver($alias, function () use ($uri, $authentication) {
194-
return $this->makeBoltDriver($uri, $authentication);
195-
});
196-
}
197-
198-
if (in_array($scheme, ['neo4j', 'neo4j+s', 'neo4j+ssc'])) {
199-
return $this->cacheDriver($alias, function () use ($uri, $authentication) {
200-
return $this->makeNeo4jDriver($uri, $authentication);
201-
});
202-
}
203-
204158
return $this->cacheDriver($alias, function () use ($uri, $authentication) {
205-
return $this->makeHttpDriver($uri, $authentication);
159+
return DriverFactory::create($uri, $this->configuration, $authentication, $this->formatter);
206160
});
207161
}
208162
}

src/DriverFactory.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Laudis Neo4j package.
7+
*
8+
* (c) Laudis technologies <http://laudis.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j;
15+
16+
use function in_array;
17+
use function is_string;
18+
use Laudis\Neo4j\Bolt\BoltDriver;
19+
use Laudis\Neo4j\Common\Uri;
20+
use Laudis\Neo4j\Contracts\AuthenticateInterface;
21+
use Laudis\Neo4j\Contracts\DriverInterface;
22+
use Laudis\Neo4j\Contracts\FormatterInterface;
23+
use Laudis\Neo4j\Databags\DriverConfiguration;
24+
use Laudis\Neo4j\Http\HttpDriver;
25+
use Laudis\Neo4j\Neo4j\Neo4jDriver;
26+
use Psr\Http\Message\UriInterface;
27+
28+
/**
29+
* @psalm-import-type OGMResults from \Laudis\Neo4j\Formatter\OGMFormatter
30+
*/
31+
final class DriverFactory
32+
{
33+
/**
34+
* @template U
35+
*
36+
* @param FormatterInterface<U> $formatter
37+
* @param string|UriInterface $uri
38+
*
39+
* @return (
40+
* func_num_args() is 4
41+
* ? DriverInterface<U>
42+
* : DriverInterface<OGMResults>
43+
* )
44+
*/
45+
public static function create($uri, ?DriverConfiguration $configuration = null, ?AuthenticateInterface $authenticate = null, FormatterInterface $formatter = null): DriverInterface
46+
{
47+
if (is_string($uri)) {
48+
$uri = Uri::create($uri);
49+
}
50+
$scheme = $uri->getScheme();
51+
$scheme = $scheme === '' ? 'bolt' : $scheme;
52+
53+
if (in_array($scheme, ['bolt', 'bolt+s', 'bolt+ssc'])) {
54+
return self::createBoltDriver($uri, $configuration, $authenticate, $formatter);
55+
}
56+
57+
if (in_array($scheme, ['neo4j', 'neo4j+s', 'neo4j+ssc'])) {
58+
return self::createNeo4jDriver($uri, $configuration, $authenticate, $formatter);
59+
}
60+
61+
return self::createHttpDriver($uri, $configuration, $authenticate, $formatter);
62+
}
63+
64+
/**
65+
* @template U
66+
*
67+
* @param FormatterInterface<U> $formatter
68+
* @param string|UriInterface $uri
69+
*
70+
* @return (
71+
* func_num_args() is 4
72+
* ? DriverInterface<U>
73+
* : DriverInterface<OGMResults>
74+
* )
75+
* @psalm-mutation-free
76+
*/
77+
private static function createBoltDriver($uri, ?DriverConfiguration $configuration, ?AuthenticateInterface $authenticate, FormatterInterface $formatter = null): DriverInterface
78+
{
79+
if ($formatter !== null) {
80+
return BoltDriver::create($uri, $configuration, $authenticate, $formatter);
81+
}
82+
83+
return BoltDriver::create($uri, $configuration, $authenticate);
84+
}
85+
86+
/**
87+
* @template U
88+
*
89+
* @param FormatterInterface<U> $formatter
90+
* @param string|UriInterface $uri
91+
*
92+
* @return (
93+
* func_num_args() is 4
94+
* ? DriverInterface<U>
95+
* : DriverInterface<OGMResults>
96+
* )
97+
* @psalm-mutation-free
98+
*/
99+
private static function createNeo4jDriver($uri, ?DriverConfiguration $configuration, ?AuthenticateInterface $authenticate, FormatterInterface $formatter = null): DriverInterface
100+
{
101+
if ($formatter !== null) {
102+
return Neo4jDriver::create($uri, $configuration, $authenticate, $formatter);
103+
}
104+
105+
return Neo4jDriver::create($uri, $configuration, $authenticate);
106+
}
107+
108+
/**
109+
* @template U
110+
*
111+
* @param FormatterInterface<U> $formatter
112+
* @param string|UriInterface $uri
113+
*
114+
* @return (
115+
* func_num_args() is 4
116+
* ? DriverInterface<U>
117+
* : DriverInterface<OGMResults>
118+
* )
119+
* @psalm-mutation-free
120+
*/
121+
private static function createHttpDriver($uri, ?DriverConfiguration $configuration, ?AuthenticateInterface $authenticate, FormatterInterface $formatter = null): DriverInterface
122+
{
123+
if ($formatter !== null) {
124+
return HttpDriver::create($uri, $configuration, $authenticate, $formatter);
125+
}
126+
127+
return HttpDriver::create($uri, $configuration, $authenticate);
128+
}
129+
}

0 commit comments

Comments
 (0)