Test Failed
Pull Request — master (#5)
by
unknown
04:40
created

VodafoneClient::getUnread()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace NotificationChannels\Vodafone;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\GuzzleException;
7
use NotificationChannels\Vodafone\Exceptions\CouldNotReceiveNotification;
8
use NotificationChannels\Vodafone\Exceptions\CouldNotSendNotification;
9
use NotificationChannels\Vodafone\Exceptions\StandardNotification;
10
11
class VodafoneClient
12
{
13
    /**
14
     * @var string Vodafone's send API endpoint
15
     */
16
    protected string $sendEndpoint = 'https://www.smsertech.com/apisend';
17
18
    /**
19
     * @var string Vodafone's status API endpoint
20
     */
21
    protected string $statusEndpoint = 'https://www.smsertech.com/apistatus';
22
23
    /**
24
     * @var string Vodafone's receive API endpoint
25
     */
26
    protected string $receiveEndpoint = 'https://www.smsertech.com/apiget';
27
28
    /** @var string Vodafone SMS username */
29
    protected $username;
30
31
    /** @var string Vodafone SMS password */
32
    protected $password;
33
34
    /**
35
     * VodafoneClient constructor.
36
     *
37
     * @param $username
38
     * @param $password
39
     */
40
    public function __construct()
41
    {
42
        $this->username = config('services.vodafone.username');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $this->username = /** @scrutinizer ignore-call */ config('services.vodafone.username');
Loading history...
43
        $this->password = config('services.vodafone.password');
44
45
        return $this;
46
    }
47
48
    /**
49
     *  VodafoneClient send method
50
     *
51
     * @param $from
52
     * @param $to
53
     * @param $message
54
     *
55
     * @return mixed Vodafone API result
56
     * @throws CouldNotSendNotification
57
     * @throws StandardNotification
58
     * @throws GuzzleException
59
     */
60
    public function send($from, $to, $message)
61
    {
62
        $client = new Client();
63
        $res = $client->post($this->sendEndpoint, [
64
            'form_params' => [
65
                'username'  => $this->username,
66
                'password'  => $this->password,
67
                'to'        => $to,
68
                'message'   => $message,
69
                'from'      => $from,
70
                'format'    => 'json',
71
                'flash'     => 0,
72
            ],
73
        ]);
74
75
        if (!$res) throw CouldNotSendNotification::serviceUnknownResponse();
0 ignored issues
show
introduced by
$res is of type Psr\Http\Message\ResponseInterface, thus it always evaluated to true.
Loading history...
76
77
        $body = $this->parseResponse($res);
78
79
        if ($body->status === 'ERROR') {
80
            throw StandardNotification::serviceRespondedWithAnError($body);
81
        }
82
83
        return $body;
84
    }
85
86
    /**
87
     * VodafoneClient receive method
88
     *
89
     * @return mixed Vodafone API result
90
     * @throws CouldNotReceiveNotification
91
     * @throws StandardNotification
92
     * @throws GuzzleException
93
     */
94
    public function receive()
95
    {
96
        $client = new Client();
97
        $res = $client->post($this->receiveEndpoint, [
98
            'form_params' => [
99
                'username'  => $this->username,
100
                'password'  => $this->password,
101
                'format'    => 'json',
102
            ],
103
        ]);
104
105
        if (!$res) throw CouldNotReceiveNotification::serviceUnknownResponse();
0 ignored issues
show
introduced by
$res is of type Psr\Http\Message\ResponseInterface, thus it always evaluated to true.
Loading history...
106
107
        $body = $this->parseResponse($res);
108
109
        if ($body->status === 'ERROR') {
110
            if($body->code === 201) throw CouldNotReceiveNotification::noNewMessages();
111
            throw StandardNotification::serviceRespondedWithAnError($body);
112
        }
113
114
        return $body;
115
    }
116
117
    /**
118
     * @return mixed|null
119
     * @throws CouldNotReceiveNotification
120
     * @throws GuzzleException
121
     * @throws StandardNotification
122
     */
123
    public static function getUnread()
124
    {
125
        return (new self())->receive();
126
    }
127
128
    /**
129
     * @param $res
130
     * @return mixed|null
131
     */
132
    public function parseResponse($res)
133
    {
134
        return json_decode($res->getBody()->getContents())[0] ?? null;
135
    }
136
}
137