Passed
Push — master ( 42d38a...1ea97a )
by Brian
02:43
created

FormatMoneyAction::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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