Skip to content

Commit 477cd80

Browse files
committed
generified Result
1 parent 598ac57 commit 477cd80

File tree

10 files changed

+108
-145
lines changed

10 files changed

+108
-145
lines changed

src/Bolt/BoltDriver.php

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

1414
namespace Laudis\Neo4j\Bolt;
1515

16-
use Bolt\connection\StreamSocket;
16+
use Bolt\Bolt;
1717
use Exception;
1818
use function is_string;
1919
use Laudis\Neo4j\Authentication\Authenticate;
@@ -40,15 +40,15 @@ final class BoltDriver implements DriverInterface
4040
{
4141
private UriInterface $parsedUrl;
4242
private AuthenticateInterface $auth;
43-
/** @var ConnectionPoolInterface<StreamSocket> */
43+
/** @var ConnectionPoolInterface<Bolt> */
4444
private ConnectionPoolInterface $pool;
4545
private DriverConfiguration $config;
4646
private FormatterInterface $formatter;
4747
private float $socketTimeout;
4848

4949
/**
50-
* @param FormatterInterface<T> $formatter
51-
* @param ConnectionPoolInterface<StreamSocket> $pool
50+
* @param FormatterInterface<T> $formatter
51+
* @param ConnectionPoolInterface<Bolt> $pool
5252
*/
5353
public function __construct(
5454
UriInterface $parsedUrl,

src/Common/TransactionHelper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public static function connectionFromSocket(
9696
$bolt = new Bolt($socket);
9797
$authenticate->authenticateBolt($bolt, $uri, $userAgent);
9898

99-
/** @var array{'name': 0, 'version': 1, 'edition': 2} $fields */
99+
/**
100+
* @var array{'name': 0, 'version': 1, 'edition': 2}
101+
* @psalm-suppress PossiblyUndefinedStringArrayOffset
102+
*/
100103
$fields = array_flip($bolt->run(<<<'CYPHER'
101104
CALL dbms.components()
102105
YIELD name, versions, edition

src/Databags/Result.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Databags;
15+
16+
use Laudis\Neo4j\Types\CypherList;
17+
18+
/**
19+
* @template T
20+
*/
21+
final class Result
22+
{
23+
private ResultSummary $summary;
24+
/** @var T */
25+
private $result;
26+
27+
/**
28+
* @param T $result
29+
* @param CypherList<string> $keys
30+
*/
31+
public function __construct($result, ResultSummary $summary)
32+
{
33+
$this->summary = $summary;
34+
$this->result = $result;
35+
}
36+
37+
/**
38+
* @return T
39+
*/
40+
public function getResult()
41+
{
42+
return $this->result;
43+
}
44+
45+
public function getSummary(): ResultSummary
46+
{
47+
return $this->summary;
48+
}
49+
}

src/Formatter/ResultFormatter.php

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,39 @@
1818
use function is_int;
1919
use Laudis\Neo4j\Contracts\ConnectionInterface;
2020
use Laudis\Neo4j\Contracts\FormatterInterface;
21+
use Laudis\Neo4j\Databags\Result;
2122
use Laudis\Neo4j\Databags\ResultSummary;
2223
use Laudis\Neo4j\Databags\ServerInfo;
2324
use Laudis\Neo4j\Databags\Statement;
2425
use Laudis\Neo4j\Databags\SummaryCounters;
2526
use Laudis\Neo4j\Enum\QueryTypeEnum;
26-
use Laudis\Neo4j\Result;
2727
use Laudis\Neo4j\Types\CypherList;
2828
use Psr\Http\Message\RequestInterface;
2929
use Psr\Http\Message\ResponseInterface;
3030
use UnexpectedValueException;
3131

3232
/**
33+
* @template T
34+
*
3335
* @psalm-import-type CypherResponseSet from \Laudis\Neo4j\Contracts\FormatterInterface
3436
* @psalm-import-type CypherResponse from \Laudis\Neo4j\Contracts\FormatterInterface
3537
* @psalm-import-type BoltCypherStats from \Laudis\Neo4j\Contracts\FormatterInterface
3638
*
37-
* @implements FormatterInterface<Result>
39+
* @implements FormatterInterface<Result<T>>
3840
*/
3941
final class ResultFormatter implements FormatterInterface
4042
{
43+
/** @var FormatterInterface<T> */
4144
private FormatterInterface $formatter;
4245

46+
/**
47+
* @param FormatterInterface<T> $formatter
48+
*/
4349
public function __construct(FormatterInterface $formatter)
4450
{
4551
$this->formatter = $formatter;
4652
}
4753

48-
/**
49-
* @param CypherResponse $response
50-
*/
5154
public function formatHttpStats(array $response, ConnectionInterface $connection, Statement $statement, float $resultAvailableAfter, float $resultConsumedAfter, CypherList $results): Result
5255
{
5356
if (!isset($response['stats'])) {
@@ -88,11 +91,7 @@ public function formatHttpStats(array $response, ConnectionInterface $connection
8891
)
8992
);
9093

91-
return new Result(
92-
new CypherList(new Vector($response['columns'])),
93-
$summary,
94-
$results
95-
);
94+
return new Result($results, $summary);
9695
}
9796

9897
/**
@@ -141,28 +140,25 @@ public function formatBoltResult(array $meta, array $results, ConnectionInterfac
141140
$response = $results[$last];
142141

143142
$counters = $this->formatBoltStats($response);
144-
$columns = $meta['fields'];
145-
146-
return new Result(
147-
new CypherList(new Vector($columns)),
148-
new ResultSummary(
149-
$counters,
150-
$connection->getDatabaseInfo(),
151-
new CypherList(new Vector()),
152-
null,
153-
null,
154-
$statement,
155-
QueryTypeEnum::fromCounters($counters),
156-
$resultAvailableAfter,
157-
$resultConsumedAfter,
158-
new ServerInfo(
159-
$connection->getServerAddress(),
160-
$connection->getProtocol(),
161-
$connection->getServerAgent()
162-
)
163-
),
164-
$this->formatter->formatBoltResult($meta, $results, $connection, $resultAvailableAfter, $resultConsumedAfter, $statement)
143+
$summary = new ResultSummary(
144+
$counters,
145+
$connection->getDatabaseInfo(),
146+
new CypherList(new Vector()),
147+
null,
148+
null,
149+
$statement,
150+
QueryTypeEnum::fromCounters($counters),
151+
$resultAvailableAfter,
152+
$resultConsumedAfter,
153+
new ServerInfo(
154+
$connection->getServerAddress(),
155+
$connection->getProtocol(),
156+
$connection->getServerAgent()
157+
)
165158
);
159+
$formattedResult = $this->formatter->formatBoltResult($meta, $results, $connection, $resultAvailableAfter, $resultConsumedAfter, $statement);
160+
161+
return new Result($formattedResult, $summary);
166162
}
167163

168164
public function formatHttpResult(ResponseInterface $response, array $body, ConnectionInterface $connection, float $resultsAvailableAfter, float $resultsConsumedAfter, iterable $statements): CypherList

src/Http/HttpUnmanagedTransaction.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Psr\Http\Client\ClientInterface;
2424
use Psr\Http\Message\RequestInterface;
2525
use Psr\Http\Message\StreamFactoryInterface;
26+
use function microtime;
2627

2728
/**
2829
* @template T
@@ -80,10 +81,12 @@ public function runStatements(iterable $statements): CypherList
8081
$body = HttpHelper::statementsToString($this->formatter, $statements);
8182

8283
$request = $request->withBody($this->factory->createStream($body));
84+
$start = microtime(true);
8385
$response = $this->connection->getImplementation()->sendRequest($request);
86+
$total = microtime(true) - $start;
8487
$data = HttpHelper::interpretResponse($response);
8588

86-
return $this->formatter->formatHttpResult($response, $data, $this->connection);
89+
return $this->formatter->formatHttpResult($response, $data, $this->connection, $total, $total, $statements);
8790
}
8891

8992
/**
@@ -96,11 +99,13 @@ public function commit(iterable $statements = []): CypherList
9699
$content = HttpHelper::statementsToString($this->formatter, $statements);
97100
$request = $request->withBody($this->factory->createStream($content));
98101

102+
$start = microtime(true);
99103
$response = $this->connection->getImplementation()->sendRequest($request);
104+
$total = microtime(true) - $start;
100105

101106
$data = HttpHelper::interpretResponse($response);
102107

103-
return $this->formatter->formatHttpResult($response, $data, $this->connection);
108+
return $this->formatter->formatHttpResult($response, $data, $this->connection, $total, $total, $statements);
104109
}
105110

106111
/**

src/Neo4j/Neo4jDriver.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313

1414
namespace Laudis\Neo4j\Neo4j;
1515

16-
use Bolt\connection\StreamSocket;
16+
use Bolt\Bolt;
1717
use Exception;
1818
use function is_string;
1919
use Laudis\Neo4j\Authentication\Authenticate;
20-
use Laudis\Neo4j\Bolt\BoltConnectionPool;
2120
use Laudis\Neo4j\Bolt\Session;
2221
use Laudis\Neo4j\Common\Uri;
2322
use Laudis\Neo4j\Contracts\AuthenticateInterface;
@@ -42,15 +41,15 @@ final class Neo4jDriver implements DriverInterface
4241
{
4342
private UriInterface $parsedUrl;
4443
private AuthenticateInterface $auth;
45-
/** @var ConnectionPoolInterface<StreamSocket> */
44+
/** @var ConnectionPoolInterface<Bolt> */
4645
private ConnectionPoolInterface $pool;
4746
private DriverConfiguration $config;
4847
private FormatterInterface $formatter;
4948
private float $socketTimeout;
5049

5150
/**
52-
* @param FormatterInterface<T> $formatter
53-
* @param ConnectionPoolInterface<StreamSocket> $pool
51+
* @param FormatterInterface<T> $formatter
52+
* @param ConnectionPoolInterface<Bolt> $pool
5453
*/
5554
public function __construct(
5655
UriInterface $parsedUrl,
@@ -93,7 +92,7 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
9392
return new self(
9493
$uri,
9594
$authenticate ?? Authenticate::fromUrl(),
96-
new Neo4jConnectionPool(new BoltConnectionPool()),
95+
new Neo4jConnectionPool(),
9796
$configuration ?? DriverConfiguration::default(),
9897
$formatter,
9998
$socketTimeout
@@ -103,7 +102,7 @@ public static function create($uri, ?DriverConfiguration $configuration = null,
103102
return new self(
104103
$uri,
105104
$authenticate ?? Authenticate::fromUrl(),
106-
new Neo4jConnectionPool(new BoltConnectionPool()),
105+
new Neo4jConnectionPool(),
107106
$configuration ?? DriverConfiguration::default(),
108107
OGMFormatter::create(),
109108
$socketTimeout

src/Result.php

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/Types/CypherList.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ final class CypherList implements CypherContainerInterface
2929
private Vector $vector;
3030

3131
/**
32-
* @param Vector<T> $vector
32+
* @param iterable<T> $vector
3333
*/
34-
public function __construct(Vector $vector)
34+
public function __construct(iterable $vector)
3535
{
36-
$this->vector = $vector;
36+
$this->vector = new Vector($vector);
3737
}
3838

3939
public function count(): int

0 commit comments

Comments
 (0)