Total Complexity | 6 |
Total Lines | 37 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
12 | class CalculatorFactory |
||
13 | { |
||
14 | /** @var array */ |
||
15 | private static $calculators = [ |
||
16 | BcMathCalculator::class, |
||
17 | ]; |
||
18 | |||
19 | /** @var null|CalculatorInterface */ |
||
20 | private static $calculator; |
||
21 | |||
22 | 145 | public static function getCalculator(): CalculatorInterface |
|
23 | { |
||
24 | 145 | if (null !== self::$calculator) { |
|
25 | 143 | return self::$calculator; |
|
26 | } |
||
27 | 2 | foreach (self::$calculators as $calculator) { |
|
28 | /** @var CalculatorInterface $calculator */ |
||
29 | 1 | if ($calculator::supported()) { |
|
30 | return |
||
31 | 1 | self::$calculator = new $calculator(); |
|
32 | } |
||
33 | } |
||
34 | |||
35 | 1 | throw new \RuntimeException('Cannot find calculator for money calculations.'); |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * @param string $calculator |
||
40 | * @return int |
||
41 | */ |
||
42 | 2 | public static function registerCalculator($calculator): int |
|
49 | } |
||
50 | } |
||
51 |