Passed
Push — master ( 365cfc...8e997c )
by Mr
04:36
created

SatoshiMoneyParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 16
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 16
ccs 0
cts 10
cp 0
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 13 4
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\Service;
10
11
use Money\Currency;
12
use Money\Exception\ParserException;
13
use Money\Money;
14
use Money\MoneyParser;
15
16
final class SatoshiMoneyParser implements MoneyParser
17
{
18
    /** @param null|Currency $forceCurrency */
19
    public function parse($money, $forceCurrency = null)
20
    {
21
        if (is_string($money) === false) {
22
            throw new ParserException('Formatted raw money should be string, e.g. 100SAT');
23
        }
24
25
        if (!preg_match('/^(?<amount>-?\d+)\s?(?<currency>M?SAT)$/', $money, $matches)) {
26
            throw new ParserException('Value cannot be parsed to Satoshi.');
27
        }
28
29
        $currency = $forceCurrency ?: new Currency($matches['currency']);
30
31
        return new Money($matches['amount'], $currency);
32
    }
33
}
34