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

FormatMsisdnAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 1
b 0
f 0
dl 0
loc 18
ccs 0
cts 9
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 16 3
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