Skip to content

Commit

Permalink
Adds options to dynamically pass the account configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagof committed Sep 17, 2024
1 parent fab9ebb commit 1712e37
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 14 deletions.
28 changes: 28 additions & 0 deletions src/API/Data/AccountConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Squarebit\InvoiceXpress\API\Data;

use Spatie\LaravelData\Attributes\MapName;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;

#[MapName(SnakeCaseMapper::class)]
class AccountConfig extends Data
{
public function __construct(
public string $serviceEndpoint,
public string $name,
public string $apiKey,
) {}

public static function default(): self
{
$cfg = config('invoicexpress-for-laravel');

return new self(
serviceEndpoint: $cfg['service_endpoint'],
name: $cfg['account']['name'],
apiKey: $cfg['account']['api_key'],
);
}
}
12 changes: 7 additions & 5 deletions src/API/Endpoints/Config/EndpointConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace Squarebit\InvoiceXpress\API\Endpoints\Config;

use Exception;
use Squarebit\InvoiceXpress\API\Data\AccountConfig;

class EndpointConfig
{
protected ?array $endpointData = null;

public function __construct(
public string $object,
public string $action,
protected AccountConfig $accountConfig,
protected string $object,
protected string $action,
) {
throw_unless(
is_array($cfg = EndpointsConfig::get($object.'.'.$action)),
Expand All @@ -29,14 +31,14 @@ public function getMethod(): string
public function getUrl(?array $queryParams = []): string
{
$queryParams = array_merge(
['api_key' => config('invoicexpress-for-laravel.account.api_key')],
['api_key' => $this->accountConfig->apiKey],
$queryParams
);

return 'https://'.
config('invoicexpress-for-laravel.account.name').
$this->accountConfig->name.
'.'.
config('invoicexpress-for-laravel.service_endpoint').
$this->accountConfig->serviceEndpoint.
'/'.
$this->endpointData['path'].
'?'.
Expand Down
10 changes: 9 additions & 1 deletion src/API/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Spatie\LaravelData\Data;
use Squarebit\InvoiceXpress\API\Data\AccountConfig;
use Squarebit\InvoiceXpress\API\Endpoints\Config\EndpointConfig;
use Squarebit\InvoiceXpress\API\Exceptions\UnknownAPIMethodException;

Expand All @@ -17,16 +18,23 @@ abstract class Endpoint
{
protected ?int $lastResponseCode;

protected AccountConfig $config;

/**
* @return T
*/
abstract protected function responseToDataObject(array $data): Data;

abstract protected function getEndpointName(): string;

public function __construct(?AccountConfig $accountConfig = null)
{
$this->config = $accountConfig ?? AccountConfig::default();
}

public function getEndpointConfig(string $action): EndpointConfig
{
return new EndpointConfig($this->getEndpointName(), $action);
return new EndpointConfig($this->config, $this->getEndpointName(), $action);
}

/**
Expand Down
26 changes: 18 additions & 8 deletions src/InvoiceXpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Squarebit\InvoiceXpress;

use Squarebit\InvoiceXpress\API\Data\AccountConfig;
use Squarebit\InvoiceXpress\API\Endpoints\ClientsEndpoint;
use Squarebit\InvoiceXpress\API\Endpoints\EstimatesEndpoint;
use Squarebit\InvoiceXpress\API\Endpoints\GuidesEndpoint;
Expand All @@ -17,43 +18,52 @@ class InvoiceXpress

public const DATE_TIME_FORMAT = 'd/m/Y H:i:s';

protected ?AccountConfig $accountConfig = null;

public function useConfig(AccountConfig $config): self
{
$this->accountConfig = $config;

return $this;
}

public function clients(): ClientsEndpoint
{
return new ClientsEndpoint;
return new ClientsEndpoint($this->accountConfig);
}

public function items(): ItemsEndpoint
{
return new ItemsEndpoint;
return new ItemsEndpoint($this->accountConfig);
}

public function sequences(): SequencesEndpoint
{
return new SequencesEndpoint;
return new SequencesEndpoint($this->accountConfig);
}

public function taxes(): TaxesEndpoint
{
return new TaxesEndpoint;
return new TaxesEndpoint($this->accountConfig);
}

public function saft(): SaftEndpoint
{
return new SaftEndpoint;
return new SaftEndpoint($this->accountConfig);
}

public function invoices(): InvoicesEndpoint
{
return new InvoicesEndpoint;
return new InvoicesEndpoint($this->accountConfig);
}

public function guides(): GuidesEndpoint
{
return new GuidesEndpoint;
return new GuidesEndpoint($this->accountConfig);
}

public function estimates(): EstimatesEndpoint
{
return new EstimatesEndpoint;
return new EstimatesEndpoint($this->accountConfig);
}
}

0 comments on commit 1712e37

Please sign in to comment.