Passed
Push — master ( 6e9a75...0966bf )
by Jordan
08:50
created

ComplexNumbers::make()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 12
nc 7
nop 2
dl 0
loc 25
ccs 0
cts 19
cp 0
crap 90
rs 8.0555
c 0
b 0
f 0
1
<?php
2
3
namespace Samsara\Fermat;
4
5
use Samsara\Exceptions\UsageError\IntegrityConstraint;
6
use Samsara\Fermat\Types\Base\Interfaces\Groups\NumberCollectionInterface;
7
use Samsara\Fermat\Types\Base\Interfaces\Numbers\SimpleNumberInterface;
8
use Samsara\Fermat\Values\ImmutableComplexNumber;
9
use Samsara\Fermat\Values\MutableComplexNumber;
10
11
class ComplexNumbers
12
{
13
14
    public const IMMUTABLE_COMPLEX = ImmutableComplexNumber::class;
15
    public const MUTABLE_COMPLEX = MutableComplexNumber::class;
16
17
    /**
18
     * @param $type
19
     * @param $value
20
     *
21
     * @return Types\ComplexNumber
22
     * @throws IntegrityConstraint
23
     */
24
    public static function make($type, $value)
25
    {
26
27
        if (is_string($value)) {
28
            if ($type === self::MUTABLE_COMPLEX) {
29
                return MutableComplexNumber::makeFromString($value);
30
            }
31
32
            return ImmutableComplexNumber::makeFromString($value);
33
        }
34
35
        if ($value instanceof NumberCollectionInterface && $value->count() === 2) {
36
            if ($type === self::MUTABLE_COMPLEX) {
37
                return new MutableComplexNumber($value->get(0), $value->get(1));
38
            }
39
40
            return new ImmutableComplexNumber($value->get(0), $value->get(1));
41
        }
42
43
        if (is_array($value) && count($value) === 2) {
44
            if ($type === self::MUTABLE_COMPLEX) {
45
                return new MutableComplexNumber($value[0], $value[1]);
46
            }
47
48
            return new ImmutableComplexNumber($value[0], $value[1]);
49
        }
50
51
    }
52
53
}