MathAdapterFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
c 3
b 0
f 0
dl 0
loc 42
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A forceAdapter() 0 3 1
A getAdapter() 0 9 2
A wrapAdapter() 0 7 2
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