Passed
Push — master ( f2baa3...ad4c1e )
by Brian
12:59
created

Comviva   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 51
c 1
b 0
f 0
dl 0
loc 81
ccs 0
cts 54
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B buildXml() 0 32 6
B call() 0 45 6
1
<?php
2
3
namespace Bmatovu\Ussd\Simulator;
4
5
use Bmatovu\Ussd\Contracts\Aggregator;
6
use GuzzleHttp\Client;
7
use GuzzleHttp\Exception\RequestException;
8
use GuzzleHttp\Exception\TransferException;
9
10
/**
11
 * Comviva HTTP Pull Flares API
12
 * Should work for MTN and Airtel
13
 */
14
class Comviva implements Aggregator
15
{
16
    public function call(string $uri, array $simulator): ?string
17
    {
18
        try {
19
            $response = (new Client())->request('POST', $uri, [
20
                'headers' => [
21
                    'Accept' => 'text/xml',
22
                    'Content-Type' => 'text/xml; charset=UTF8',
23
                ],
24
                'body' => $this->buildXml([
25
                    'name' => 'request',
26
                    'attributes' => [
27
                        'type' => 'pull',
28
                    ],
29
                ], [
30
                    'sessionId' => $simulator['session_id'],
31
                    'transactionId' => null,
32
                    'msisdn' => $simulator['phone_number'],
33
                    'newRequest' => $simulator['new_session'] ? 1 : 0,
34
                    'flowState' => $simulator['new_session'] ? 'FD' : 'FE',
35
                    'subscriberInput' => $simulator['input'], // $simulator['new_session'] ? substr($simulator['input'], 4) : $simulator['input'],
36
                ]),
37
            ]);
38
39
            $body = (string) $response->getBody();
40
41
            $body = new \SimpleXMLElement($body);
42
43
            $flow = $body->freeflow->freeflowState->__toString();
44
45
            $applicationResponse = $body->applicationResponse->__toString();
46
47
            if ($flow == 'FB') {
48
                throw new \Exception($applicationResponse);
49
            }
50
        } catch (RequestException $ex) {
51
            $response = $ex->getResponse();
52
            $body = (string) $response->getBody();
53
            $message = $body ?? $response->getReasonPhrase();
54
55
            throw new \Exception(sprintf('%s . %s', $message, $response->getStatusCode()));
56
        } catch (TransferException $ex) {
57
            throw new \Exception(sprintf('%s . %s', $ex->getMessage(), $ex->getCode()));
58
        }
59
60
        return $applicationResponse;
61
    }
62
63
    protected function buildXml(string|array $root, array $elements): string
64
    {
65
        $domDoc = new \DOMDocument('1.0', 'UTF-8');
66
        $domDoc->xmlStandalone = true;
67
        $domDoc->preserveWhiteSpace = false;
68
        $domDoc->formatOutput = true;
69
70
        if (is_string($root)) {
0 ignored issues
show
introduced by
The condition is_string($root) is always false.
Loading history...
71
            $rootEl = $domDoc->createElement($root);
72
        } else {
73
            $rootEl = $domDoc->createElement($root['name']);
74
            foreach ($root['attributes'] ?? [] as $key => $value) {
75
                $rootEl->setAttribute($key, $value);
76
            }
77
        }
78
79
        foreach ($elements as $key => $value) {
80
            if (!is_array($value)) {
81
                $elem = $domDoc->createElement($key, $value);
82
            } else {
83
                $elem = $domDoc->createElement($key);
84
                foreach ($value as $key => $value) {
0 ignored issues
show
Comprehensibility Bug introduced by
$value is overwriting a variable from outer foreach loop.
Loading history...
Comprehensibility Bug introduced by
$key is overwriting a variable from outer foreach loop.
Loading history...
85
                    $subElem = $domDoc->createElement($key, $value);
86
                    $elem->appendChild($subElem);
87
                }
88
            }
89
            $rootEl->appendChild($elem);
90
        }
91
92
        $domDoc->appendChild($rootEl);
93
94
        return $domDoc->saveXML();
95
    }
96
}
97