Skip to content

Commit

Permalink
Merge pull request #11 from sandrokeil/feature/improve-statement
Browse files Browse the repository at this point in the history
Improve statement / cursor handling
  • Loading branch information
sandrokeil authored Jul 7, 2020
2 parents 6578e4f + e0cdb2b commit b57e950
Show file tree
Hide file tree
Showing 15 changed files with 581 additions and 88 deletions.
66 changes: 64 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,65 @@
# Change Log
# Changelog

\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
## TBA

### Added

* Dependencies `TypeSupport` and `CursorType` for `Statement` class
* Interface `StatementHandler`
* `TypeSupport` interface to `TransactionSupport` interface
* Transaction methods to TransactionSupport` interface
* Allow the configuration of type and guard for handler classes

### Deprecated

* Nothing

### Removed

* Dependencies `ClientInterface` and `RequestInterface` from `Statement` class

### Fixed

* Cursor id can be null in `Statement` class

## 0.2.0 (2020-06-17)

### Added

* Use PSR-17 (HTTP factory)
* Handler classes for easier usage
* Improved structure and simplified code
* Folder with examples

### Deprecated

* Nothing

### Removed

* Velocypack support (to much maintenance)
* PHP <=7.1 support

### Fixed

* Nothing

## 0.1.0 (2019-03-30)

### Added

> Don't use this release
* Tagged before removing VPACK support. Will maybe added later again.

### Deprecated

* Nothing

### Removed

* Nothing

### Fixed

* Nothing
3 changes: 1 addition & 2 deletions example/aql-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@
foreach ($statements as $query => $bindVars) {
$statement = new Statement(
$client,
Cursor::create($query, $bindVars, 1000, true)->toRequest($requestFactory, $streamFactory),
$requestFactory,
Cursor::create($query, $bindVars, 1000, true),
$streamHandlerFactory
);

Expand Down
35 changes: 35 additions & 0 deletions src/Exception/NoCursorId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Sandro Keil (https://sandro-keil.de)
*
* @link http://github.com/sandrokeil/arangodb-php-client for the canonical source repository
* @copyright Copyright (c) 2018-2020 Sandro Keil
* @license http://github.com/sandrokeil/arangodb-php-client/blob/master/LICENSE.md New BSD License
*/

declare(strict_types=1);

namespace ArangoDb\Exception;

use ArangoDb\Type\Type;

final class NoCursorId extends RuntimeException
{
/**
* @var Type
*/
private $type;

public static function forType(Type $type): self
{
$self = new self('There is no cursor id to get more results.');

$self->type = $type;
return $self;
}

public function getType(): Type
{
return $this->type;
}
}
14 changes: 7 additions & 7 deletions src/Exception/ServerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace ArangoDb\Exception;

use ArangoDb\Type\Type;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class ServerException extends RuntimeException implements ClientExceptionInterface
Expand All @@ -21,24 +21,24 @@ final class ServerException extends RuntimeException implements ClientExceptionI
private $response;

/**
* @var RequestInterface
* @var Type
*/
private $request;
private $type;

public static function with(RequestInterface $request, ResponseInterface $response): self
public static function with(Type $type, ResponseInterface $response): self
{
$self = new self(
sprintf('Response with status code "%s" was returned.', $response->getStatusCode()),
$response->getStatusCode()
);
$self->request = $request;
$self->type = $type;
$self->response = $response;
return $self;
}

public function getRequest(): RequestInterface
public function getType(): Type
{
return $this->request;
return $this->type;
}

public function getResponse(): ResponseInterface
Expand Down
49 changes: 32 additions & 17 deletions src/Handler/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ArangoDb\Exception\GuardErrorException;
use ArangoDb\Exception\UnexpectedResponse;
use ArangoDb\Guard\Guard;
use ArangoDb\Guard\SuccessHttpStatusCode;
use ArangoDb\Http\TypeSupport;
use ArangoDb\Type\Collection as CollectionType;
Expand All @@ -27,20 +28,34 @@ final class Collection implements CollectionHandler
private $client;

/**
* @var SuccessHttpStatusCode
* @var Guard
*/
private static $guard;
private $guard;

public function __construct(TypeSupport $client)
{
/**
* @var string
*/
protected $collectionClass;

/**
* @param TypeSupport $client
* @param string $collectionClass FQCN of the class which implements \ArangoDb\Type\CollectionType
* @param Guard|null $guard
*/
public function __construct(
TypeSupport $client,
string $collectionClass = CollectionType::class,
Guard $guard = null
) {
$this->client = $client;
self::$guard = SuccessHttpStatusCode::withoutContentId();
$this->collectionClass = $collectionClass;
$this->guard = $guard ?? SuccessHttpStatusCode::withoutContentId();
}

public function create(string $collectionName, array $options = []): string
{
$type = CollectionType::create($collectionName, $options)
->useGuard(self::$guard);
$type = ($this->collectionClass)::create($collectionName, $options)
->useGuard($this->guard);

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

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

public function has(string $collectionName): bool
{
$type = CollectionType::info($collectionName)
->useGuard(self::$guard);
$type = ($this->collectionClass)::info($collectionName)
->useGuard($this->guard);

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

public function drop(string $collectionName): void
{
$type = CollectionType::delete($collectionName)
->useGuard(self::$guard);
$type = ($this->collectionClass)::delete($collectionName)
->useGuard($this->guard);

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

public function count(string $collectionName): int
{
$type = CollectionType::count($collectionName)
->useGuard(self::$guard);
$type = ($this->collectionClass)::count($collectionName)
->useGuard($this->guard);

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

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

public function get(string $collectionName): ResponseInterface
{
$type = CollectionType::info($collectionName)
->useGuard(self::$guard);
$type = ($this->collectionClass)::info($collectionName)
->useGuard($this->guard);

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

public function truncate(string $collectionName): void
{
$type = CollectionType::truncate($collectionName)
->useGuard(self::$guard);
$type = ($this->collectionClass)::truncate($collectionName)
->useGuard($this->guard);

$this->client->sendType($type);
}
Expand Down
41 changes: 28 additions & 13 deletions src/Handler/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ArangoDb\Exception\GuardErrorException;
use ArangoDb\Exception\UnexpectedResponse;
use ArangoDb\Guard\Guard;
use ArangoDb\Guard\SuccessHttpStatusCode;
use ArangoDb\Http\TypeSupport;
use ArangoDb\Type\Document as DocumentType;
Expand All @@ -27,20 +28,34 @@ final class Document implements DocumentHandler
private $client;

/**
* @var SuccessHttpStatusCode
* @var Guard
*/
private static $guard;
private $guard;

public function __construct(TypeSupport $client)
{
/**
* @var string
*/
protected $documentClass;

/**
* @param TypeSupport $client
* @param string $documentClass FQCN of the class which implements \ArangoDb\Type\DocumentType
* @param Guard|null $guard
*/
public function __construct(
TypeSupport $client,
string $documentClass = DocumentType::class,
Guard $guard = null
) {
$this->client = $client;
self::$guard = SuccessHttpStatusCode::withoutContentId();
$this->documentClass = $documentClass;
$this->guard = $guard ?? SuccessHttpStatusCode::withoutContentId();
}

public function save(string $collectionName, array $doc, int $flags = 0): string
public function save(string $collectionName, array $docs, int $flags = 0): string
{
$type = DocumentType::create($collectionName, $doc, $flags)
->useGuard(self::$guard);
$type = ($this->documentClass)::create($collectionName, $docs, $flags)
->useGuard($this->guard);

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

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

public function get(string $documentId): ResponseInterface
{
$type = DocumentType::read($documentId)->useGuard(self::$guard);
$type = ($this->documentClass)::read($documentId)->useGuard($this->guard);

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

public function remove(string $documentId): void
{
$type = DocumentType::deleteOne($documentId)
->useGuard(self::$guard);
$type = ($this->documentClass)::deleteOne($documentId)
->useGuard($this->guard);

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

public function has(string $documentId): bool
{
$type = DocumentType::readHeader($documentId)
->useGuard(self::$guard);
$type = ($this->documentClass)::readHeader($documentId)
->useGuard($this->guard);

try {
$this->client->sendType($type);
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/DocumentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public function has(string $documentId): bool;

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

public function save(string $collectionName, array $doc, int $flags = 0): string;
public function save(string $collectionName, array $docs, int $flags = 0): string;
}
Loading

0 comments on commit b57e950

Please sign in to comment.