NetgsmPackages   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 4
eloc 22
c 2
b 0
f 1
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseResponse() 0 21 3
A getPackages() 0 5 1
1
<?php
2
3
namespace TarfinLabs\Netgsm\Balance;
4
5
use GuzzleHttp\Exception\GuzzleException;
6
use TarfinLabs\Netgsm\Exceptions\NetgsmException;
7
use TarfinLabs\Netgsm\NetgsmApiClient;
8
use TarfinLabs\Netgsm\NetgsmErrors;
9
10
class NetgsmPackages extends NetgsmApiClient
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $response;
16
17
    /**
18
     * @var string
19
     */
20
    protected $url = 'balance/list/get';
21
22
    /**
23
     * @var array
24
     */
25
    protected $errorCodes = [
26
        '30'  => NetgsmErrors::CREDENTIALS_INCORRECT,
27
        '40'  => NetgsmErrors::NO_RECORD,
28
        '100' => NetgsmErrors::SYSTEM_ERROR,
29
    ];
30
31
    /**
32
     * handles the response and return the package list as an array.
33
     *
34
     * @return array
35
     * @throws NetgsmException
36
     */
37
    public function parseResponse(): array
38
    {
39
        $availablePackages = [];
40
41
        if (array_key_exists($this->response, $this->errorCodes)) {
42
            $message = $this->errorCodes[$this->response] ?? NetgsmErrors::SYSTEM_ERROR;
43
            throw new NetgsmException($message, $this->response);
44
        }
45
46
        $rows = array_filter(explode('<BR>', $this->response));
47
        foreach ($rows as $row) {
48
            $columns = array_filter(explode('|', $row));
49
            $columns = array_map('trim', $columns);
50
            $availablePackages[] = [
51
                'amount'      => (int) $columns[0],
52
                'amountType'  => $columns[1] ?? null,
53
                'packageType' => $columns[2] ?? null,
54
            ];
55
        }
56
57
        return $availablePackages;
58
    }
59
60
    /**
61
     * returns the packages list for associated netgsm account.
62
     *
63
     * @return array
64
     * @throws GuzzleException
65
     * @throws NetgsmException
66
     */
67
    public function getPackages(): array
68
    {
69
        $this->response = $this->callApi('GET', $this->url, ['tip' => 1]);
70
71
        return $this->parseResponse();
72
    }
73
}
74