Passed
Push — master ( 64f30d...d93186 )
by Brian
02:27
created

FormatMoneyAction::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Bmatovu\Ussd\Actions;
4
5
use Illuminate\Contracts\Cache\Repository as CacheContract;
6
7
class FormatMoneyAction
8
{
9
    protected CacheContract $cache;
10
    protected string $prefix;
11
    protected int $ttl;
12
13
    protected float $amount;
14
    protected string $currency;
15
16 2
    public function __construct(CacheContract $cache, string $prefix, ?int $ttl = null)
17
    {
18 2
        $this->cache = $cache;
19 2
        $this->prefix = $prefix;
20 2
        $this->ttl = $ttl;
21
    }
22
23 2
    public function __invoke(\DOMNode $node): void
24
    {
25 2
        $this->extractParameters($node);
26
27 1
        $formattedAmount = number_format($this->amount);
28
29 1
        $this->cache->put("{$this->prefix}_amount", "{$this->currency} {$formattedAmount}", $this->ttl);
30
    }
31
32 2
    protected function extractParameters(\DOMNode $node): void
33
    {
34 2
        $amount = $node->attributes->getNamedItem('amount')->nodeValue
0 ignored issues
show
Bug introduced by
The method getNamedItem() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        $amount = $node->attributes->/** @scrutinizer ignore-call */ getNamedItem('amount')->nodeValue

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35 2
            ?? $this->cache->get("{$this->prefix}_amount");
36
37 2
        if (! $amount) {
38 1
            throw new \Exception("Arg 'amount' is required.");
39
        }
40
41 1
        $this->amount = (float) $amount;
42
43 1
        $this->currency = $node->attributes->getNamedItem('currency')->nodeValue
44 1
            ?? $this->cache->get("{$this->prefix}_currency", 'UGX');
45
    }
46
}
47