MathAdapterFactory::wrapAdapter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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