Test Failed
Pull Request — master (#257)
by Бабичев
26:04
created

BrickMath::scale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Bavix\Wallet\Simple;
4
5
use Bavix\Wallet\Interfaces\Mathable;
6
use Brick\Math\BigDecimal;
7
use Brick\Math\RoundingMode;
8
9
/**
10
 * Class BrickMath.
11
 */
12
class BrickMath implements Mathable
13
{
14
    /**
15
     * @var int
16 1
     */
17
    protected $scale;
18 1
19 1
    /**
20 1
     * {@inheritdoc}
21
     */
22
    public function add($first, $second, ?int $scale = null): string
23
    {
24
        return (string) BigDecimal::of($first)
25
            ->plus(BigDecimal::of($second))
26 1
            ->toScale($this->scale($scale), RoundingMode::DOWN);
27
    }
28 1
29 1
    /**
30 1
     * {@inheritdoc}
31
     */
32
    public function sub($first, $second, ?int $scale = null): string
33
    {
34
        return (string) BigDecimal::of($first)
35
            ->minus(BigDecimal::of($second))
36 1
            ->toScale($this->scale($scale), RoundingMode::DOWN);
37
    }
38 1
39 1
    /**
40
     * {@inheritdoc}
41
     */
42
    public function div($first, $second, ?int $scale = null): string
43
    {
44
        return (string) BigDecimal::of($first)
45 1
            ->dividedBy(BigDecimal::of($second), $this->scale($scale), RoundingMode::DOWN);
46
    }
47 1
48 1
    /**
49 1
     * {@inheritdoc}
50
     */
51
    public function mul($first, $second, ?int $scale = null): string
52
    {
53
        return (string) BigDecimal::of($first)
54
            ->multipliedBy(BigDecimal::of($second))
55 1
            ->toScale($this->scale($scale), RoundingMode::DOWN);
56
    }
57 1
58 1
    /**
59 1
     * {@inheritdoc}
60
     */
61
    public function pow($first, $second, ?int $scale = null): string
62
    {
63
        return (string) BigDecimal::of($first)
64
            ->power($second)
65 1
            ->toScale($this->scale($scale), RoundingMode::DOWN);
66
    }
67 1
68 1
    /**
69
     * {@inheritdoc}
70
     */
71
    public function ceil($number): string
72
    {
73
        return (string) BigDecimal::of($number)
74 1
            ->dividedBy(BigDecimal::one(), 0, RoundingMode::CEILING);
75
    }
76 1
77 1
    /**
78
     * {@inheritdoc}
79
     */
80
    public function floor($number): string
81
    {
82
        return (string) BigDecimal::of($number)
83 1
            ->dividedBy(BigDecimal::one(), 0, RoundingMode::FLOOR);
84
    }
85 1
86 1
    /**
87
     * {@inheritdoc}
88
     */
89
    public function round($number, int $precision = 0): string
90
    {
91
        return (string) BigDecimal::of($number)
92
            ->dividedBy(BigDecimal::one(), $precision, RoundingMode::HALF_UP);
93 1
    }
94
95
    /**
96 1
     * @param float|int|string $number
97 1
     * @return string
98 1
     */
99
    public function abs($number): string
100
    {
101
        return (string) BigDecimal::of($number)->abs();
102
    }
103
104
    /**
105 6
     * {@inheritdoc}
106
     */
107 6
    public function compare($first, $second): int
108
    {
109
        return BigDecimal::of($first)->compareTo(BigDecimal::of($second));
110
    }
111
112
    /**
113
     * @param int|null $scale
114
     * @return int
115
     */
116
    protected function scale(?int $scale = null): int
117
    {
118
        if ($this->scale === null) {
119
            $this->scale = (int) config('wallet.math.scale', 64);
120
        }
121
122
        return $scale ?? $this->scale;
123
    }
124
}
125