Skip to content

Commit e9d8d65

Browse files
committed
Use TypeSupport and CursorType dependency for Statement class
1 parent cf2ef1f commit e9d8d65

File tree

6 files changed

+75
-56
lines changed

6 files changed

+75
-56
lines changed

CHANGELOG.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
### Added
66

7-
* Nothing
7+
* Dependencies `TypeSupport` and `CursorType` for `Statement` class
88

99
### Deprecated
1010

1111
* Nothing
1212

1313
### Removed
1414

15-
* Nothing
15+
* Dependencies `ClientInterface` and `RequestInterface` from `Statement` class
1616

1717
### Fixed
1818

19-
* Nothing
19+
* Cursor id can be null in `Statement` class
2020

2121
## 0.2.0 (2020-06-17)
2222

example/aql-query.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@
5555
foreach ($statements as $query => $bindVars) {
5656
$statement = new Statement(
5757
$client,
58-
Cursor::create($query, $bindVars, 1000, true)->toRequest($requestFactory, $streamFactory),
59-
$requestFactory,
58+
Cursor::create($query, $bindVars, 1000, true),
6059
$streamHandlerFactory
6160
);
6261

src/Exception/NoCursorId.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* Sandro Keil (https://sandro-keil.de)
4+
*
5+
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
6+
* @copyright Copyright (c) 2018-2020 Sandro Keil
7+
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace ArangoDb\Exception;
13+
14+
use ArangoDb\Type\Type;
15+
16+
final class NoCursorId extends RuntimeException
17+
{
18+
/**
19+
* @var Type
20+
*/
21+
private $type;
22+
23+
public static function forType(Type $type): self
24+
{
25+
$self = new self('There is no cursor id to get more results.');
26+
27+
$self->type = $type;
28+
return $self;
29+
}
30+
31+
public function getType(): Type
32+
{
33+
return $this->type;
34+
}
35+
}

src/Exception/ServerException.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace ArangoDb\Exception;
1111

12+
use ArangoDb\Type\Type;
1213
use Psr\Http\Client\ClientExceptionInterface;
13-
use Psr\Http\Message\RequestInterface;
1414
use Psr\Http\Message\ResponseInterface;
1515

1616
final class ServerException extends RuntimeException implements ClientExceptionInterface
@@ -21,24 +21,24 @@ final class ServerException extends RuntimeException implements ClientExceptionI
2121
private $response;
2222

2323
/**
24-
* @var RequestInterface
24+
* @var Type
2525
*/
26-
private $request;
26+
private $type;
2727

28-
public static function with(RequestInterface $request, ResponseInterface $response): self
28+
public static function with(Type $type, ResponseInterface $response): self
2929
{
3030
$self = new self(
3131
sprintf('Response with status code "%s" was returned.', $response->getStatusCode()),
3232
$response->getStatusCode()
3333
);
34-
$self->request = $request;
34+
$self->type = $type;
3535
$self->response = $response;
3636
return $self;
3737
}
3838

39-
public function getRequest(): RequestInterface
39+
public function getType(): Type
4040
{
41-
return $this->request;
41+
return $this->type;
4242
}
4343

4444
public function getResponse(): ResponseInterface

src/Statement/Statement.php

+22-30
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,29 @@
99

1010
namespace ArangoDb\Statement;
1111

12+
use ArangoDb\Exception\NoCursorId;
1213
use ArangoDb\Exception\ServerException;
13-
use ArangoDb\Http\Url;
14+
use ArangoDb\Http\TypeSupport;
15+
use ArangoDb\Type\CursorType;
1416
use Countable;
15-
use Fig\Http\Message\RequestMethodInterface;
1617
use Fig\Http\Message\StatusCodeInterface;
1718
use Iterator;
1819
use Psr\Http\Client\ClientExceptionInterface;
19-
use Psr\Http\Client\ClientInterface;
20-
use Psr\Http\Message\RequestFactoryInterface;
21-
use Psr\Http\Message\RequestInterface;
2220

2321
/**
2422
* @implements Iterator<int, mixed>
2523
*/
2624
final class Statement implements QueryResult, Iterator, Countable
2725
{
2826
/**
29-
* @var ClientInterface
27+
* @var TypeSupport
3028
*/
3129
private $client;
3230

3331
/**
34-
* @var RequestFactoryInterface
32+
* @var CursorType
3533
*/
36-
private $requestFactory;
37-
38-
/**
39-
* @var RequestInterface
40-
*/
41-
private $request;
34+
private $cursor;
4235

4336
/**
4437
* @var StreamHandler
@@ -60,20 +53,17 @@ final class Statement implements QueryResult, Iterator, Countable
6053
/**
6154
* Query is executed on first access
6255
*
63-
* @param ClientInterface $client - connection to be used
64-
* @param RequestInterface $request Cursor request
65-
* @param RequestFactoryInterface $requestFactory
56+
* @param TypeSupport $client - connection to be used
57+
* @param CursorType $cursor
6658
* @param StreamHandlerFactoryInterface $streamHandlerFactory
6759
*/
6860
public function __construct(
69-
ClientInterface $client,
70-
RequestInterface $request,
71-
RequestFactoryInterface $requestFactory,
61+
TypeSupport $client,
62+
CursorType $cursor,
7263
StreamHandlerFactoryInterface $streamHandlerFactory
7364
) {
7465
$this->client = $client;
75-
$this->request = $request;
76-
$this->requestFactory = $requestFactory;
66+
$this->cursor = $cursor;
7767
$this->streamHandlerFactory = $streamHandlerFactory;
7868
}
7969

@@ -85,15 +75,17 @@ public function __construct(
8575
*/
8676
private function fetchOutstanding(): void
8777
{
88-
$request = $this->fetches === 0
89-
? $this->request
90-
: $this->requestFactory->createRequest(
91-
RequestMethodInterface::METHOD_PUT,
92-
Url::CURSOR . '/' . $this->streamHandler->cursorId()
93-
);
94-
95-
$request->getBody()->rewind();
96-
$response = $this->client->sendRequest($request);
78+
if ($this->fetches === 0) {
79+
$request = $this->cursor;
80+
} else {
81+
$cursorId = $this->streamHandler->cursorId();
82+
83+
if ($cursorId === null) {
84+
throw NoCursorId::forType($this->cursor);
85+
}
86+
$request = $this->cursor::nextBatch($cursorId);
87+
}
88+
$response = $this->client->sendType($request);
9789

9890
$httpStatusCode = $response->getStatusCode();
9991

test/Statement/StatementTest.php

+7-14
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public function it_returns_data_with_batch_size(): void
2828
'FOR i IN 1..' . $count . ' RETURN {"_key": i+1}',
2929
[],
3030
$count
31-
)->toRequest($this->requestFactory, $this->streamFactory),
32-
$this->requestFactory,
31+
),
3332
$this->streamHandlerFactory
3433
);
3534
$this->assertEquals(0, $statement->fetches());
@@ -58,8 +57,7 @@ public function it_returns_all_data_with_batch_size(): void
5857
'FOR i IN 0..99 RETURN {"_key": i+1}',
5958
[],
6059
10
61-
)->toRequest($this->requestFactory, $this->streamFactory),
62-
$this->requestFactory,
60+
),
6361
$this->streamHandlerFactory
6462
);
6563
$this->assertEquals(0, $statement->fetches());
@@ -88,8 +86,7 @@ public function it_supports_rewind(): void
8886
'FOR i IN 0..99 RETURN {"_key": i+1}',
8987
[],
9088
10
91-
)->toRequest($this->requestFactory, $this->streamFactory),
92-
$this->requestFactory,
89+
),
9390
$this->streamHandlerFactory
9491
);
9592

@@ -126,8 +123,7 @@ public function it_supports_single_object(): void
126123
{
127124
$statement = new Statement(
128125
$this->client,
129-
Cursor::create('RETURN {"_key": 1}')->toRequest($this->requestFactory, $this->streamFactory),
130-
$this->requestFactory,
126+
Cursor::create('RETURN {"_key": 1}'),
131127
$this->streamHandlerFactory
132128
);
133129

@@ -144,8 +140,7 @@ public function it_supports_arbitrary_data(): void
144140
{
145141
$statement = new Statement(
146142
$this->client,
147-
Cursor::create('RETURN "test"')->toRequest($this->requestFactory, $this->streamFactory),
148-
$this->requestFactory,
143+
Cursor::create('RETURN "test"'),
149144
$this->streamHandlerFactory
150145
);
151146

@@ -169,8 +164,7 @@ public function it_supports_count(): void
169164
true,
170165
null,
171166
['fullCount' => true]
172-
)->toRequest($this->requestFactory, $this->streamFactory),
173-
$this->requestFactory,
167+
),
174168
$this->streamHandlerFactory
175169
);
176170

@@ -195,8 +189,7 @@ public function it_fetches_data(): void
195189
'FOR i IN 0..99 LIMIT 50 RETURN {"_key": i+1}',
196190
[],
197191
10
198-
)->toRequest($this->requestFactory, $this->streamFactory),
199-
$this->requestFactory,
192+
),
200193
$this->streamHandlerFactory
201194
);
202195

0 commit comments

Comments
 (0)