Skip to content

Commit

Permalink
implement better bulk responses (#2)
Browse files Browse the repository at this point in the history
* implement better bulk responses

* phpunit fix

* rename BasicMetaResponse to simple MetaData

* remove composer version
  • Loading branch information
ins0 authored Jul 9, 2020
1 parent 0eefbb5 commit 63ab718
Show file tree
Hide file tree
Showing 20 changed files with 523 additions and 174 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"name": "mittwald/vault-php",
"type": "library",
"license": "MIT",
"version": "0.0.1",
"homepage": "https://www.mittwald.de/",
"description": "PHP library for Vault",
"require": {
"ext-json": "*",
"guzzlehttp/psr7": ">=1.6",
"guzzlehttp/psr7": "^1.6",
"php": ">=5.6",
"php-http/httplug": ">=1.1.0"
},
"suggest": {
"php-http/curl-client": "CURL Client Adapter"
"php-http/curl-client": "CURL Client Adapter",
"tuupola/http-factory": "HTTP Factory"
},
"require-dev": {
"phpunit/phpunit": ">=5.0.0"
Expand Down
7 changes: 6 additions & 1 deletion examples/BulkOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use VaultPHP\SecretEngines\Engines\Transit\Request\EncryptData\EncryptData;
use VaultPHP\SecretEngines\Engines\Transit\Request\EncryptData\EncryptDataBulkRequest;
use VaultPHP\SecretEngines\Engines\Transit\Request\UpdateKeyConfigRequest;
use VaultPHP\SecretEngines\Engines\Transit\Response\EncryptDataResponse;
use VaultPHP\SecretEngines\Engines\Transit\Transit;
use VaultPHP\SecretEngines\Engines\Transit\EncryptionType;
use VaultPHP\VaultClient;
Expand Down Expand Up @@ -52,11 +53,15 @@
]);
$encryptBulkResponse = $transitApi->encryptDataBulk($encryptRequest);

// indicates that one or more bulk results contains errors
var_dump($encryptBulkResponse->hasErrors());

/** @var EncryptDataResponse $bulkResult */
foreach($encryptBulkResponse as $bulkResult) {
// BULK REQUEST WON'T THROW INVALID DATA EXCEPTIONS
// SO YOU ARE RESPONSABLE TO CHECK IF EVERY BULK WAS
// SUCCESSFULLY PROCESSED
if (!$bulkResult->getBasicMetaResponse()->hasErrors()) {
if (!$bulkResult->getMetaData()->hasErrors()) {
var_dump($bulkResult->getCiphertext());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/VaultPHP/Authentication/Provider/Kubernetes.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function authenticate()
false
);

if ($auth = $response->getBasicMetaResponse()->getAuth()) {
if ($auth = $response->getMetaData()->getAuth()) {
return new AuthenticationMetaData($auth);
}

Expand Down
2 changes: 1 addition & 1 deletion src/VaultPHP/Authentication/Provider/UserPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function authenticate()
false
);

if ($auth = $response->getBasicMetaResponse()->getAuth()) {
if ($auth = $response->getMetaData()->getAuth()) {
return new AuthenticationMetaData($auth);
}

Expand Down
2 changes: 1 addition & 1 deletion src/VaultPHP/Exceptions/VaultResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(ResponseInterface $response, RequestInterface $reque
$this->request = $request;

$parsedResponse = EndpointResponse::fromResponse($response);
$returnedErrors = $parsedResponse->getBasicMetaResponse()->getErrors();
$returnedErrors = $parsedResponse->getMetaData()->getErrors();
$errors = implode(', ', is_array($returnedErrors) ? $returnedErrors : []);

parent::__construct($errors, $response->getStatusCode(), $prevException);
Expand Down
134 changes: 134 additions & 0 deletions src/VaultPHP/Response/BulkEndpointResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

namespace VaultPHP\Response;

use ArrayAccess;
use Countable;
use Iterator;
use VaultPHP\Exceptions\VaultException;

/**
* Class BulkEndpointResponse
* @package VaultPHP\Response
*/
class BulkEndpointResponse extends EndpointResponse implements Iterator, ArrayAccess, Countable
{
/** @var integer */
private $iteratorPosition = 0;

/**
* @var array
*/
protected $batch_results = [];

/**
* @return bool
*/
public function hasErrors() {
$errorOccurred = $this->getMetaData()->hasErrors();

if (!$errorOccurred) {
/** @var EndpointResponse $batchResult */
foreach ($this as $batchResult) {
$errorOccurred = $batchResult->getMetaData()->hasErrors();
if ($errorOccurred) {
return true;
}
}
}

return $errorOccurred;
}

/**
* @return array
*/
public function getBatchResults() {
return $this->batch_results;
}

/**
* @return mixed
*/
public function current()
{
return $this->batch_results[$this->iteratorPosition];
}

/**
* @return void
*/
public function next()
{
++$this->iteratorPosition;
}

/**
* @return integer
*/
public function key()
{
return (int) $this->iteratorPosition;
}

/**
* @return bool
*/
public function valid()
{
return isset($this->batch_results[$this->iteratorPosition]);
}

/**
* @return void
*/
public function rewind()
{
$this->iteratorPosition = 0;
}

/**
* @return int
*/
public function count()
{
return count($this->batch_results);
}

/**
* @param integer $offset
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->batch_results[$offset]);
}

/**
* @param integer $offset
* @return mixed
*/
public function offsetGet($offset)
{
return $this->batch_results[$offset];
}

/**
* @param mixed $offset
* @param mixed $value
* @throws VaultException
*/
public function offsetSet($offset, $value)
{
throw new VaultException('readonly');
}

/**
* @param mixed $offset
* @throws VaultException
*/
public function offsetUnset($offset)
{
throw new VaultException('readonly');
}
}
63 changes: 29 additions & 34 deletions src/VaultPHP/Response/EndpointResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@
*/
class EndpointResponse implements EndpointResponseInterface
{
/** @var BasicMetaResponse */
private $basicMetaResponse;
/** @var MetaData */
protected $metaData;

/**
* EndpointResponse constructor.
* @param array|object $data
* @param array $meta
*/
public function __construct($data = [])
public function __construct($data = [], $meta = [])
{
$this->basicMetaResponse = new BasicMetaResponse();
$this->metaData = new MetaData($meta);
$this->populateData($data);
}

/**
* @param $response
* @return array
*/
private static function getResponseContent(ResponseInterface $response) {
protected static function getResponseContent(ResponseInterface $response) {
$responseBody = $response->getBody();
$responseBody->rewind();
$responseBodyContents = $responseBody->getContents();
Expand All @@ -41,54 +42,40 @@ private static function getResponseContent(ResponseInterface $response) {
* @param ResponseInterface $response
* @return static
*/
static function fromResponse(ResponseInterface $response)
public static function fromResponse(ResponseInterface $response)
{
$metaData = static::getResponseContent($response);

/** @var object|array $domainData */
$domainData = isset($metaData['data']) ? $metaData['data'] : [];
unset($metaData['data']);

$responseDTO = new static($domainData);
$responseDTO->basicMetaResponse = new BasicMetaResponse($metaData);

return $responseDTO;
return new static($domainData, $metaData);
}

/**
* @param ResponseInterface $response
* @return static[]
* @return BulkEndpointResponse
*/
static function fromBulkResponse(ResponseInterface $response)
{
$resultArray = [];
$metaData = static::getResponseContent($response);

/** @var object $domainData */
/** @var object|array $domainData */
$domainData = isset($metaData['data']) ? $metaData['data'] : [];
unset($metaData['data']);

if ($domainData && is_array($domainData->batch_results)) {
$responseDTO = new BulkEndpointResponse($domainData, $metaData);
$responseDTO->batch_results = array_map(function($batchResult) {
/** @var object $batchResult */
foreach($domainData->batch_results as $batchResult) {
/** @var array $batchMetaData */
$batchMetaData = $metaData;

if (isset($batchResult->error)) {
/** @var array $currentErrors */
$currentErrors = isset($metaData['errors']) ? $metaData['errors'] : [];
array_push($currentErrors, $batchResult->error);

$batchMetaData['errors'] = $currentErrors;
}

$responseDTO = new static($batchResult);
$responseDTO->basicMetaResponse = new BasicMetaResponse($batchMetaData);
$resultArray[] = $responseDTO;
}
}
$errors = isset($batchResult->error) && $batchResult->error ? explode(', ', (string) $batchResult->error) : [];
return new static($batchResult, [
'errors' => $errors,
]);
}, $responseDTO->batch_results);

return $resultArray;
return $responseDTO;
}

/**
Expand All @@ -107,10 +94,18 @@ private function populateData($data)
}

/**
* @return BasicMetaResponse
* @return MetaData
*/
public function getMetaData()
{
return $this->metaData;
}

/**
* @return bool
*/
public function getBasicMetaResponse()
public function hasErrors()
{
return $this->basicMetaResponse;
return (bool) $this->getMetaData()->hasErrors();
}
}
18 changes: 15 additions & 3 deletions src/VaultPHP/Response/EndpointResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,24 @@ interface EndpointResponseInterface
{
/**
* @param ResponseInterface $response
* @return mixed
* @return static
*/
static function fromResponse(ResponseInterface $response);

/**
* @return BasicMetaResponse
* @param ResponseInterface $response
* @return BulkEndpointResponse
*/
static function fromBulkResponse(ResponseInterface $response);


/**
* @return MetaData
*/
public function getMetaData();

/**
* @return bool
*/
public function getBasicMetaResponse();
public function hasErrors();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace VaultPHP\Response;

/**
* Class BasicMetaResponse
* Class MetaData
* @package VaultPHP\Response
*/
class BasicMetaResponse implements BasicMetaResponseInterface
class MetaData implements MetaDataInterface
{
/** @var string|null */
private $request_id;
Expand All @@ -33,7 +33,7 @@ class BasicMetaResponse implements BasicMetaResponseInterface
private $errors;

/**
* BasicMetaResponse constructor.
* MetaData constructor.
* @param array|object $data
*/
public function __construct($data = [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace VaultPHP\Response;

/**
* Interface BasicMetaResponseInterface
* Interface MetaDataInterface
* @package VaultPHP\Response
*/
interface BasicMetaResponseInterface
interface MetaDataInterface
{
/**
* GenericEndpointResponse constructor.
Expand Down
Loading

0 comments on commit 63ab718

Please sign in to comment.