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

SatoshiMoneyParser::parse()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 20
rs 10
c 0
b 0
f 0
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