MathAdapterFactory::forceAdapter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Mdanter\Ecc\Math;
4
5
class MathAdapterFactory
6
{
7
    /**
8
     * @var GmpMathInterface
9
     */
10
    private static $forcedAdapter = null;
11
12
    /**
13
     * @param GmpMathInterface $adapter
14
     */
15
    public static function forceAdapter(GmpMathInterface $adapter = null)
16
    {
17
        self::$forcedAdapter = $adapter;
18
    }
19
20
    /**
21
     * @param bool $debug
22
     * @return DebugDecorator|GmpMathInterface|null
23
     */
24
    public static function getAdapter(bool $debug = false): GmpMathInterface
25
    {
26
        if (self::$forcedAdapter !== null) {
27
            return self::$forcedAdapter;
28
        }
29
30
        $adapter = new GmpMath();
31
32
        return self::wrapAdapter($adapter, $debug);
33
    }
34
35
    /**
36
     * @param GmpMathInterface $adapter
37
     * @param bool $debug
38
     * @return DebugDecorator|GmpMathInterface
39
     */
40
    private static function wrapAdapter(GmpMathInterface $adapter, bool $debug): GmpMathInterface
41
    {
42
        if ($debug === true) {
43
            return new DebugDecorator($adapter);
44
        }
45
46
        return $adapter;
47
    }
48
}
49