diff --git a/.github/ISSUE_TEMPLATE/bug.yml b/.github/ISSUE_TEMPLATE/bug.yml deleted file mode 100644 index a9933e1..0000000 --- a/.github/ISSUE_TEMPLATE/bug.yml +++ /dev/null @@ -1,58 +0,0 @@ -name: Bug Report -description: Report an Issue or Bug with the Package -title: "[Bug]: " -labels: ["bug"] -body: - - type: markdown - attributes: - value: | - We're sorry to hear you have a problem. Can you help us solve it by providing the following details. - - type: textarea - id: what-happened - attributes: - label: What happened? - description: What did you expect to happen? - placeholder: I cannot currently do X thing because when I do, it breaks X thing. - validations: - required: true - - type: textarea - id: how-to-reproduce - attributes: - label: How to reproduce the bug - description: How did this occur, please add any config values used and provide a set of reliable steps if possible. - placeholder: When I do X I see Y. - validations: - required: true - - type: input - id: package-version - attributes: - label: Package Version - description: What version of our Package are you running? Please be as specific as possible - placeholder: 2.0.0 - validations: - required: true - - type: input - id: php-version - attributes: - label: PHP Version - description: What version of PHP are you running? Please be as specific as possible - placeholder: 8.2.0 - validations: - required: true - - type: dropdown - id: operating-systems - attributes: - label: Which operating systems does with happen with? - description: You may select more than one. - multiple: true - options: - - macOS - - Windows - - Linux - - type: textarea - id: notes - attributes: - label: Notes - description: Use this field to provide any other notes that you feel might be relevant to the issue. - validations: - required: false diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml deleted file mode 100644 index 686b00b..0000000 --- a/.github/ISSUE_TEMPLATE/config.yml +++ /dev/null @@ -1,11 +0,0 @@ -blank_issues_enabled: false -contact_links: - - name: Ask a question - url: https://github.com/stfndamjanovic/circuit-breaker/discussions/new?category=q-a - about: Ask the community for help - - name: Request a feature - url: https://github.com/stfndamjanovic/circuit-breaker/discussions/new?category=ideas - about: Share ideas for new features - - name: Report a security issue - url: https://github.com/stfndamjanovic/circuit-breaker/security/policy - about: Learn how to notify us for sensitive bugs diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 39b1580..0000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,19 +0,0 @@ -# Please see the documentation for all configuration options: -# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates - -version: 2 -updates: - - - package-ecosystem: "github-actions" - directory: "/" - schedule: - interval: "weekly" - labels: - - "dependencies" - - - package-ecosystem: "composer" - directory: "/" - schedule: - interval: "weekly" - labels: - - "dependencies" diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml deleted file mode 100644 index b34f094..0000000 --- a/.github/workflows/dependabot-auto-merge.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: dependabot-auto-merge -on: pull_request_target - -permissions: - pull-requests: write - contents: write - -jobs: - dependabot: - runs-on: ubuntu-latest - if: ${{ github.actor == 'dependabot[bot]' }} - steps: - - - name: Dependabot metadata - id: metadata - uses: dependabot/fetch-metadata@v1.6.0 - with: - github-token: "${{ secrets.GITHUB_TOKEN }}" - - - name: Auto-merge Dependabot PRs for semver-minor/patch updates - if: ${{steps.metadata.outputs.update-type != 'version-update:semver-major'}} - run: gh pr merge --auto --merge "$PR_URL" - env: - PR_URL: ${{github.event.pull_request.html_url}} - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml deleted file mode 100644 index a817f81..0000000 --- a/.github/workflows/update-changelog.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: "Update Changelog" - -on: - release: - types: [released] - -permissions: - contents: write - -jobs: - update: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - ref: main - - - name: Update Changelog - uses: stefanzweifel/changelog-updater-action@v1 - with: - latest-version: ${{ github.event.release.name }} - release-notes: ${{ github.event.release.body }} - - - name: Commit updated CHANGELOG - uses: stefanzweifel/git-auto-commit-action@v4 - with: - branch: main - commit_message: Update CHANGELOG - file_pattern: CHANGELOG.md diff --git a/composer.json b/composer.json index 549eb4d..b106b84 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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": { diff --git a/src/CircuitBreaker.php b/src/CircuitBreaker.php index a0dcd00..0ad7a25 100755 --- a/src/CircuitBreaker.php +++ b/src/CircuitBreaker.php @@ -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; } diff --git a/src/Config.php b/src/Config.php index 5531341..50228b7 100644 --- a/src/Config.php +++ b/src/Config.php @@ -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 + ]; } } diff --git a/src/Storage/CircuitBreakerStorage.php b/src/Storage/CircuitBreakerStorage.php index fa272a3..e2c2ab9 100644 --- a/src/Storage/CircuitBreakerStorage.php +++ b/src/Storage/CircuitBreakerStorage.php @@ -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; + } /** @@ -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 */ diff --git a/src/Utilities/Str.php b/src/Utilities/Str.php deleted file mode 100644 index c315d62..0000000 --- a/src/Utilities/Str.php +++ /dev/null @@ -1,16 +0,0 @@ -call($success); - } - - $this->assertEquals(0, $breaker->getStorage()->getFailuresCount()); - } - public function test_if_it_will_record_every_failure() { $breaker = CircuitBreaker::for('test-service') @@ -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()); } @@ -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); - // } }