Bitcoin   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 8
c 2
b 0
f 0
dl 0
loc 21
ccs 8
cts 9
cp 0.8889
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNative() 0 12 3
A zero() 0 3 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the ngutech/bitcoin-interop project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace NGUtech\Bitcoin\ValueObject;
10
11
use Daikon\Interop\Assertion;
12
use Daikon\Interop\InvalidArgumentException;
13
use Daikon\Money\ValueObject\Money;
14
use NGUtech\Bitcoin\Service\SatoshiCurrencies;
15
16
final class Bitcoin extends Money
17
{
18
    /** @param null|string $value */
19 7
    public static function fromNative($value): self
20
    {
21 7
        Assertion::nullOrString($value, 'Must be a string.');
22 7
        if ($value === null) {
23
            return new self;
24
        }
25
26 7
        if (!preg_match('/^(?<amount>-?\d+)\s?(?<currency>M?SAT|BTC|XBT)$/i', $value, $matches)) {
27 2
            throw new InvalidArgumentException('Invalid amount.');
28
        }
29
30 7
        return new self(self::asBaseMoney($matches['amount'], strtoupper($matches['currency'])));
31
    }
32
33
    /** @param null|string $currency */
34 1
    public static function zero($currency = null): self
35
    {
36 1
        return self::fromNative('0'.($currency ?? SatoshiCurrencies::MSAT));
37
    }
38
}
39