Skip to content

Commit

Permalink
Add: basic plytix setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jordyvanderhaegen committed Feb 28, 2024
1 parent 5bc6d9a commit 6314d2e
Show file tree
Hide file tree
Showing 21 changed files with 553 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vendor
composer.lock
.phpunit.result.cache
.phpunit.cache
.php-cs-fixer.cache
13 changes: 6 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
],
"require": {
"php": "^8.0",
"illuminate/support": "^8.0"
"illuminate/support": "^10.0",
"saloonphp/rate-limit-plugin": "^2.0",
"saloonphp/saloon": "^3.6"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.5",
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.0"
"orchestra/testbench": "^8.0",
"phpunit/phpunit": "^10.0"
},
"autoload": {
"psr-4": {
Expand All @@ -45,10 +47,7 @@
"laravel": {
"providers": [
"Esign\\Plytix\\PlytixServiceProvider"
],
"aliases": {
"Plytix": "Esign\\Plytix\\Facades\\PlytixFacade"
}
]
}
},
"minimum-stability": "dev",
Expand Down
41 changes: 41 additions & 0 deletions config/plytix.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
<?php

use Saloon\RateLimitPlugin\Limit;

return [
/**
* The API key to be used for authenticating with the Plytix API.
*/
'api_key' => env('PLYTIX_API_KEY'),

/**
* The API password to be used for authenticating with the Plytix API.
*/
'api_password' => env('PLYTIX_API_PASSWORD'),

'authenticator_cache' => [
/**
* The key that will be used to cache the Plytix access token.
*/
'key' => 'esign.plytix.authenticator',

/**
* The cache store to be used for the Plytix access token.
* Use null to utilize the default cache store from the cache.php config file.
* To disable caching, you can use the 'array' store.
*/
'store' => null,
],

'rate_limiting' => [
/**
* The rate limits to be used for the Plytix API.
*/
'limits' => [
Limit::allow(20)->everySeconds(10),
Limit::allow(2000)->everyHour(),
],

/**
* The cache store to be used for the Plytix rate limits.
* Use null to utilize the default cache store from the cache.php config file.
* To disable caching, you can use the 'array' store.
*/
'cache_store' => null,
],
];
48 changes: 24 additions & 24 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true" verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Esign Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing"/>
</php>
<testsuites>
<testsuite name="Esign Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="DB_CONNECTION" value="testing"/>
<env name="PLYTIX_API_KEY" value="fake-api-key"/>
<env name="PLYTIX_API_PASSWORD" value="fake-api-password"/>
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
13 changes: 0 additions & 13 deletions src/Facades/PlytixFacade.php

This file was deleted.

57 changes: 56 additions & 1 deletion src/Plytix.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,61 @@

namespace Esign\Plytix;

class Plytix
use Esign\Plytix\Requests\TokenRequest;
use Illuminate\Support\Facades\Cache;
use Saloon\Contracts\Authenticator;
use Saloon\Http\Auth\TokenAuthenticator;
use Saloon\Http\Connector;
use Saloon\RateLimitPlugin\Contracts\RateLimitStore;
use Saloon\RateLimitPlugin\Limit;
use Saloon\RateLimitPlugin\Stores\LaravelCacheStore;
use Saloon\RateLimitPlugin\Traits\HasRateLimits;
use Saloon\Traits\Plugins\AlwaysThrowOnErrors;

class Plytix extends Connector
{
use AlwaysThrowOnErrors;
use HasRateLimits;

public function resolveBaseUrl(): string
{
return 'https://pim.plytix.com';
}

protected function defaultAuth(): ?Authenticator
{
$cacheStore = Cache::store(config('plytix.authenticator_cache.store'));
$cachedAuthenticator = $cacheStore->get(config('plytix.authenticator_cache.key'));

if ($cachedAuthenticator instanceof PlytixTokenAuthenticator && ! $cachedAuthenticator->hasExpired()) {
return $cachedAuthenticator;
}

$tokenResponse = (new PlytixAuth())->send(new TokenRequest(
config('plytix.api_key'),
config('plytix.api_password')
));

$authenticator = new PlytixTokenAuthenticator($tokenResponse->json('data.0.access_token'));

$cacheStore->put(
key: config('plytix.authenticator_cache.key'),
value: $authenticator,
ttl: $authenticator->expiresAt
);

return $authenticator;
}

protected function resolveRateLimitStore(): RateLimitStore
{
return new LaravelCacheStore(
Cache::store(config('plytix.rate_limiting.cache_store'))
);
}

protected function resolveLimits(): array
{
return config('plytix.rate_limiting.limits');
}
}
16 changes: 16 additions & 0 deletions src/PlytixAuth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Esign\Plytix;

use Saloon\Http\Connector;
use Saloon\Traits\Plugins\AcceptsJson;

class PlytixAuth extends Connector
{
use AcceptsJson;

public function resolveBaseUrl(): string
{
return 'https://auth.plytix.com';
}
}
26 changes: 26 additions & 0 deletions src/PlytixTokenAuthenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Esign\Plytix;

use DateTimeImmutable;
use Saloon\Contracts\Authenticator;
use Saloon\Http\PendingRequest;

class PlytixTokenAuthenticator implements Authenticator
{
public function __construct(
public readonly string $token,
public readonly DateTimeImmutable $expiresAt = new DateTimeImmutable('+15 minutes'),
) {
}

public function set(PendingRequest $pendingRequest): void
{
$pendingRequest->headers()->add('Authorization', 'Bearer ' . $this->token);
}

public function hasExpired(): bool
{
return $this->expiresAt->getTimestamp() <= (new DateTimeImmutable)->getTimestamp();
}
}
29 changes: 29 additions & 0 deletions src/Requests/CreateProductRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Esign\Plytix\Requests;

use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class CreateProductRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(protected array $payload)
{
}

public function resolveEndpoint(): string
{
return '/api/v1/products';
}

public function defaultBody(): array
{
return $this->payload;
}
}
34 changes: 34 additions & 0 deletions src/Requests/TokenRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Esign\Plytix\Requests;

use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class TokenRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(
protected string $apiKey,
protected string $apiPassword,
) {
}

public function resolveEndpoint(): string
{
return '/auth/api/get-token';
}

protected function defaultBody(): array
{
return [
'api_key' => $this->apiKey,
'api_password' => $this->apiPassword,
];
}
}
28 changes: 28 additions & 0 deletions src/Requests/UpdateProductRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Esign\Plytix\Requests;

use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Traits\Body\HasJsonBody;

class UpdateProductRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::PATCH;

public function __construct(protected string $productId, protected array $payload)
{}

public function resolveEndpoint(): string
{
return '/api/v1/products/' . $this->productId;
}

public function defaultBody(): array
{
return $this->payload;
}
}
Loading

0 comments on commit 6314d2e

Please sign in to comment.