Passed
Push — master ( d8d104...a380f8 )
by Brian
02:35
created

FormatMoneyAction::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
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 \DOMNode $node;
10
    protected CacheContract $cache;
11
    protected string $prefix;
12
    protected int $ttl;
13
14
    protected float $amount;
15
    protected string $currency;
16
17 2
    public function __construct(\DOMNode $node, CacheContract $cache, string $prefix, ?int $ttl = null)
18
    {
19 2
        $this->node = $node;
20 2
        $this->cache = $cache;
21 2
        $this->prefix = $prefix;
22 2
        $this->ttl = $ttl;
23
    }
24
25 2
    public function handle(): ?string
26
    {
27 2
        $this->extractParameters($this->node);
28
29 1
        $formattedAmount = number_format($this->amount);
30
31 1
        $this->cache->put("{$this->prefix}_amount", "{$this->currency} {$formattedAmount}", $this->ttl);
32
33 1
        return '';
34
    }
35
36
    public function process(?string $answer): void
0 ignored issues
show
Unused Code introduced by
The parameter $answer is not used and could be removed. ( Ignorable by Annotation )

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

36
    public function process(/** @scrutinizer ignore-unused */ ?string $answer): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
    }
39
40 2
    protected function extractParameters(\DOMNode $node): void
41
    {
42 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

42
        $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...
43 2
            ?? $this->cache->get("{$this->prefix}_amount");
44
45 2
        if (! $amount) {
46 1
            throw new \Exception("Arg 'amount' is required.");
47
        }
48
49 1
        $this->amount = (float) $amount;
50
51 1
        $this->currency = $node->attributes->getNamedItem('currency')->nodeValue
52 1
            ?? $this->cache->get("{$this->prefix}_currency", 'UGX');
53
    }
54
}
55