Completed
Push — master ( f8925d...515845 )
by antikainen
01:42
created

ZonerSmsGateway   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 67.44%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 123
ccs 29
cts 43
cp 0.6744
rs 10
c 4
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A httpClient() 0 4 2
A getCredits() 0 16 2
C sendMessage() 0 46 9
1
<?php
2
3
namespace NotificationChannels\ZonerSmsGateway;
4
5
use GuzzleHttp\Client as HttpClient;
6
use NotificationChannels\ZonerSmsGateway\Exceptions\ZonerSmsGatewayException;
7
8
class ZonerSmsGateway
9
{
10
    /** URL of the Zoner SMS-API service. */
11
    const ENDPOINT_URL = 'https://sms.zoner.fi/sms.php';
12
13
    /** @var HttpClient HTTP Client */
14
    protected $http;
15
16
    /** @var string|null Zoner SMS-API username. */
17
    protected $username = null;
18
19
    /** @var string|null Zoner SMS-API password. */
20
    protected $password = null;
21
22
    /** @var string|null Default sender number or text. */
23
    protected $sender = null;
24
25
    /**
26
     * @param string|null $username
27
     * @param string|null $password
28
     * @param string|null $sender sender number or name
29
     * @param HttpClient|null $httpClient
30
     */
31 11
    public function __construct($username, $password, $sender = null, HttpClient $httpClient = null)
32
    {
33 11
        $this->username = $username;
34 11
        $this->password = $password;
35
36 11
        $this->sender = $sender;
37 11
        $this->http = $httpClient;
38 11
    }
39
40
    /**
41
     * Gets the HttpClient.
42
     *
43
     * @return HttpClient
44
     */
45 10
    protected function httpClient()
46
    {
47 10
        return $this->http ?: $this->http = new HttpClient();
48
    }
49
50
    /**
51
     * Sends a message via the gateway.
52
     *
53
     * @param string $receiver phone number where to send (for example "35840123456")
54
     * @param string $message message to send (UTF-8, but this function converts it to ISO-8859-15)
55
     * @param string|null $sender sender phone number (for example "35840123456")
56
     * or string (max 11 chars, a-ZA-Z0-9)
57
     *
58
     * @return string tracking number
59
     *
60
     * @throws ZonerSmsGatewayException if sending failed.
61
     */
62 10
    public function sendMessage($receiver, $message, $sender = null)
63
    {
64 10
        if (empty($this->username)) {
65
            throw ZonerSmsGatewayException::usernameNotProvided();
66
        }
67
68 10
        if (empty($receiver)) {
69
            throw ZonerSmsGatewayException::receiverNotProvided();
70
        }
71
72 10
        if (empty($sender)) {
73 9
            if (empty($this->sender)) {
74
                throw ZonerSmsGatewayException::senderNotProvided();
75
            } else {
76 9
                $sender = $this->sender;
77
            }
78
        }
79
80 10
        if (empty($message)) {
81
            throw ZonerSmsGatewayException::emptyMessage();
82
        }
83
        $params = [
84 10
            'username' => $this->username,
85 10
            'password' => $this->password,
86 10
            'numberto' => $receiver,
87 10
            'numberfrom' => $sender,
88 10
            'message' => utf8_decode($message),
89
        ];
90
91 10
        $response = $this->httpClient()->post(self::ENDPOINT_URL, [
92 10
            'form_params' => $params,
93
        ]);
94 9
        if ($response->getStatusCode() === 200) {
95 9
            $body = $response->getBody();
96 9
            $statusAndCode = explode(' ', $body, 2);
97 9
            if ($statusAndCode[0] === 'OK') {
98 8
                return $statusAndCode[1];
99 1
            } elseif ($statusAndCode[0] === 'ERR') {
100 1
                throw ZonerSmsGatewayException::serviceRespondedWithAnError($statusAndCode[1]);
101
            } else {
102
                throw ZonerSmsGatewayException::unknownZonerResponse($statusAndCode);
103
            }
104
        } else {
105
            throw ZonerSmsGatewayException::unexpectedHttpStatus($response);
106
        }
107
    }
108
109
    /**
110
     * Returns the amount of credits left in the service, or whatever the service replies.
111
     *
112
     * @throws ZonerSmsGatewayException if request failed.
113
     */
114
    public function getCredits()
115
    {
116
        $params = [
117
            'username' => $this->username,
118
            'password' => $this->password,
119
        ];
120
121
        $response = $this->httpClient()->post(self::ENDPOINT_URL, [
122
            'form_params' => $params,
123
        ]);
124
        if ($response->getStatusCode() === 200) {
125
            return $response->getBody();
126
        } else {
127
            throw ZonerSmsGatewayException::unexpectedHttpStatus($response);
128
        }
129
    }
130
}
131