FormatMoneyAction::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 20
ccs 0
cts 13
cp 0
rs 9.9
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Bmatovu\Ussd\Actions;
4
5
/**
6
 * Usage:
7
 *
8
 * ```xml
9
 * <action name="format_money" amount="15600.5075" currency="USD" decimals="2" />
10
 * <response text="{{fmt_amount}}" /><!-- USD 15,600.51 -->
11
 * ```
12
 */
13
class FormatMoneyAction extends BaseAction
14
{
15
    public function process(?string $answer): void
16
    {
17
        $amount = $this->readAttr('amount', $this->store->get('amount'));
18
19
        if (!$amount) {
20
            return;
21
        }
22
23
        $money = number_format(
24
            (float) $amount,
25
            (int) $this->readAttr('decimals', $this->store->get('decimals', 0)),
26
            $this->readAttr('decimal_separator', $this->store->get('decimal_separator', '.')),
27
            $this->readAttr('thousands_separator', $this->store->get('thousands_separator', ','))
28
        );
29
30
        $currency = $this->readAttr('currency', 'USD');
31
32
        $prefix = $this->readAttr('prefix', 'fmt');
33
34
        $this->store->put("{$prefix}_amount", "{$currency} {$money}");
35
    }
36
}
37