Skip to content

Commit

Permalink
Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
stfndamjanovic committed Jan 18, 2024
1 parent bc6c0ca commit 3a66cc1
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 257 deletions.
58 changes: 0 additions & 58 deletions .github/ISSUE_TEMPLATE/bug.yml

This file was deleted.

11 changes: 0 additions & 11 deletions .github/ISSUE_TEMPLATE/config.yml

This file was deleted.

19 changes: 0 additions & 19 deletions .github/dependabot.yml

This file was deleted.

25 changes: 0 additions & 25 deletions .github/workflows/dependabot-auto-merge.yml

This file was deleted.

31 changes: 0 additions & 31 deletions .github/workflows/update-changelog.yml

This file was deleted.

7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "stfn/circuit-breaker",
"description": "This is my package circuit-breaker",
"description": "This is circuit-breaker pattern implemented in PHP",
"keywords": [
"stfndamjanovic",
"circuit-breaker"
"circuit-breaker-php"
],
"homepage": "https://github.com/stfndamjanovic/circuit-breaker",
"license": "MIT",
Expand All @@ -19,8 +19,7 @@
},
"require-dev": {
"phpunit/phpunit": "^10.3.2",
"friendsofphp/php-cs-fixer": "^3.21.1",
"spatie/ray": "^1.28"
"friendsofphp/php-cs-fixer": "^3.21.1"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/CircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static function for(string $service)
*/
public function withOptions(array $options): self
{
$this->config = Config::make($options);
$this->config = Config::fromArray($options);

return $this;
}
Expand Down
41 changes: 27 additions & 14 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,48 @@

namespace Stfn\CircuitBreaker;

use Stfn\CircuitBreaker\Utilities\Str;

class Config
{
/**
* @var int
*/
public int $failureThreshold = 5;
public int $failureThreshold;

/**
* @var int
*/
public int $recoveryTime = 60;
public int $recoveryTime;

/**
* @param int $failureThreshold
* @param int $recoveryTime
*/
public function __construct(int $failureThreshold = 5, int $recoveryTime = 60)
{
$this->failureThreshold = $failureThreshold;
$this->recoveryTime = $recoveryTime;
}

/**
* @param array $config
* @return Config
*/
public static function make(array $config = []): Config
public static function fromArray(array $config = []): Config
{
$object = new self();

foreach ($config as $property => $value) {
$property = Str::camelize($property);
if (property_exists($object, $property)) {
$object->{$property} = $value;
}
}
return new Config(
$config['failure_threshold'] ?? 5,
$config['recovery_time'] ?? 60
);
}

return $object;
/**
* @return array
*/
public function toArray()
{
return [
'failure_threshold' => $this->failureThreshold,
'recovery_time' => $this->recoveryTime
];
}
}
21 changes: 4 additions & 17 deletions src/Storage/CircuitBreakerStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
abstract class CircuitBreakerStorage
{
/**
* @var string
*/
protected string $service = '';

/**
* @return string
* @param string $service
* @return void
*/
public function getService(): string
public function init(string $service): void
{
return $this->service;

}

/**
Expand All @@ -30,15 +26,6 @@ abstract public function getState(): CircuitState;
*/
abstract public function setState(CircuitState $state): void;

/**
* @param string $service
* @return void
*/
public function init(string $service): void
{

}

/**
* @return void
*/
Expand Down
16 changes: 0 additions & 16 deletions src/Utilities/Str.php

This file was deleted.

61 changes: 0 additions & 61 deletions tests/CircuitBreakerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,6 @@ public function test_if_it_will_throw_an_exception_if_circuit_breaker_is_open()
});
}

public function test_if_it_will_record_every_success()
{
$breaker = CircuitBreaker::for('test-service');

$success = function () {
return true;
};

$tries = 3;

foreach (range(1, $tries) as $i) {
$breaker->call($success);
}

$this->assertEquals(0, $breaker->getStorage()->getFailuresCount());
}

public function test_if_it_will_record_every_failure()
{
$breaker = CircuitBreaker::for('test-service')
Expand Down Expand Up @@ -101,27 +84,6 @@ public function test_if_it_will_open_circuit_after_failure_threshold()
}

$this->assertTrue($breaker->isOpen());
}

public function test_if_counter_is_reset_after_circuit_change_state_from_close_to_open()
{
$breaker = CircuitBreaker::for('test-service')
->withOptions(['failure_threshold' => 3]);

$fail = function () {
throw new \Exception();
};

$tries = 4;

foreach (range(1, $tries) as $i) {
try {
$breaker->call($fail);
} catch (\Exception) {

}
}

$this->assertEquals(0, $breaker->getStorage()->getFailuresCount());
}

Expand Down Expand Up @@ -234,27 +196,4 @@ public function test_if_it_can_fail_even_without_exception()
$this->assertEquals(0, $breaker->getStorage()->getFailuresCount());
$this->assertTrue($breaker->isOpen());
}

// public function test_if_redis_work()
// {
// $redis = new \Redis();
// $redis->connect('127.0.0.1');
//
// $store = new RedisStorage('test-service', $redis);
//
// $config = Config::make([
// 'recovery_time' => 60,
// 'failure_threshold' => 3
// ]);
//
// $breaker = new CircuitBreaker($config, $store);
//
// $success = function () {
// return true;
// };
//
// $result = $breaker->call($success);
//
// dd($result);
// }
}

0 comments on commit 3a66cc1

Please sign in to comment.