-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an availability method to check if Magento is reachable (#42)
Added an availability method to check if Magento is reachable
- Loading branch information
1 parent
64164a1
commit 178cc9a
Showing
16 changed files
with
379 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoClient\Actions; | ||
|
||
use JustBetter\MagentoClient\Contracts\ChecksMagento; | ||
|
||
class CheckMagento implements ChecksMagento | ||
{ | ||
public const AVAILABLE_KEY = 'magento-client:available:'; | ||
|
||
public function available(string $connection): bool | ||
{ | ||
return cache()->get(static::AVAILABLE_KEY.$connection, true); | ||
} | ||
|
||
public static function bind(): void | ||
{ | ||
app()->singleton(ChecksMagento::class, static::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoClient\Contracts; | ||
|
||
interface ChecksMagento | ||
{ | ||
public function available(string $connection): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoClient\Jobs\Middleware; | ||
|
||
use Closure; | ||
use JustBetter\MagentoClient\Client\Magento; | ||
|
||
class AvailableMiddleware | ||
{ | ||
protected string $connection; | ||
|
||
protected int $seconds; | ||
|
||
public function __construct(?string $connection = null, int $seconds = 5) | ||
{ | ||
$this->connection = $connection ?? config('magento.connection'); | ||
$this->seconds = $seconds; | ||
} | ||
|
||
public function handle(object $job, Closure $next): void | ||
{ | ||
/** @var Magento $magento */ | ||
$magento = app(Magento::class); | ||
$magento->connection($this->connection); | ||
|
||
if ($magento->available()) { | ||
$next($job); | ||
} elseif (method_exists($job, 'release')) { | ||
$job->release($this->seconds); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoClient\Listeners; | ||
|
||
use JustBetter\MagentoClient\Actions\CheckMagento; | ||
use JustBetter\MagentoClient\Events\MagentoResponseEvent; | ||
|
||
class StoreAvailabilityListener | ||
{ | ||
public const COUNT_KEY = 'magento-client:response:count:unavailable:'; | ||
|
||
public function handle(MagentoResponseEvent $event): void | ||
{ | ||
/** @var array<int, int> $codes */ | ||
$codes = config('magento.connections.'.$event->connection.'.availability.codes', [502, 503, 504]); | ||
|
||
if (! in_array($event->response->status(), $codes)) { | ||
return; | ||
} | ||
|
||
$countKey = static::COUNT_KEY.$event->connection; | ||
|
||
/** @var int $count */ | ||
$count = cache()->get($countKey, 0); | ||
$count++; | ||
|
||
/** @var int $threshold */ | ||
$threshold = config('magento.connections.'.$event->connection.'.availability.threshold', 10); | ||
|
||
/** @var int $timespan */ | ||
$timespan = config('magento.connections.'.$event->connection.'.availability.timespan', 10); | ||
|
||
/** @var int $cooldown */ | ||
$cooldown = config('magento.connections.'.$event->connection.'.availability.cooldown', 2); | ||
|
||
cache()->put($countKey, $count, now()->addMinutes($timespan)); | ||
|
||
if ($count >= $threshold) { | ||
cache()->put(CheckMagento::AVAILABLE_KEY.$event->connection, false, now()->addMinutes($cooldown)); | ||
|
||
cache()->forget($countKey); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoClient\Tests\Actions; | ||
|
||
use JustBetter\MagentoClient\Actions\CheckMagento; | ||
use JustBetter\MagentoClient\Tests\TestCase; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class CheckMagentoTest extends TestCase | ||
{ | ||
#[Test] | ||
public function it_can_be_available(): void | ||
{ | ||
/** @var CheckMagento $action */ | ||
$action = app(CheckMagento::class); | ||
|
||
$this->assertTrue($action->available('default')); | ||
} | ||
|
||
#[Test] | ||
public function it_can_be_unavailable(): void | ||
{ | ||
/** @var CheckMagento $action */ | ||
$action = app(CheckMagento::class); | ||
|
||
cache()->put(CheckMagento::AVAILABLE_KEY.config('magento.connection'), false); | ||
|
||
$this->assertFalse($action->available('default')); | ||
} | ||
|
||
#[Test] | ||
public function it_can_handle_multiple_connections(): void | ||
{ | ||
/** @var CheckMagento $action */ | ||
$action = app(CheckMagento::class); | ||
|
||
cache()->put(CheckMagento::AVAILABLE_KEY.'connection', false); | ||
cache()->put(CheckMagento::AVAILABLE_KEY.'another-connection', true); | ||
|
||
$this->assertFalse($action->available('connection')); | ||
$this->assertTrue($action->available('another-connection')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoClient\Tests\Enums; | ||
|
||
use JustBetter\MagentoClient\Enums\AuthenticationMethod; | ||
use JustBetter\MagentoClient\Providers\BearerTokenProvider; | ||
use JustBetter\MagentoClient\Providers\OAuthProvider; | ||
use JustBetter\MagentoClient\Tests\TestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class AuthenticationMethodTest extends TestCase | ||
{ | ||
#[Test] | ||
#[DataProvider('providers')] | ||
public function it_can_get_the_provider(AuthenticationMethod $authenticationMethod, string $expectedProvider): void | ||
{ | ||
/** @var class-string $expectedProvider */ | ||
$this->assertInstanceOf($expectedProvider, $authenticationMethod->provider()); | ||
} | ||
|
||
public static function providers(): array | ||
{ | ||
return [ | ||
[ | ||
AuthenticationMethod::Token, | ||
BearerTokenProvider::class, | ||
], | ||
[ | ||
AuthenticationMethod::OAuth, | ||
OAuthProvider::class, | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace JustBetter\MagentoClient\Tests\Fakes; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldBeUnique; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
|
||
class TestJob implements ShouldBeUnique, ShouldQueue | ||
{ | ||
use Dispatchable; | ||
use InteractsWithQueue; | ||
use Queueable; | ||
|
||
public function handle(): void | ||
{ | ||
// | ||
} | ||
} |
Oops, something went wrong.