-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from justbetter/feature/availability
Add availability
- Loading branch information
Showing
20 changed files
with
398 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace JustBetter\DynamicsClient\Actions\Availability; | ||
|
||
use JustBetter\DynamicsClient\Contracts\Availability\ChecksAvailability; | ||
|
||
class CheckAvailability implements ChecksAvailability | ||
{ | ||
const AVAILABLE_KEY = 'dynamics-client:availability:'; | ||
|
||
public function check(string $connection): bool | ||
{ | ||
return cache()->get(static::AVAILABLE_KEY.$connection, true); | ||
} | ||
|
||
public static function bind(): void | ||
{ | ||
app()->singleton(ChecksAvailability::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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace JustBetter\DynamicsClient\Actions\Availability; | ||
|
||
use JustBetter\DynamicsClient\Contracts\Availability\RegistersUnavailability; | ||
|
||
class RegisterUnavailability implements RegistersUnavailability | ||
{ | ||
public const COUNT_KEY = 'dynamics-client:unavailable-count:'; | ||
|
||
public function register(string $connection): void | ||
{ | ||
$countKey = static::COUNT_KEY.$connection; | ||
|
||
/** @var int $count */ | ||
$count = cache()->get($countKey, 0); | ||
$count++; | ||
|
||
/** @var int $threshold */ | ||
$threshold = config('dynamics.connections.'.$connection.'.availability.threshold', 10); | ||
|
||
/** @var int $timespan */ | ||
$timespan = config('dynamics.connections.'.$connection.'.availability.timespan', 10); | ||
|
||
/** @var int $cooldown */ | ||
$cooldown = config('dynamics.connections.'.$connection.'.availability.cooldown', 2); | ||
|
||
cache()->put($countKey, $count, now()->addMinutes($timespan)); | ||
|
||
if ($count >= $threshold) { | ||
cache()->put(CheckAvailability::AVAILABLE_KEY.$connection, false, now()->addMinutes($cooldown)); | ||
|
||
cache()->forget($countKey); | ||
} | ||
} | ||
|
||
public static function bind(): void | ||
{ | ||
app()->singleton(RegistersUnavailability::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
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\DynamicsClient\Contracts\Availability; | ||
|
||
interface ChecksAvailability | ||
{ | ||
public function check(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace JustBetter\DynamicsClient\Contracts\Availability; | ||
|
||
interface RegistersUnavailability | ||
{ | ||
public function register(string $connection): void; | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace JustBetter\DynamicsClient\Events; | ||
|
||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Http\Client\Response; | ||
|
||
class DynamicsResponseEvent | ||
{ | ||
use Dispatchable; | ||
|
||
public function __construct( | ||
public Response $response, | ||
public string $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace JustBetter\DynamicsClient\Events; | ||
|
||
use Illuminate\Foundation\Events\Dispatchable; | ||
|
||
class DynamicsTimeoutEvent | ||
{ | ||
use Dispatchable; | ||
|
||
public function __construct( | ||
public string $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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace JustBetter\DynamicsClient\Exceptions; | ||
|
||
class UnavailableException extends DynamicsException {} |
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,23 @@ | ||
<?php | ||
|
||
namespace JustBetter\DynamicsClient\Listeners; | ||
|
||
use JustBetter\DynamicsClient\Contracts\Availability\RegistersUnavailability; | ||
use JustBetter\DynamicsClient\Events\DynamicsResponseEvent; | ||
|
||
class ResponseAvailabilityListener | ||
{ | ||
public function __construct(protected RegistersUnavailability $unavailability) {} | ||
|
||
public function handle(DynamicsResponseEvent $event): void | ||
{ | ||
/** @var array<int, int> $codes */ | ||
$codes = config('dynamics.connections.'.$event->connection.'.availability.codes', [502, 503, 504]); | ||
|
||
if (! in_array($event->response->status(), $codes)) { | ||
return; | ||
} | ||
|
||
$this->unavailability->register($event->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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace JustBetter\DynamicsClient\Listeners; | ||
|
||
use JustBetter\DynamicsClient\Contracts\Availability\RegistersUnavailability; | ||
use JustBetter\DynamicsClient\Events\DynamicsTimeoutEvent; | ||
|
||
class TimeoutAvailabilityListener | ||
{ | ||
public function __construct(protected RegistersUnavailability $unavailability) {} | ||
|
||
public function handle(DynamicsTimeoutEvent $event): void | ||
{ | ||
$this->unavailability->register($event->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
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,23 @@ | ||
<?php | ||
|
||
namespace JustBetter\DynamicsClient\Tests\Actions\Availability; | ||
|
||
use JustBetter\DynamicsClient\Actions\Availability\CheckAvailability; | ||
use JustBetter\DynamicsClient\Tests\TestCase; | ||
use PHPUnit\Framework\Attributes\Test; | ||
|
||
class CheckAvailabilityTest extends TestCase | ||
{ | ||
#[Test] | ||
public function it_checks_availability(): void | ||
{ | ||
/** @var CheckAvailability $action */ | ||
$action = app(CheckAvailability::class); | ||
|
||
$this->assertTrue($action->check('default')); | ||
|
||
cache()->put(CheckAvailability::AVAILABLE_KEY.'default', false); | ||
|
||
$this->assertFalse($action->check('default')); | ||
} | ||
} |
Oops, something went wrong.