Skip to content

Commit

Permalink
Merge pull request #135 from Sysix/interfaces
Browse files Browse the repository at this point in the history
added ApiInterface, make request protected and use getRequest() instead
  • Loading branch information
Sysix authored Dec 21, 2023
2 parents 19ced0c + 6158c94 commit f4724c1
Show file tree
Hide file tree
Showing 39 changed files with 206 additions and 175 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => true,
'native_type_declaration_casing' => true,
'ordered_imports' => ['imports_order' => ['class', 'function', 'const'], 'sort_algorithm' => 'alpha'],
'no_alias_functions' => true,
'no_alias_language_construct_call' => true,
'no_alternative_syntax' => true,
Expand Down
29 changes: 16 additions & 13 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@

namespace Sysix\LexOffice;

use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use SensitiveParameter;
use Sysix\LexOffice\Clients\Contact;
use Sysix\LexOffice\Clients\Country;
use Sysix\LexOffice\Clients\CreditNote;
Expand All @@ -21,30 +27,22 @@
use Sysix\LexOffice\Clients\RecurringTemplate;
use Sysix\LexOffice\Clients\Voucher;
use Sysix\LexOffice\Clients\VoucherList;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use SensitiveParameter;
use Sysix\LexOffice\Interfaces\ApiInterface;

class Api
class Api implements ApiInterface
{
public string $apiUrl = 'https://api.lexoffice.io';

protected string $apiVersion = 'v1';

public RequestInterface $request;
protected RequestInterface $request;

public function __construct(
#[SensitiveParameter] protected string $apiKey,
protected ClientInterface $client
) {
}

/**
* @param string[] $headers
*/
public function newRequest(string $method, string $resource, array $headers = []): self
{
$this->setRequest(
Expand All @@ -71,16 +69,21 @@ public function setRequest(RequestInterface $request): self
return $this;
}

protected function createApiUri(string $resource): UriInterface
public function getRequest(): RequestInterface
{
return new Uri($this->apiUrl . '/' . $this->apiVersion . '/' . $resource);
return $this->request;
}

public function getResponse(): ResponseInterface
{
return $this->client->sendRequest($this->request);
}

protected function createApiUri(string $resource): UriInterface
{
return new Uri($this->apiUrl . '/' . $this->apiVersion . '/' . $resource);
}

public function contact(): Contact
{
return new Contact($this);
Expand Down
4 changes: 3 additions & 1 deletion src/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace Sysix\LexOffice;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Interfaces\ApiInterface;
use Sysix\LexOffice\Interfaces\ClientInterface;

abstract class BaseClient implements ClientInterface
{
protected string $resource;

public function __construct(
protected Api $api
protected ApiInterface $api
) {
}

Expand Down
10 changes: 0 additions & 10 deletions src/ClientInterface.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Clients/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Sysix\LexOffice\Clients;

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

class Country extends BaseClient
{
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Sysix\LexOffice\Clients;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\BaseClient;
use Sysix\LexOffice\Clients\Traits\CreateTrait;
use Sysix\LexOffice\Clients\Traits\DeleteTrait;
use Psr\Http\Message\ResponseInterface;

class Event extends BaseClient
{
Expand Down
8 changes: 4 additions & 4 deletions src/Clients/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Sysix\LexOffice\Clients;

use Sysix\LexOffice\BaseClient;
use Sysix\LexOffice\Exceptions\LexOfficeApiException;
use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\BaseClient;
use Sysix\LexOffice\Config\FileClientConfig;
use Sysix\LexOffice\Exceptions\LexOfficeApiException;
use Sysix\LexOffice\Utils;

class File extends BaseClient
Expand All @@ -18,7 +18,7 @@ public function get(string $id, string $acceptHeader = '*/*'): ResponseInterface
{
$this->api->newRequest('GET', $this->resource . '/' . rawurlencode($id));

$this->api->request = $this->api->request->withHeader('Accept', $acceptHeader);
$this->api->setRequest($this->api->getRequest()->withHeader('Accept', $acceptHeader));

return $this->api->getResponse();
}
Expand All @@ -44,7 +44,7 @@ public function upload(string $filepath, string $type): ResponseInterface
'Content-Type' => 'multipart/form-data; boundary=' . $body->getBoundary()
]);

$api->request = $api->request->withBody($body);
$api->setRequest($api->getRequest()->withBody($body));

return $api->getResponse();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/PaymentCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Sysix\LexOffice\Clients;

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

class PaymentCondition extends BaseClient
{
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/PostingCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Sysix\LexOffice\Clients;

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

class PostingCategory extends BaseClient
{
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Sysix\LexOffice\Clients;

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

class Profile extends BaseClient
{
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Traits/CreateFinalizeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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(Utils::createStream($data));
$api->setRequest($api->getRequest()->withBody(Utils::createStream($data)));

return $api->getResponse();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Traits/CreateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function create(array $data): ResponseInterface
{
$api = $this->api->newRequest('POST', $this->resource);

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

return $api->getResponse();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Traits/DocumentClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Sysix\LexOffice\Clients\Traits;

use Sysix\LexOffice\Clients\File;
use Psr\Http\Message\ResponseInterface;
use stdClass;
use Sysix\LexOffice\Clients\File;
use Sysix\LexOffice\Utils;

trait DocumentClientTrait
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Traits/UpdateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function update(string $id, array $data): ResponseInterface
{
$api = $this->api->newRequest('PUT', $this->resource . '/' . rawurlencode($id));

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

return $api->getResponse();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/Traits/VoucherListTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Sysix\LexOffice\Clients\Traits;

use Sysix\LexOffice\Clients\VoucherList;
use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\VoucherList;

trait VoucherListTrait
{
Expand Down
6 changes: 3 additions & 3 deletions src/Clients/Voucher.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Sysix\LexOffice\Clients;

use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\BaseClient;
use Sysix\LexOffice\Config\FileClientConfig;
use Sysix\LexOffice\Clients\Traits\CreateTrait;
use Sysix\LexOffice\Clients\Traits\GetTrait;
use Sysix\LexOffice\Clients\Traits\UpdateTrait;
use Sysix\LexOffice\Config\FileClientConfig;
use Sysix\LexOffice\Exceptions\LexOfficeApiException;
use Sysix\LexOffice\Utils;
use Psr\Http\Message\ResponseInterface;

class Voucher extends BaseClient
{
Expand All @@ -37,7 +37,7 @@ public function upload(string $id, string $filepath): ResponseInterface
'Content-Type' => 'multipart/form-data; boundary=' . $body->getBoundary()
]);

$api->request = $api->request->withBody($body);
$api->setRequest($api->getRequest()->withBody($body));

return $api->getResponse();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Clients/VoucherList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Sysix\LexOffice\Clients;

use Sysix\LexOffice\PaginationClient;
use DateTimeInterface;
use Sysix\LexOffice\PaginationClient;

class VoucherList extends PaginationClient
{
Expand Down
25 changes: 25 additions & 0 deletions src/Interfaces/ApiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Sysix\LexOffice\Interfaces;

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

interface ApiInterface
{
public function __construct(string $apiKey, ClientInterface $client);

/**
* @param string[] $headers
*/
public function newRequest(string $method, string $resource, array $headers = []): self;

public function setRequest(RequestInterface $request): self;

public function getRequest(): RequestInterface;

public function getResponse(): ResponseInterface;
}
10 changes: 10 additions & 0 deletions src/Interfaces/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sysix\LexOffice\Interfaces;

interface ClientInterface
{
public function __construct(ApiInterface $lexOffice);
}
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Sysix\LexOffice;

use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\Psr7\MultipartStream;
use GuzzleHttp\Psr7\Stream;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand Down
26 changes: 13 additions & 13 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Sysix\LexOffice\Tests;

use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Sysix\LexOffice\Clients\Contact;
use Sysix\LexOffice\Clients\Country;
use Sysix\LexOffice\Clients\CreditNote;
Expand All @@ -14,14 +16,12 @@
use Sysix\LexOffice\Clients\OrderConfirmation;
use Sysix\LexOffice\Clients\Payment;
use Sysix\LexOffice\Clients\PaymentCondition;
use Sysix\LexOffice\Clients\Profile;
use Sysix\LexOffice\Clients\PostingCategory;
use Sysix\LexOffice\Clients\Profile;
use Sysix\LexOffice\Clients\Quotation;
use Sysix\LexOffice\Clients\RecurringTemplate;
use Sysix\LexOffice\Clients\Voucher;
use Sysix\LexOffice\Clients\VoucherList;
use Sysix\LexOffice\Clients\RecurringTemplate;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\ResponseInterface;

class ApiTest extends TestClient
{
Expand Down Expand Up @@ -61,12 +61,12 @@ public function testApiUrl(): void
new Response(200, [], 'post-content')
);

$this->assertStringStartsWith('api.lexoffice.io', $stub->request->getUri()->getHost());
$this->assertStringStartsWith('api.lexoffice.io', $stub->getRequest()->getUri()->getHost());

$stub->apiUrl = 'https://test.de';
$stub->newRequest('POST', 'post-content');

$this->assertStringStartsWith('test.de', $stub->request->getUri()->getHost());
$this->assertStringStartsWith('test.de', $stub->getRequest()->getUri()->getHost());
}

public function testGetSuccessResponse(): void
Expand Down Expand Up @@ -110,17 +110,17 @@ public function testGetFailedResponse(): void

public function testRequestHeaders(): void
{
$stub = $this->createApiMockObject(
$api = $this->createApiMockObject(
new Response(200, [], 'post-content')
);

$this->assertCount(1, $stub->request->getHeader('Authorization'));
$this->assertEquals('application/json', $stub->request->getHeaderLine('Accept'));
$this->assertCount(1, $api->getRequest()->getHeader('Authorization'));
$this->assertEquals('application/json', $api->getRequest()->getHeaderLine('Accept'));

$stub->newRequest('POST', '/');
$this->assertEquals('application/json', $stub->request->getHeaderLine('Content-Type'));
$api->newRequest('POST', '/');
$this->assertEquals('application/json', $api->getRequest()->getHeaderLine('Content-Type'));

$stub->newRequest('PUT', '/');
$this->assertEquals('application/json', $stub->request->getHeaderLine('Content-Type'));
$api->newRequest('PUT', '/');
$this->assertEquals('application/json', $api->getRequest()->getHeaderLine('Content-Type'));
}
}
Loading

0 comments on commit f4724c1

Please sign in to comment.