Files
clinicpro/src/Payment/Service/CircuitBreakerService.php
T
hamed de1a78a235 feat: Implement SMS sending functionality with KavehNegar and Rangineh providers
- Add SendSmsMessage class for encapsulating SMS message data.
- Create KavehNegarProvider and RanginehProvider classes implementing SmsProviderInterface for sending SMS.
- Implement SmsLogRepository and SmsTemplateRepository for managing SMS logs and templates.
- Develop SendSmsHandler for handling SMS sending messages.
- Create SmsService to manage SMS dispatching and logging.
- Add UserProfileController for managing user profiles with CRUD operations.
- Implement UserProfile entity and repository for user profile data management.
- Update symfony.lock and bootstrap.php for project dependencies and environment setup.
2026-06-09 22:00:34 +03:30

43 lines
1.2 KiB
PHP

<?php
namespace App\Payment\Service;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class CircuitBreakerService
{
private const FAIL_THRESHOLD = 3;
private const OPEN_TTL = 300; // 5 minutes
public function __construct(private readonly CacheInterface $cache) {}
public function isOpen(string $gateway): bool
{
$item = $this->cache->getItem('cb_open_' . $gateway);
return $item->isHit();
}
public function recordFailure(string $gateway): void
{
$countKey = 'cb_fail_' . $gateway;
$item = $this->cache->getItem($countKey);
$count = ($item->isHit() ? (int) $item->get() : 0) + 1;
$item->set($count)->expiresAfter(self::OPEN_TTL);
$this->cache->save($item);
if ($count >= self::FAIL_THRESHOLD) {
$openItem = $this->cache->getItem('cb_open_' . $gateway);
$openItem->set(true)->expiresAfter(self::OPEN_TTL);
$this->cache->save($openItem);
}
}
public function recordSuccess(string $gateway): void
{
$this->cache->deleteItem('cb_fail_' . $gateway);
$this->cache->deleteItem('cb_open_' . $gateway);
}
}