CircuitBreakerFactory   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 23
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 12 3
1
<?php
2
3
/**
4
 * This file is part of the bugloos/fault-tolerance-bundle project.
5
 * (c) Bugloos <https://bugloos.com/>
6
 * For the full copyright and license information, please view
7
 * the LICENSE file that was distributed with this source code.
8
 */
9
10
namespace Bugloos\FaultToleranceBundle\Factory;
11
12
use Bugloos\FaultToleranceBundle\CircuitBreaker\CircuitBreaker;
13
use Bugloos\FaultToleranceBundle\CircuitBreaker\ClosedCircuitBreaker;
14
use Bugloos\FaultToleranceBundle\CircuitBreaker\Storage\Storage;
15
16
/**
17
 * @author Mojtaba Gheytasi <[email protected]>
18
 */
19
class CircuitBreakerFactory
20
{
21
    private Storage $storage;
22
23
    protected array $circuitBreakersByCommand = [];
24
25
    public function __construct(Storage $storage)
26
    {
27
        $this->storage = $storage;
28
    }
29
30
    public function create(string $commandKey, array $circuitBreakerConfig)
31
    {
32
        if (! isset($this->circuitBreakersByCommand[$commandKey])) {
33
            if ($circuitBreakerConfig['enabled']) {
34
                $this->circuitBreakersByCommand[$commandKey] =
35
                    new CircuitBreaker($commandKey, $this->storage, $circuitBreakerConfig);
36
            } else {
37
                $this->circuitBreakersByCommand[$commandKey] = new ClosedCircuitBreaker();
38
            }
39
        }
40
41
        return $this->circuitBreakersByCommand[$commandKey];
42
    }
43
}
44