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

NetgsmAvailableCredit::getCredit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 NetgsmAvailableCredit extends NetgsmApiClient
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $response;
16
17
    /**
18
     * @var array
19
     */
20
    protected $successCodes = [
21
        '00',
22
    ];
23
24
    protected $url = 'balance/list/get';
25
26
    /**
27
     * @var array
28
     */
29
    protected $errorCodes = [
30
        '30'  => NetgsmErrors::CREDENTIALS_INCORRECT,
31
        '40'  => NetgsmErrors::CREDENTIALS_INCORRECT,
32
        '100' => NetgsmErrors::SYSTEM_ERROR,
33
    ];
34
35
    /**
36
     * @return string
37
     * @throws NetgsmException
38
     */
39
    public function parseResponse(): ?string
40
    {
41
        $result = explode(' ', $this->response);
42
43
        if (empty($result[0])) {
44
            throw new NetgsmException(NetgsmErrors::NETGSM_GENERAL_ERROR);
45
        }
46
47
        $code = $result[0];
48
49
        if (! in_array($code, $this->successCodes)) {
50
            $message = $this->errorCodes[$code];
51
            throw new NetgsmException($message, $code);
52
        }
53
54
        return $result[1];
55
    }
56
57
    /**
58
     * @throws NetgsmErrors
59
     * @throws GuzzleException
60
     * @throws NetgsmException
61
     */
62
    public function getCredit(): ?string
63
    {
64
        $this->response = $this->callApi('GET', $this->url);
65
66
        return $this->parseResponse();
67
    }
68
}
69