Africastalking::call()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 32
ccs 0
cts 20
cp 0
rs 9.7333
cc 2
nc 2
nop 2
crap 6
1
<?php
2
3
namespace Bmatovu\Ussd\Simulator;
4
5
use Bmatovu\Ussd\Contracts\Aggregator;
6
use Bmatovu\Ussd\Exceptions\FlowBreakException;
7
use GuzzleHttp\Client;
8
9
class Africastalking implements Aggregator
10
{
11
    public function call(string $uri, array $simulator): ?string
12
    {
13
        $params = [
14
            'sessionId' => $simulator['session_id'],
15
            'networkCode' => $simulator['network_code'],
16
            'phoneNumber' => $simulator['phone_number'],
17
            'text' => $simulator['answers'],
18
            'serviceCode' => $simulator['service_code'],
19
        ];
20
21
        // try {
22
        $response = (new Client())->request('POST', $uri, [
23
            'headers' => [
24
                'Accept' => 'text/plain',
25
            ],
26
            'form_params' => $params,
27
        ]);
28
29
        $body = (string) $response->getBody();
30
31
        $cmd = substr($body, 0, 3);
32
        $payload = substr($body, 4);
33
        // } catch (\Throwable $th) {
34
        //     $firstLine = preg_split('#\r?\n#', ltrim($th->getMessage()), 2)[0];
35
        //     throw new \Exception($firstLine);
36
        // }
37
38
        if ('END' === $cmd) {
39
            throw new FlowBreakException($payload);
40
        }
41
42
        return $payload;
43
    }
44
}
45