Skip to content

Commit

Permalink
Refactor config and add handler stack provider
Browse files Browse the repository at this point in the history
  • Loading branch information
pactode committed Apr 18, 2018
1 parent 3345fda commit e8b10ff
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 22 deletions.
68 changes: 50 additions & 18 deletions config/shopify.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,59 @@
'profile' => \Signifly\Shopify\Laravel\Profiles\ConfigProfile::class,

/*
* The API key from private app credentials.
* The handlerStack to use.
*/
'api_key' => env('SHOPIFY_API_KEY'),
'handlerStackProvider' => \Signifly\Shopify\Laravel\HandlerStacks\DefaultHandlerStackProvider::class,

/*
* The password from private app credentials.
*/
'password' => env('SHOPIFY_PASSWORD'),
'credentials' => [

/*
* The shopify domain for your shop.
*/
'domain' => env('SHOPIFY_DOMAIN'),
/*
* The API key from private app credentials.
*/
'api_key' => env('SHOPIFY_API_KEY'),

/*
* The webhook secret provider to use.
*/
'webhook_secret_provider' => \Signifly\Shopify\Laravel\Webhooks\SecretProviders\ConfigSecretProvider::class,
/*
* The password from private app credentials.
*/
'password' => env('SHOPIFY_PASSWORD'),

/*
* The shopify webhook secret.
*/
'webhook_secret' => env('SHOPIFY_WEBHOOK_SECRET'),
/*
* The shopify domain for your shop.
*/
'domain' => env('SHOPIFY_DOMAIN'),

],

'rate_limit' => [

/*
* The buffer from the max calls limit.
*/
'buffer' => 3,

/*
* The request cycle.
*/
'cycle' => 0.5,

/*
* The processes that can run in parallel.
*/
'processes' => 1,

],

'webhooks' => [

/*
* The webhook secret provider to use.
*/
'secret_provider' => \Signifly\Shopify\Laravel\Webhooks\SecretProviders\ConfigSecretProvider::class,

/*
* The shopify webhook secret.
*/
'secret' => env('SHOPIFY_WEBHOOK_SECRET'),

],
];
2 changes: 1 addition & 1 deletion src/Http/Middleware/ValidateWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function handle($request, Closure $next)

protected function getSecretProvider()
{
$secretProviderClass = config('shopify.webhook_secret_provider');
$secretProviderClass = config('shopify.webhooks.secret_provider');

return new $secretProviderClass();
}
Expand Down
6 changes: 4 additions & 2 deletions src/Profiles/ConfigProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ class ConfigProfile extends CredentialsProfile
{
public function __construct()
{
$config = config('shopify');
$config = config('shopify.credentials');
$providerClass = config('shopify.handlerStackProvider');
$provider = new $providerClass;

parent::__construct($config['api_key'], $config['password'], $config['domain']);
parent::__construct($config['api_key'], $config['password'], $config['domain'], $provider->getHandlerStack());
}
}
29 changes: 29 additions & 0 deletions src/Providers/DefaultHandlerStackProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Signifly\Shopify\Laravel\Providers;

use GuzzleHttp\HandlerStack;
use Concat\Http\Middleware\RateLimiter;
use Concat\Http\Middleware\RateLimitProvider;
use Signifly\Shopify\RateLimit\DefaultRateLimitCalculator;

class DefaultHandlerStackProvider implements HandlerStackProviderContract
{
public function getHandlerStack() : HandlerStack
{
$handlerStack = HandlerStack::create();
$handlerStack->push(new RateLimiter($this->getRateLimitProvider()));
return $handlerStack;
}

protected function getRateLimitProvider() : RateLimitProvider
{
return new DefaultRateLimitProvider(
new DefaultRateLimitCalculator(
config('shopify.rate_limit.buffer'),
config('shopify.rate_limit.cycle'),
config('shopify.rate_limit.processes')
)
);
}
}
78 changes: 78 additions & 0 deletions src/Providers/DefaultRateLimitProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Signifly\Shopify\Laravel\Providers;

use Illuminate\Support\Facades\Cache;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Concat\Http\Middleware\RateLimitProvider;

class DefaultRateLimitProvider implements RateLimitProvider
{
protected $calculator;

public function __construct(RateLimitCalculatorContract $calculator)
{
$this->calculator = $calculator;
}

/**
* Returns when the last request was made.
*
* @return float|null When the last request was made.
*/
public function getLastRequestTime()
{
return Cache::get('last_request_time');
}

/**
* Used to set the current time as the last request time to be queried when
* the next request is attempted.
*/
public function setLastRequestTime()
{
return Cache::forever('last_request_time', microtime(true));
}

/**
* Returns what is considered the time when a given request is being made.
*
* @param RequestInterface $request The request being made.
*
* @return float Time when the given request is being made.
*/
public function getRequestTime(RequestInterface $request)
{
return microtime(true);
}

/**
* Returns the minimum amount of time that is required to have passed since
* the last request was made. This value is used to determine if the current
* request should be delayed, based on when the last request was made.
*
* Returns the allowed time between the last request and the next, which
* is used to determine if a request should be delayed and by how much.
*
* @param RequestInterface $request The pending request.
*
* @return float The minimum amount of time that is required to have passed
* since the last request was made (in seconds).
*/
public function getRequestAllowance(RequestInterface $request)
{
return Cache::get('request_allowance', 0.5);
}

/**
* Used to set the minimum amount of time that is required to pass between
* this request and the next request.
*
* @param ResponseInterface $response The resolved response.
*/
public function setRequestAllowance(ResponseInterface $response)
{
Cache::forever('request_allowance', $this->calculator->calculate());
}
}
13 changes: 13 additions & 0 deletions src/Providers/HandlerStackProviderContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Signifly\Shopify\Laravel\Providers;

use GuzzleHttp\HandlerStack;
use Concat\Http\Middleware\RateLimitProvider;

interface HandlerStackProviderContract
{
public function getHandlerStack() : HandlerStack;

protected function getRateLimitProvider() : RateLimitProvider;
}
2 changes: 1 addition & 1 deletion src/Webhooks/SecretProviders/ConfigSecretProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class ConfigSecretProvider implements SecretProviderContract
{
public function getSecret(string $domain) : string
{
return (string) config('shopify.webhook_secret');
return (string) config('shopify.webhooks.secret');
}
}

0 comments on commit e8b10ff

Please sign in to comment.