Passed
Push — master ( e39bb0...54e3cc )
by Dmitry
14:48
created

MoneyBuilder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A buildMoney() 0 14 5
A divide() 0 6 2
A checkFloat() 0 3 1
A calculatePriceMultiplier() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hiqdev\php\billing\price;
6
7
use Money\Currencies\ISOCurrencies;
8
use Money\Currency;
9
use Money\Money;
10
use Money\Parser\DecimalMoneyParser;
11
12
class MoneyBuilder
13
{
14
    public static function buildMoney(string $price, string $currency): ?Money
15
    {
16
        if (!is_numeric($price)) {
17
            throw new \InvalidArgumentException('price of the Money must be numeric');
18
        }
19
        if (!self::checkFloat($price)) {
20
            return new Money($price, new Currency($currency));
21
        }
22
        if (!is_int($price) && ($price * 100) == ((int) ($price * 100))) {
0 ignored issues
show
introduced by
The condition is_int($price) is always false.
Loading history...
23
            return (new DecimalMoneyParser(new ISOCurrencies()))->parse($price, new Currency($currency));
24
        }
25
        return new Money(
26
            (int) ($price * self::calculatePriceMultiplier($price)),
27
            new Currency($currency)
28
        );
29
    }
30
31
    public static function calculatePriceMultiplier(string $price): int
32
    {
33
        $price = self::divide($price);
34
        if (!is_array($price)) {
35
            return 1;
36
        }
37
        return (int) (1 . implode(array_fill(0, strlen($price[1]),0)));
38
    }
39
40
    private static function divide(string $number): array | string
41
    {
42
        if (self::checkFloat($number)) {
43
            return explode('.', $number);
44
        }
45
        return $number;
46
    }
47
48
    private static function checkFloat(string $number): bool
49
    {
50
        return is_float($number + 0);
51
    }
52
}
53