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

FormatMsisdnAction::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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