Skip to content

Commit b57e950

Browse files
authored
Merge pull request #11 from sandrokeil/feature/improve-statement
Improve statement / cursor handling
2 parents 6578e4f + e0cdb2b commit b57e950

15 files changed

+581
-88
lines changed

CHANGELOG.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,65 @@
1-
# Change Log
1+
# Changelog
22

3-
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
3+
## TBA
4+
5+
### Added
6+
7+
* Dependencies `TypeSupport` and `CursorType` for `Statement` class
8+
* Interface `StatementHandler`
9+
* `TypeSupport` interface to `TransactionSupport` interface
10+
* Transaction methods to TransactionSupport` interface
11+
* Allow the configuration of type and guard for handler classes
12+
13+
### Deprecated
14+
15+
* Nothing
16+
17+
### Removed
18+
19+
* Dependencies `ClientInterface` and `RequestInterface` from `Statement` class
20+
21+
### Fixed
22+
23+
* Cursor id can be null in `Statement` class
24+
25+
## 0.2.0 (2020-06-17)
26+
27+
### Added
28+
29+
* Use PSR-17 (HTTP factory)
30+
* Handler classes for easier usage
31+
* Improved structure and simplified code
32+
* Folder with examples
33+
34+
### Deprecated
35+
36+
* Nothing
37+
38+
### Removed
39+
40+
* Velocypack support (to much maintenance)
41+
* PHP <=7.1 support
42+
43+
### Fixed
44+
45+
* Nothing
46+
47+
## 0.1.0 (2019-03-30)
48+
49+
### Added
50+
51+
> Don't use this release
52+
53+
* Tagged before removing VPACK support. Will maybe added later again.
54+
55+
### Deprecated
56+
57+
* Nothing
58+
59+
### Removed
60+
61+
* Nothing
62+
63+
### Fixed
64+
65+
* Nothing

example/aql-query.php

Lines changed: 1 addition & 2 deletions
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

Lines changed: 35 additions & 0 deletions
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

Lines changed: 7 additions & 7 deletions
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/Handler/Collection.php

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

1414
use ArangoDb\Exception\GuardErrorException;
1515
use ArangoDb\Exception\UnexpectedResponse;
16+
use ArangoDb\Guard\Guard;
1617
use ArangoDb\Guard\SuccessHttpStatusCode;
1718
use ArangoDb\Http\TypeSupport;
1819
use ArangoDb\Type\Collection as CollectionType;
@@ -27,20 +28,34 @@ final class Collection implements CollectionHandler
2728
private $client;
2829

2930
/**
30-
* @var SuccessHttpStatusCode
31+
* @var Guard
3132
*/
32-
private static $guard;
33+
private $guard;
3334

34-
public function __construct(TypeSupport $client)
35-
{
35+
/**
36+
* @var string
37+
*/
38+
protected $collectionClass;
39+
40+
/**
41+
* @param TypeSupport $client
42+
* @param string $collectionClass FQCN of the class which implements \ArangoDb\Type\CollectionType
43+
* @param Guard|null $guard
44+
*/
45+
public function __construct(
46+
TypeSupport $client,
47+
string $collectionClass = CollectionType::class,
48+
Guard $guard = null
49+
) {
3650
$this->client = $client;
37-
self::$guard = SuccessHttpStatusCode::withoutContentId();
51+
$this->collectionClass = $collectionClass;
52+
$this->guard = $guard ?? SuccessHttpStatusCode::withoutContentId();
3853
}
3954

4055
public function create(string $collectionName, array $options = []): string
4156
{
42-
$type = CollectionType::create($collectionName, $options)
43-
->useGuard(self::$guard);
57+
$type = ($this->collectionClass)::create($collectionName, $options)
58+
->useGuard($this->guard);
4459

4560
$response = $this->client->sendType($type);
4661

@@ -55,8 +70,8 @@ public function create(string $collectionName, array $options = []): string
5570

5671
public function has(string $collectionName): bool
5772
{
58-
$type = CollectionType::info($collectionName)
59-
->useGuard(self::$guard);
73+
$type = ($this->collectionClass)::info($collectionName)
74+
->useGuard($this->guard);
6075

6176
try {
6277
$this->client->sendType($type);
@@ -69,16 +84,16 @@ public function has(string $collectionName): bool
6984

7085
public function drop(string $collectionName): void
7186
{
72-
$type = CollectionType::delete($collectionName)
73-
->useGuard(self::$guard);
87+
$type = ($this->collectionClass)::delete($collectionName)
88+
->useGuard($this->guard);
7489

7590
$this->client->sendType($type);
7691
}
7792

7893
public function count(string $collectionName): int
7994
{
80-
$type = CollectionType::count($collectionName)
81-
->useGuard(self::$guard);
95+
$type = ($this->collectionClass)::count($collectionName)
96+
->useGuard($this->guard);
8297

8398
$response = $this->client->sendType($type);
8499

@@ -93,16 +108,16 @@ public function count(string $collectionName): int
93108

94109
public function get(string $collectionName): ResponseInterface
95110
{
96-
$type = CollectionType::info($collectionName)
97-
->useGuard(self::$guard);
111+
$type = ($this->collectionClass)::info($collectionName)
112+
->useGuard($this->guard);
98113

99114
return $this->client->sendType($type);
100115
}
101116

102117
public function truncate(string $collectionName): void
103118
{
104-
$type = CollectionType::truncate($collectionName)
105-
->useGuard(self::$guard);
119+
$type = ($this->collectionClass)::truncate($collectionName)
120+
->useGuard($this->guard);
106121

107122
$this->client->sendType($type);
108123
}

src/Handler/Document.php

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

1414
use ArangoDb\Exception\GuardErrorException;
1515
use ArangoDb\Exception\UnexpectedResponse;
16+
use ArangoDb\Guard\Guard;
1617
use ArangoDb\Guard\SuccessHttpStatusCode;
1718
use ArangoDb\Http\TypeSupport;
1819
use ArangoDb\Type\Document as DocumentType;
@@ -27,20 +28,34 @@ final class Document implements DocumentHandler
2728
private $client;
2829

2930
/**
30-
* @var SuccessHttpStatusCode
31+
* @var Guard
3132
*/
32-
private static $guard;
33+
private $guard;
3334

34-
public function __construct(TypeSupport $client)
35-
{
35+
/**
36+
* @var string
37+
*/
38+
protected $documentClass;
39+
40+
/**
41+
* @param TypeSupport $client
42+
* @param string $documentClass FQCN of the class which implements \ArangoDb\Type\DocumentType
43+
* @param Guard|null $guard
44+
*/
45+
public function __construct(
46+
TypeSupport $client,
47+
string $documentClass = DocumentType::class,
48+
Guard $guard = null
49+
) {
3650
$this->client = $client;
37-
self::$guard = SuccessHttpStatusCode::withoutContentId();
51+
$this->documentClass = $documentClass;
52+
$this->guard = $guard ?? SuccessHttpStatusCode::withoutContentId();
3853
}
3954

40-
public function save(string $collectionName, array $doc, int $flags = 0): string
55+
public function save(string $collectionName, array $docs, int $flags = 0): string
4156
{
42-
$type = DocumentType::create($collectionName, $doc, $flags)
43-
->useGuard(self::$guard);
57+
$type = ($this->documentClass)::create($collectionName, $docs, $flags)
58+
->useGuard($this->guard);
4459

4560
$response = $this->client->sendType($type);
4661

@@ -55,7 +70,7 @@ public function save(string $collectionName, array $doc, int $flags = 0): string
5570

5671
public function get(string $documentId): ResponseInterface
5772
{
58-
$type = DocumentType::read($documentId)->useGuard(self::$guard);
73+
$type = ($this->documentClass)::read($documentId)->useGuard($this->guard);
5974

6075
return $this->client->sendType($type);
6176
}
@@ -67,8 +82,8 @@ public function getById(string $collectionName, string $id): ResponseInterface
6782

6883
public function remove(string $documentId): void
6984
{
70-
$type = DocumentType::deleteOne($documentId)
71-
->useGuard(self::$guard);
85+
$type = ($this->documentClass)::deleteOne($documentId)
86+
->useGuard($this->guard);
7287

7388
$this->client->sendType($type);
7489
}
@@ -80,8 +95,8 @@ public function removeById(string $collectionName, string $id): void
8095

8196
public function has(string $documentId): bool
8297
{
83-
$type = DocumentType::readHeader($documentId)
84-
->useGuard(self::$guard);
98+
$type = ($this->documentClass)::readHeader($documentId)
99+
->useGuard($this->guard);
85100

86101
try {
87102
$this->client->sendType($type);

src/Handler/DocumentHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ public function has(string $documentId): bool;
2929

3030
public function hasById(string $collectionName, string $id): bool;
3131

32-
public function save(string $collectionName, array $doc, int $flags = 0): string;
32+
public function save(string $collectionName, array $docs, int $flags = 0): string;
3333
}

0 commit comments

Comments
 (0)