Completed
Pull Request — master (#7)
by
unknown
01:16
created

NetgsmPackages   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 2
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parseResponse() 0 22 3
A getPackages() 0 6 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
    protected $url = 'balance/list/get';
18
19
    /**
20
     * @var array
21
     */
22
    protected $errorCodes = [
23
        '30'  => NetgsmErrors::MESSAGE_TOO_LONG,
24
        '40'  => NetgsmErrors::CREDENTIALS_INCORRECT,
25
        '100' => NetgsmErrors::SYSTEM_ERROR,
26
    ];
27
28
    /**
29
     * @return array
30
     * @throws NetgsmException
31
     */
32
    public function parseResponse(): array
33
    {
34
        $availablePackages = [];
35
36
        if (array_key_exists($this->response, $this->errorCodes)) {
37
            $message = $this->errorCodes[$this->response];
38
            throw new NetgsmException($message, $this->response);
39
        }
40
41
        $rows = array_filter(explode('<BR>', $this->response));
42
        foreach ($rows as $row) {
43
            $columns = array_filter(explode('|', $row));
44
            $columns = array_map('trim', $columns);
45
            $availablePackages[] = [
46
                'amount'      => (int) $columns[0],
47
                'amountType'  => $columns[1] ?? null,
48
                'packageType' => $columns[2] ?? null,
49
            ];
50
        }
51
52
        return $availablePackages;
53
    }
54
55
    /**
56
     * @return array
57
     * @throws GuzzleException
58
     * @throws NetgsmException
59
     */
60
    public function getPackages(): array
61
    {
62
        $this->response = $this->callApi('GET', $this->url, ['tip' => 1]);
63
64
        return $this->parseResponse();
65
    }
66
}
67