Skip to content

Commit

Permalink
cleanup BaseClient, move to Utils, deprecate getAsJson
Browse files Browse the repository at this point in the history
  • Loading branch information
Sysix committed Dec 7, 2023
1 parent 8ebe058 commit adccfc3
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 76 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,6 @@ $response = $api->file()->get($entityId);
### get JSON from Success and Error Response

```php
$json = $api->*()->getAsJson($response);
// can be possible null because the response body can be empty
$json = \Sysix\LexOffice\Utils::getJsonFromResponse($response);
```
37 changes: 18 additions & 19 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,24 @@ At the moment, it doesn't look like the endpoint will be added soon. So we will

This functions will be removed in the next major (2.0) Update

- `\Sysix\Lexoffice\Clients\CreditNote::getAll`
- `\Sysix\Lexoffice\Clients\DownPaymentInvoice::getAll`
- `\Sysix\Lexoffice\Clients\Invoice::getAll`
- `\Sysix\Lexoffice\Clients\OrderConfirmation::getAll`
- `\Sysix\Lexoffice\Clients\Quotation::getAll`
- `\Sysix\Lexoffice\Clients\Voucher::getAll`
- `\Sysix\Lexoffice\Clients\VoucherList::setToEverything`
-
- `\Sysix\Lexoffice\Clients\CreditNote::getPage`
- `\Sysix\Lexoffice\Clients\DownPaymentInvoice::getPage`
- `\Sysix\Lexoffice\Clients\Invoice::getPage`
- `\Sysix\Lexoffice\Clients\OrderConfirmation::getPage`
- `\Sysix\Lexoffice\Clients\Quotation::getPage`
- `\Sysix\Lexoffice\Clients\Voucher::getPage`

For almost all clients there is a new method called `getVoucherListClient` which returns a `\Sysix\LexOffice\Api\Clients\VoucherList`.
With this client there are more filters for the vouchers.

! You need to set a non-empty `statuses` property to the `\Sysix\LexOffice\Api\Clients\VoucherList`
|Method Deprecated|Usage instead|
|---|---|
|`\Sysix\Lexoffice\Clients\CreditNote::getAll`|`\Sysix\Lexoffice\Clients\CreditNote::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\DownPaymentInvoice::getAll`|`\Sysix\Lexoffice\Clients\DownPaymentInvoice::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\Invoice::getAll`|`\Sysix\Lexoffice\Clients\Invoice::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\OrderConfirmation::getAll`|`\Sysix\Lexoffice\Clients\OrderConfirmation::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\Quotation::getAll`|`\Sysix\Lexoffice\Clients\Quotation::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\Voucher::getAll`|`\Sysix\Lexoffice\Clients\Voucher::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\VoucherList::setToEverything`|not replacement|
|`\Sysix\Lexoffice\Clients\CreditNote::getPage`|`\Sysix\Lexoffice\Clients\CreditNote::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\DownPaymentInvoice::getPage`|`\Sysix\Lexoffice\Clients\DownPaymentInvoice::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\Invoice::getPage`|`\Sysix\Lexoffice\Clients\Invoice::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\OrderConfirmation::getPage`|`\Sysix\Lexoffice\Clients\OrderConfirmation::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\Quotation::getPage`|`\Sysix\Lexoffice\Clients\Quotation::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\Voucher::getPage`|`\Sysix\Lexoffice\Clients\Voucher::getVoucherListClient`|
|`\Sysix\Lexoffice\Clients\**::getAsJson`|`\Sysix\Lexoffice\Utils::getJsonFromResponse`|

! You need to set a non-empty `statuses` property to the returned `\Sysix\LexOffice\Api\Clients\VoucherList`.


## Strict Typed
Expand Down
32 changes: 5 additions & 27 deletions src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

namespace Sysix\LexOffice;

use GuzzleHttp\Psr7\MultipartStream;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

abstract class BaseClient implements ClientInterface
{
Expand All @@ -17,35 +15,15 @@ public function __construct(
{
}

public function getAsJson(ResponseInterface $response): object
{
$body = $response->getBody()->__toString();

return Utils::jsonDecode($body);
}

protected function createStream(mixed $content): StreamInterface
{
return Utils::streamFor(
Utils::jsonEncode($content)
);
}

/**
* @param array<string, string|bool|resource> $content
* @deprecated 1.0 use Sysix\LexOffice\Utils::getJsonFromResponse()
*/
protected function createMultipartStream(array $content, string $boundary = null): MultipartStream
public function getAsJson(ResponseInterface $response): object
{
$stream = [];
$boundary = $boundary ?: '--lexoffice';
trigger_error(self::class . '::' . __METHOD__ . ' should not be called anymore, use \Sysix\LexOffice\Utils::getJsonFromResponse instead', E_USER_WARNING);

foreach ($content as $key => $value) {
$stream[] = [
'name' => $key,
'contents' => $value
];
}
$body = $response->getBody()->__toString();

return new MultipartStream($stream, $boundary);
return Utils::jsonDecode($body);
}
}
3 changes: 2 additions & 1 deletion src/Clients/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Sysix\LexOffice\Clients\Traits\GetTrait;
use Sysix\LexOffice\Exceptions\LexOfficeApiException;
use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Utils;

class File extends BaseClient
{
Expand Down Expand Up @@ -38,7 +39,7 @@ public function upload(string $filepath, string $type): ResponseInterface
throw new LexOfficeApiException('file is to big to upload: ' . $filepath . ', max upload size: ' . self::MAX_FILE_SIZE . 'bytes');
}

$body = $this->createMultipartStream([
$body = Utils::createMultipartStream([
'file' => fopen($filepath, 'r'),
'type' => $type
]);
Expand Down
3 changes: 2 additions & 1 deletion src/Clients/Traits/CreateFinalizeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sysix\LexOffice\Clients\Traits;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Utils;

trait CreateFinalizeTrait
{
Expand All @@ -13,7 +14,7 @@ public function create(array $data, bool $finalized = false): ResponseInterface
{
$api = $this->api->newRequest('POST', $this->resource . ($finalized ? '?finalize=true' : ''));

$api->request = $api->request->withBody($this->createStream($data));
$api->request = $api->request->withBody(Utils::createStream($data));

return $api->getResponse();
}
Expand Down
3 changes: 2 additions & 1 deletion src/Clients/Traits/CreateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sysix\LexOffice\Clients\Traits;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Utils;

trait CreateTrait
{
Expand All @@ -13,7 +14,7 @@ public function create(array $data): ResponseInterface
{
$api = $this->api->newRequest('POST', $this->resource);

$api->request = $api->request->withBody($this->createStream($data));
$api->request = $api->request->withBody(Utils::createStream($data));

return $api->getResponse();
}
Expand Down
10 changes: 8 additions & 2 deletions src/Clients/Traits/DocumentClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Sysix\LexOffice\Clients\File;
use Psr\Http\Message\ResponseInterface;
use stdClass;
use Sysix\LexOffice\Utils;

trait DocumentClientTrait
{
Expand All @@ -21,8 +22,13 @@ public function document(string $id, bool $asContent = false): ResponseInterface
return $response;
}

/** @var stdClass{documentField: string} $content */
$content = $this->getAsJson($response);
/** @var ?stdClass{documentField: string} $content */
$content = Utils::getJsonFromResponse($response);

if ($content === null) {
return $response;
}

$fileClient = new File($this->api);

return $fileClient->get($content->documentFileId);
Expand Down
3 changes: 2 additions & 1 deletion src/Clients/Traits/UpdateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sysix\LexOffice\Clients\Traits;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Utils;

trait UpdateTrait
{
Expand All @@ -13,7 +14,7 @@ public function update(string $id, array $data): ResponseInterface
{
$api = $this->api->newRequest('PUT', $this->resource . '/' . $id);

$api->request = $api->request->withBody($this->createStream($data));
$api->request = $api->request->withBody(Utils::createStream($data));

return $api->getResponse();
}
Expand Down
16 changes: 10 additions & 6 deletions src/PaginationClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,22 @@ public function getAll(): ResponseInterface
trigger_error(self::class . '::' . __METHOD__ . ' should not be called anymore, in future versions this method WILL not exist', E_USER_WARNING);

$response = $this->getPage(0);
/** @var stdClass{totalPages:int, content:stdClass[]} $result */
$result = $this->getAsJson($response);
/** @var ?stdClass{totalPages:int, content:stdClass[]} $result */
$result = Utils::getJsonFromResponse($response);

if ($result->totalPages == 1) {
if ($result === null || $result->totalPages == 1) {
return $response;
}

// update content to get all contacts
for ($i = 1; $i < $result->totalPages; $i++) {
$responsePage = $this->getPage($i);
/** @var stdClass{totalPages:int, content:stdClass[]} $resultPage */
$resultPage = $this->getAsJson($responsePage);
/** @var ?stdClass{totalPages:int, content:stdClass[]} $resultPage */
$resultPage = Utils::getJsonFromResponse($responsePage);

if ($resultPage === null) {
continue;
}

foreach ($resultPage->content as $entity) {
$result->content = [
Expand All @@ -62,6 +66,6 @@ public function getAll(): ResponseInterface
}
}

return $response->withBody($this->createStream($result));
return $response->withBody(Utils::createStream($result));
}
}
47 changes: 38 additions & 9 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@


use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\Psr7\MultipartStream;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

class Utils
{
public static function getJsonFromResponse(ResponseInterface $response): mixed
{
$body = $response->getBody()->__toString();

if ($response->getHeaderLine("Content-Type") === "application/json") {
return self::jsonDecode($body);
}

return null;
}

/**
* @param string $resource
* @param array{size?: int, metadata?: mixed[], mode?: bool, seekable?: bool} $options
* @return Stream
*/
public static function streamFor(string $resource = '', array $options = []): Stream
{
Expand All @@ -30,10 +42,7 @@ public static function streamFor(string $resource = '', array $options = []): St
}

/**
* @param mixed $value
* @param int $options
* @param int<1, max> $depth
* @return string
*/
public static function jsonEncode(mixed $value, int $options = 0, int $depth = 512): string
{
Expand All @@ -46,11 +55,7 @@ public static function jsonEncode(mixed $value, int $options = 0, int $depth = 5
}

/**
* @param string $json
* @param bool $assoc
* @param int<1, max> $depth
* @param int $options
* @return mixed
*/
public static function jsonDecode(string $json, bool $assoc = false, int $depth = 512, int $options = 0): mixed
{
Expand All @@ -61,4 +66,28 @@ public static function jsonDecode(string $json, bool $assoc = false, int $depth

return $data;
}


public static function createStream(mixed $content): StreamInterface
{
return Utils::streamFor(Utils::jsonEncode($content));
}

/**
* @param array<string, string|bool|resource> $content
*/
public static function createMultipartStream(array $content, string $boundary = null): MultipartStream
{
$stream = [];
$boundary = $boundary ?: '--lexoffice';

foreach ($content as $key => $value) {
$stream[] = [
'name' => $key,
'contents' => $value
];
}

return new MultipartStream($stream, $boundary);
}
}
2 changes: 1 addition & 1 deletion tests/Clients/CreditNoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function testDocumentContent(): void
[$api, $stub] = $this->createClientMultiMockObject(
CreditNote::class,
[
new Response(200, [], '{"documentFileId": "fake-id"}'),
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Clients/DownPaymentInvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testDocumentContent(): void
[$api, $stub] = $this->createClientMultiMockObject(
DownPaymentInvoice::class,
[
new Response(200, [], '{"documentFileId": "fake-id"}'),
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Clients/InvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function testDocumentContent(): void
[$api, $stub] = $this->createClientMultiMockObject(
Invoice::class,
[
new Response(200, [], '{"documentFileId": "fake-id"}'),
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Clients/OrderConfirmationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function testDocumentContent(): void
[$api, $stub] = $this->createClientMultiMockObject(
OrderConfirmation::class,
[
new Response(200, [], '{"documentFileId": "fake-id"}'),
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Clients/QuotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function testDocumentContent(): void
[$api, $stub] = $this->createClientMultiMockObject(
Quotation::class,
[
new Response(200, [], '{"documentFileId": "fake-id"}'),
new Response(200, ['Content-Type' => 'application/json'], '{"documentFileId": "fake-id"}'),
new Response()
]
);
Expand Down
11 changes: 8 additions & 3 deletions tests/PaginationClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function setProtectedProperty(object $object, string $property, $value)
$reflection_property->setValue($object, $value);
}

public function testGetAll(): void
public function testGetAllSingle(): void
{
$this->expectDeprecationV1Warning('getAll');

Expand All @@ -63,11 +63,16 @@ public function testGetAll(): void
'{"content": [], "totalPages": 1}',
$stub->getAll()->getBody()->__toString()
);
}

public function testGetAllMultiple(): void
{
$this->expectDeprecationV1Warning('getAll');

[, $stub] = $this->createPaginationClientMockObject(
[
new Response(200, [], '{"content": [{"name": "a"}], "totalPages": 2}'),
new Response(200, [], '{"content": [{"name": "b"}], "totalPages": 2}')
new Response(200, ['Content-Type' => 'application/json'], '{"content": [{"name": "a"}], "totalPages": 2}'),
new Response(200, ['Content-Type' => 'application/json'], '{"content": [{"name": "b"}], "totalPages": 2}')
]
);

Expand Down

0 comments on commit adccfc3

Please sign in to comment.