* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ namespace PrestaShop\Module\PsAccounts\Factory; use PrestaShop\Module\PsAccounts\Adapter\Configuration; use PrestaShop\Module\PsAccounts\Api\Client\AccountsClient; use PrestaShop\Module\PsAccounts\Http\Client\CircuitBreaker\CircuitBreaker; use PrestaShop\Module\PsAccounts\Http\Client\CircuitBreaker\PersistentCircuitBreaker; class CircuitBreakerFactory { /** * @var array */ private $provides = [ AccountsClient::class, ]; /** * @var array */ private $instances = []; /** * @var Configuration */ private $configStorage; /** * @param Configuration $configStorage */ public function __construct( Configuration $configStorage ) { $this->configStorage = $configStorage; } /** * @param string $resourceId * * @return CircuitBreaker * * @throws \Exception */ public function createInstance($resourceId) { $instance = new PersistentCircuitBreaker( static::className($resourceId), 'PS_ACCOUNTS', $this->configStorage ); $instance->setDefaultFallbackResponse([ 'status' => false, 'httpCode' => 500, 'body' => ['message' => 'Circuit Breaker Open'], ]); $this->instances[$resourceId] = $instance; return $instance; } /** * @param string $resourceId * * @return CircuitBreaker * * @throws \Exception */ public function getOrCreate($resourceId) { if (!array_key_exists($resourceId, $this->instances)) { static::createInstance($resourceId); } return $this->instances[$resourceId]; } /** * @return void * * @throws \Exception */ public function resetAll() { foreach ($this->provides as $class) { static::getOrCreate($class)->reset(); } } /** * @param string $resourceId * * @return CircuitBreaker * * @throws \Exception */ public static function create($resourceId) { /** @var \Ps_accounts $module */ $module = \Module::getInstanceByName('ps_accounts'); /** @var CircuitBreakerFactory $factory */ $factory = $module->getService(CircuitBreakerFactory::class); return $factory->createInstance($resourceId); } /** * @param string $className * * @return string */ protected function className($className) { return strtoupper(preg_replace(['/^.*\\\\/', '/([^A-Z])([A-Z])/'], ['', '$1_$2'], $className)); } }