Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 19e5086

Browse files
authored
Merge pull request #95 from Nyholm/patch-config
Avoid using ConfigInterface since it does not provide any type safety
2 parents bc92bf5 + 1588d24 commit 19e5086

File tree

8 files changed

+59
-77
lines changed

8 files changed

+59
-77
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"php": "^5.6 || ^7.0",
2525
"ext-bcmath": "*",
2626
"ext-mbstring": "*",
27+
"graphaware/neo4j-common": "^3.4",
2728
"graphaware/neo4j-bolt": "^1.5",
2829
"symfony/event-dispatcher": "^2.7 || ^3.0",
2930
"myclabs/php-enum": "^1.4",
@@ -49,5 +50,6 @@
4950
"psr-4": {
5051
"GraphAware\\Neo4j\\Client\\Tests\\": "tests/"
5152
}
52-
}
53+
},
54+
"minimum-stability": "dev"
5355
}

src/ClientBuilder.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace GraphAware\Neo4j\Client;
1313

14+
use GraphAware\Common\Connection\BaseConfiguration;
1415
use GraphAware\Common\Driver\ConfigInterface;
1516
use GraphAware\Neo4j\Client\Connection\ConnectionManager;
1617
use GraphAware\Neo4j\Client\HttpDriver\Configuration;
@@ -57,9 +58,9 @@ public static function create($config = [])
5758
/**
5859
* Add a connection to the handled connections.
5960
*
60-
* @param string $alias
61-
* @param string $uri
62-
* @param ConfigInterface $config
61+
* @param string $alias
62+
* @param string $uri
63+
* @param BaseConfiguration $config
6364
*
6465
* @return ClientBuilder
6566
*/

src/Connection/Connection.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111

1212
namespace GraphAware\Neo4j\Client\Connection;
1313

14-
use GraphAware\Bolt\Configuration;
14+
use GraphAware\Bolt\Configuration as BoltConfiguration;
1515
use GraphAware\Bolt\Driver as BoltDriver;
1616
use GraphAware\Bolt\Exception\MessageFailureException;
1717
use GraphAware\Bolt\GraphDatabase as BoltGraphDB;
18+
use GraphAware\Common\Connection\BaseConfiguration;
1819
use GraphAware\Common\Cypher\Statement;
1920
use GraphAware\Neo4j\Client\Exception\Neo4jException;
2021
use GraphAware\Neo4j\Client\HttpDriver\GraphDatabase as HttpGraphDB;
@@ -50,9 +51,9 @@ class Connection
5051
/**
5152
* Connection constructor.
5253
*
53-
* @param string $alias
54-
* @param string $uri
55-
* @param Configuration|null $config
54+
* @param string $alias
55+
* @param string $uri
56+
* @param BaseConfiguration|null $config
5657
*/
5758
public function __construct($alias, $uri, $config = null)
5859
{
@@ -170,7 +171,7 @@ private function buildDriver()
170171
$uri = sprintf('%s://%s:%d', $params['scheme'], $params['host'], $port);
171172
$config = null;
172173
if (isset($params['user']) && isset($params['pass'])) {
173-
$config = Configuration::newInstance()->withCredentials($params['user'], $params['pass']);
174+
$config = BoltConfiguration::create()->withCredentials($params['user'], $params['pass']);
174175
}
175176
$this->driver = BoltGraphDB::driver($uri, $config);
176177
} elseif (preg_match('/http/', $this->uri)) {

src/Connection/ConnectionManager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace GraphAware\Neo4j\Client\Connection;
1313

14-
use GraphAware\Neo4j\Client\HttpDriver\Configuration;
14+
use GraphAware\Common\Connection\BaseConfiguration;
1515

1616
class ConnectionManager
1717
{
@@ -26,9 +26,9 @@ class ConnectionManager
2626
private $master;
2727

2828
/**
29-
* @param string $alias
30-
* @param string $uri
31-
* @param null|Configuration $config
29+
* @param string $alias
30+
* @param string $uri
31+
* @param BaseConfiguration|null $config
3232
*/
3333
public function registerConnection($alias, $uri, $config = null)
3434
{

src/HttpDriver/Configuration.php

Lines changed: 18 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
use Http\Discovery\HttpClientDiscovery;
1717
use Http\Discovery\MessageFactoryDiscovery;
1818
use Http\Message\RequestFactory;
19+
use GraphAware\Common\Connection\BaseConfiguration;
1920

20-
class Configuration implements ConfigInterface
21+
/**
22+
* @author Tobias Nyholm <[email protected]>
23+
*/
24+
class Configuration extends BaseConfiguration implements ConfigInterface
2125
{
2226
/**
2327
* @var int
@@ -31,34 +35,15 @@ class Configuration implements ConfigInterface
3135
*/
3236
protected $curlInterface;
3337

34-
/**
35-
* @var HttpClient
36-
*/
37-
private $httpClient;
38-
39-
/**
40-
* @var RequestFactory
41-
*/
42-
private $requestFactory;
43-
4438
/**
4539
* @return Configuration
4640
*/
4741
public static function create(HttpClient $httpClient = null, RequestFactory $requestFactory = null)
4842
{
49-
$config = new self();
50-
$config->httpClient = $httpClient ?: HttpClientDiscovery::find();
51-
$config->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
52-
53-
return $config;
54-
}
55-
56-
/**
57-
* @return HttpClient
58-
*/
59-
public function getHttpClient()
60-
{
61-
return $this->httpClient;
43+
return new self([
44+
'http_client' => $httpClient ?: HttpClientDiscovery::find(),
45+
'request_factory' => $requestFactory ?: MessageFactoryDiscovery::find(),
46+
]);
6247
}
6348

6449
/**
@@ -68,18 +53,7 @@ public function getHttpClient()
6853
*/
6954
public function setHttpClient(HttpClient $httpClient)
7055
{
71-
$new = clone $this;
72-
$new->httpClient = $httpClient;
73-
74-
return $new;
75-
}
76-
77-
/**
78-
* @return RequestFactory
79-
*/
80-
public function getRequestFactory()
81-
{
82-
return $this->requestFactory;
56+
return $this->setValue('http_client', $httpClient);
8357
}
8458

8559
/**
@@ -89,36 +63,29 @@ public function getRequestFactory()
8963
*/
9064
public function setRequestFactory(RequestFactory $requestFactory)
9165
{
92-
$new = clone $this;
93-
$new->requestFactory = $requestFactory;
94-
95-
return $new;
66+
return $this->setValue('request_factory', $requestFactory);
9667
}
9768

9869
/**
9970
* @param int $timeout
10071
*
10172
* @return Configuration
102-
* @deprecated Will be removed in 5.0
73+
* @deprecated Will be removed in 5.0. The Timeout option will disappear.
10374
*/
10475
public function withTimeout($timeout)
10576
{
106-
$this->timeout = $timeout;
107-
108-
return $this;
77+
return $this->setValue('timeout', $timeout);
10978
}
11079

11180
/**
11281
* @param string $interface
11382
*
11483
* @return $this
115-
* @deprecated Will be removed in 5.0
84+
* @deprecated Will be removed in 5.0. The CurlInterface option will disappear.
11685
*/
11786
public function withCurlInterface($interface)
11887
{
119-
$this->curlInterface = $interface;
120-
121-
return $this;
88+
return $this->setValue('curl_interface', $interface);
12289
}
12390

12491
/**
@@ -127,15 +94,15 @@ public function withCurlInterface($interface)
12794
*/
12895
public function getTimeout()
12996
{
130-
return $this->timeout;
97+
return $this->getValue('timeout');
13198
}
13299

133100
/**
134101
* @return string
135-
* @deprecated Will be removed in 5.0
102+
* @deprecated Will be removed in 5.0.
136103
*/
137104
public function getCurlInterface()
138105
{
139-
return $this->curlInterface;
106+
return $this->getValue('curl_interface');
140107
}
141108
}

src/HttpDriver/Driver.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace GraphAware\Neo4j\Client\HttpDriver;
1313

14+
use GraphAware\Common\Connection\BaseConfiguration;
1415
use GraphAware\Common\Driver\ConfigInterface;
1516
use GraphAware\Common\Driver\DriverInterface;
1617
use Http\Adapter\Guzzle6\Client;
@@ -30,11 +31,15 @@ class Driver implements DriverInterface
3031
protected $config;
3132

3233
/**
33-
* @param string $uri
34-
* @param Configuration $config
34+
* @param string $uri
35+
* @param BaseConfiguration $config
3536
*/
3637
public function __construct($uri, ConfigInterface $config = null)
3738
{
39+
if (null !== $config && !$config instanceof BaseConfiguration) {
40+
throw new \RuntimeException(sprintf('Second argument to "%s" must be null or "%s"', __CLASS__, BaseConfiguration::class));
41+
}
42+
3843
$this->uri = $uri;
3944
$this->config = null !== $config ? $config : Configuration::create();
4045
}
@@ -62,16 +67,16 @@ public function getUri()
6267
private function getHttpClient()
6368
{
6469
$options = [];
65-
if (null !== $this->config->getTimeout()) {
66-
$options['timeout'] = $this->config->getTimeout();
70+
if ($this->config->hasValue('timeout')) {
71+
$options['timeout'] = $this->config->getValue('timeout');
6772
}
6873

69-
if (null !== $this->config->getCurlInterface()) {
70-
$options['curl'][10062] = $this->config->getCurlInterface();
74+
if ($this->config->hasValue('curl_interface')) {
75+
$options['curl'][10062] = $this->config->getValue('curl_interface');
7176
}
7277

7378
if (empty($options)) {
74-
return $this->config->getHttpClient();
79+
return $this->config->getValue('http_client');
7580
}
7681

7782
// This is to keep BC. Will be removed in 5.0

src/HttpDriver/GraphDatabase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
namespace GraphAware\Neo4j\Client\HttpDriver;
1313

14+
use GraphAware\Common\Connection\BaseConfiguration;
1415
use GraphAware\Common\Driver\ConfigInterface;
1516
use GraphAware\Common\GraphDatabaseInterface;
1617

1718
class GraphDatabase implements GraphDatabaseInterface
1819
{
1920
/**
20-
* @param string $uri
21-
* @param ConfigInterface|null $config
21+
* @param string $uri
22+
* @param BaseConfiguration|null $config
2223
*
2324
* @return Driver
2425
*/

src/HttpDriver/Session.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace GraphAware\Neo4j\Client\HttpDriver;
1313

14+
use GraphAware\Common\Connection\BaseConfiguration;
1415
use GraphAware\Common\Driver\ConfigInterface;
1516
use GraphAware\Common\Driver\SessionInterface;
1617
use GraphAware\Common\Transaction\TransactionInterface;
@@ -60,9 +61,9 @@ class Session implements SessionInterface
6061
private $requestFactory;
6162

6263
/**
63-
* @param string $uri
64-
* @param GuzzleClient|HttpClient $httpClient
65-
* @param Configuration|ConfigInterface $config
64+
* @param string $uri
65+
* @param GuzzleClient|HttpClient $httpClient
66+
* @param BaseConfiguration $config
6667
*/
6768
public function __construct($uri, $httpClient, ConfigInterface $config)
6869
{
@@ -73,11 +74,15 @@ public function __construct($uri, $httpClient, ConfigInterface $config)
7374
throw new \RuntimeException('Second argument to Session::__construct must be an instance of Http\Client\HttpClient.');
7475
}
7576

77+
if (null !== $config && !$config instanceof BaseConfiguration) {
78+
throw new \RuntimeException(sprintf('Third argument to "%s" must be null or "%s"', __CLASS__, BaseConfiguration::class));
79+
}
80+
7681
$this->uri = $uri;
7782
$this->httpClient = new PluginClient($httpClient, [new ErrorPlugin()]);
7883
$this->responseFormatter = new ResponseFormatter();
7984
$this->config = $config;
80-
$this->requestFactory = $config->getRequestFactory();
85+
$this->requestFactory = $config->getValue('request_factory');
8186
}
8287

8388
/**

0 commit comments

Comments
 (0)