CircuitBreakerFactory::create()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 12
rs 10
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