feat(смс): точка расширения резерва канала (флаг, по умолчанию выкл)
This commit is contained in:
@@ -42,4 +42,27 @@ final class SmsRouter
|
||||
|
||||
return $candidates[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Запасной канал, если основной отказал. Работает ТОЛЬКО при включённом
|
||||
* флаге services.sms.failover_to_universal (по умолчанию ВЫКЛ). Возвращает
|
||||
* универсала, отличного от основного. Точка расширения — сейчас не вызывается
|
||||
* из джоба (решение владельца 23.07.2026 — без резерва).
|
||||
*/
|
||||
public function fallbackFor(string $operator): ?SmsProvider
|
||||
{
|
||||
if (! config('services.sms.failover_to_universal')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$primary = $this->pick($operator);
|
||||
|
||||
foreach ($this->providers as $provider) {
|
||||
if (in_array('*', $provider->servesOperators(), true) && $provider !== $primary) {
|
||||
return $provider;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Services\Sms\SmsOutgoing;
|
||||
use App\Services\Sms\SmsProvider;
|
||||
use App\Services\Sms\SmsRouter;
|
||||
use App\Services\Sms\SmsSendResult;
|
||||
use Carbon\CarbonImmutable;
|
||||
|
||||
function failover_provider(string $key, array $operators): SmsProvider
|
||||
{
|
||||
return new class($key, $operators) implements SmsProvider
|
||||
{
|
||||
public function __construct(private string $k, private array $ops) {}
|
||||
|
||||
public function key(): string
|
||||
{
|
||||
return $this->k;
|
||||
}
|
||||
|
||||
public function servesOperators(): array
|
||||
{
|
||||
return $this->ops;
|
||||
}
|
||||
|
||||
public function priceKopecks(string $operator): int
|
||||
{
|
||||
return 100;
|
||||
}
|
||||
|
||||
public function send(SmsOutgoing $m): SmsSendResult
|
||||
{
|
||||
return new SmsSendResult('x', 1, 100, CarbonImmutable::now());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
it('по умолчанию резерва нет — второй канал не отдаётся', function () {
|
||||
config()->set('services.sms.failover_to_universal', false);
|
||||
$router = new SmsRouter([
|
||||
failover_provider('mts', ['mts']),
|
||||
failover_provider('smsc', ['*']),
|
||||
]);
|
||||
|
||||
expect($router->fallbackFor('mts'))->toBeNull();
|
||||
});
|
||||
|
||||
it('при включённом флаге резерв — универсал', function () {
|
||||
config()->set('services.sms.failover_to_universal', true);
|
||||
$router = new SmsRouter([
|
||||
failover_provider('mts', ['mts']),
|
||||
failover_provider('smsc', ['*']),
|
||||
]);
|
||||
|
||||
expect($router->fallbackFor('mts')?->key())->toBe('smsc');
|
||||
});
|
||||
Reference in New Issue
Block a user