SatoshiMoneyParser::parse()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
ccs 0
cts 7
cp 0
crap 20
rs 10
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
    /**
19
     * @param string $money
20
     * @param null|Currency $forceCurrency
21
     */
22
    public function parse($money, $forceCurrency = null)
23
    {
24
        if (is_string($money) === false) {
0 ignored issues
show
introduced by
The condition is_string($money) === false is always false.
Loading history...
25
            throw new ParserException('Formatted raw money should be string, e.g. 100SAT');
26
        }
27
28
        if (!preg_match('/^(?<amount>-?\d+)\s?(?<currency>M?SAT)$/i', $money, $matches)) {
29
            throw new ParserException('Value cannot be parsed to Satoshi.');
30
        }
31
32
        $currency = $forceCurrency ?: new Currency(strtoupper($matches['currency']));
33
34
        return new Money($matches['amount'], $currency);
35
    }
36
}
37