FormatMsisdnAction::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 18
ccs 0
cts 10
cp 0
rs 9.9666
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Bmatovu\Ussd\Actions;
4
5
/**
6
 * Usage:
7
 *
8
 * ```xml
9
 * <action name="format_msisdn" msisdn="0743876123" country_code="256" />
10
 * <response text="{{fmt_msisdn}}" /><!-- 256743876123 -->
11
 * ```
12
 */
13
class FormatMsisdnAction extends BaseAction
14
{
15
    public function process(?string $answer): void
16
    {
17
        $msisdn = $this->readAttr('msisdn', $this->store->get('msisdn'));
18
19
        if (!$msisdn) {
20
            return;
21
        }
22
23
        $msisdn = preg_replace('/[^\d]/', '', $msisdn);
24
25
        $country_code = $this->readAttr('country_code');
26
        if ($country_code) {
27
            $msisdn = preg_replace('/^0/', $country_code, $msisdn);
28
        }
29
30
        $prefix = $this->readAttr('prefix', 'fmt');
31
32
        $this->store->put("{$prefix}_msisdn", $msisdn);
33
    }
34
}
35