FormatMoneyAction   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 2
eloc 12
c 2
b 0
f 0
dl 0
loc 22
ccs 0
cts 13
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 20 2
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