Skip to content

Commit 470e22e

Browse files
committed
updated driver configuration to allow for ssl override
1 parent b59bae0 commit 470e22e

File tree

3 files changed

+159
-15
lines changed

3 files changed

+159
-15
lines changed

src/Databags/DriverConfiguration.php

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,29 @@ final class DriverConfiguration
2828
{
2929
public const DEFAULT_USER_AGENT = 'neo4j-php-client/%s';
3030

31-
/** @var string|null */
32-
private $userAgent;
31+
private ?string $userAgent;
3332
/** @var pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null */
3433
private $httpPsrBindings;
34+
private SslConfiguration $sslConfig;
3535

3636
/**
37-
* @param string|null $userAgent
3837
* @param pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null $httpPsrBindings
3938
*/
40-
public function __construct($userAgent, $httpPsrBindings)
39+
public function __construct(?string $userAgent, $httpPsrBindings, SslConfiguration $sslConfig)
4140
{
4241
$this->userAgent = $userAgent;
4342
$this->httpPsrBindings = $httpPsrBindings;
43+
$this->sslConfig = $sslConfig;
4444
}
4545

4646
/**
47-
* @pure
48-
*
49-
* @param string|null $userAgent
5047
* @param pure-callable():(HttpPsrBindings|null)|HttpPsrBindings|null $httpPsrBindings
48+
*
49+
* @pure
5150
*/
52-
public static function create($userAgent, $httpPsrBindings): self
51+
public static function create(?string $userAgent, $httpPsrBindings, SslConfiguration $sslConfig): self
5352
{
54-
return new self($userAgent, $httpPsrBindings);
53+
return new self($userAgent, $httpPsrBindings, $sslConfig);
5554
}
5655

5756
/**
@@ -62,10 +61,7 @@ public static function create($userAgent, $httpPsrBindings): self
6261
*/
6362
public static function default(): self
6463
{
65-
return new self(
66-
self::DEFAULT_USER_AGENT,
67-
HttpPsrBindings::default()
68-
);
64+
return new self(self::DEFAULT_USER_AGENT, HttpPsrBindings::default(), SslConfiguration::default());
6965
}
7066

7167
public function getUserAgent(): string
@@ -91,7 +87,10 @@ public function getUserAgent(): string
9187
*/
9288
public function withUserAgent($userAgent): self
9389
{
94-
return new self($userAgent, $this->httpPsrBindings);
90+
$tbr = clone $this;
91+
$tbr->userAgent = $userAgent;
92+
93+
return $tbr;
9594
}
9695

9796
/**
@@ -101,7 +100,23 @@ public function withUserAgent($userAgent): self
101100
*/
102101
public function withHttpPsrBindings($bindings): self
103102
{
104-
return new self($this->userAgent, $bindings);
103+
$tbr = clone $this;
104+
$tbr->httpPsrBindings = $bindings;
105+
106+
return $tbr;
107+
}
108+
109+
public function withSslConfiguration(SslConfiguration $config): self
110+
{
111+
$tbr = clone $this;
112+
$tbr->sslConfig = $config;
113+
114+
return $tbr;
115+
}
116+
117+
public function getSslConfiguration(): SslConfiguration
118+
{
119+
return $this->sslConfig;
105120
}
106121

107122
public function getHttpPsrBindings(): HttpPsrBindings

src/Databags/SslConfiguration.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laudis Neo4j package.
5+
*
6+
* (c) Laudis technologies <http://laudis.tech>
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+
namespace Laudis\Neo4j\Databags;
13+
14+
use Laudis\Neo4j\Enum\SslMode;
15+
16+
/**
17+
* @psalm-immutable
18+
*/
19+
final class SslConfiguration
20+
{
21+
private SslMode $mode;
22+
private bool $verifyPeer;
23+
24+
public function __construct(SslMode $mode, bool $verifyPeer)
25+
{
26+
$this->mode = $mode;
27+
$this->verifyPeer = $verifyPeer;
28+
}
29+
30+
public function getMode(): SslMode
31+
{
32+
return $this->mode;
33+
}
34+
35+
public function isVerifyPeer(): bool
36+
{
37+
return $this->verifyPeer;
38+
}
39+
40+
/**
41+
* @pure
42+
*/
43+
public static function create(SslMode $mode, bool $verifyPeer): self
44+
{
45+
return new self($mode, $verifyPeer);
46+
}
47+
48+
/**
49+
* @pure
50+
*/
51+
public static function default(): self
52+
{
53+
/** @psalm-suppress ImpureMethodCall */
54+
return self::create(SslMode::FROM_URL(), true);
55+
}
56+
57+
public function withMode(SslMode $mode): self
58+
{
59+
$tbr = clone $this;
60+
$tbr->mode = $mode;
61+
62+
return $tbr;
63+
}
64+
65+
public function withVerifyPeer(bool $verify): self
66+
{
67+
$tbr = clone $this;
68+
$tbr->verifyPeer = $verify;
69+
70+
return $tbr;
71+
}
72+
}

src/Enum/SslMode.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Laudis Neo4j package.
5+
*
6+
* (c) Laudis technologies <http://laudis.tech>
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+
namespace Laudis\Neo4j\Enum;
13+
14+
use const E_DEPRECATED;
15+
use function error_reporting;
16+
use JsonSerializable;
17+
use Laudis\TypedEnum\TypedEnum;
18+
19+
/**
20+
* Turn of error reporting for class definition. PHP Users of 8.1 receive a deprectation warning otherwise but
21+
* it is not fixable from the minimum version 7.4 as it required the "mixed" keyword.
22+
*/
23+
$oldReporting = error_reporting(error_reporting() & ~E_DEPRECATED);
24+
25+
/**
26+
* @method static self ENABLE()
27+
* @method static self DISABLE()
28+
* @method static self FROM_URL()
29+
*
30+
* @extends TypedEnum<string>
31+
*
32+
* @psalm-immutable
33+
*
34+
* @psalm-suppress MutableDependency
35+
*/
36+
final class SslMode extends TypedEnum implements JsonSerializable
37+
{
38+
private const ENABLE = 'enable';
39+
private const DISABLE = 'disable';
40+
private const FROM_URL = 'from_url';
41+
42+
public function __toString()
43+
{
44+
/** @noinspection MagicMethodsValidityInspection */
45+
return $this->getValue();
46+
}
47+
48+
public function jsonSerialize()
49+
{
50+
return $this->getValue();
51+
}
52+
}
53+
54+
/**
55+
* Turn back on old error reporting after class definition.
56+
*/
57+
error_reporting($oldReporting);

0 commit comments

Comments
 (0)