Completed
Push — master ( 2abedd...d4037f )
by antikainen
03:33
created

ZonerSmsGateway   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 62.79%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A httpClient() 0 4 2
C sendMessage() 0 46 9
A getCredits() 0 16 2
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 10
    public function __construct($username, $password, $sender = null, HttpClient $httpClient = null)
32
    {
33 10
        $this->username = $username;
34 10
        $this->password = $password;
35
36 10
        $this->sender = $sender;
37 10
        $this->http = $httpClient;
38 10
    }
39
40
    /**
41
     * Gets the HttpClient.
42
     *
43
     * @return HttpClient
44
     */
45 9
    protected function httpClient()
46
    {
47 9
        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 9
    public function sendMessage($receiver, $message, $sender = null)
63
    {
64 9
        if (empty($this->username)) {
65
            throw ZonerSmsGatewayException::usernameNotProvided();
66
        }
67
68 9
        if (empty($receiver)) {
69
            throw ZonerSmsGatewayException::receiverNotProvided();
70
        }
71
72 9
        if (empty($sender)) {
73 8
            if (empty($this->sender)) {
74
                throw ZonerSmsGatewayException::senderNotProvided();
75
            } else {
76 8
                $sender = $this->sender;
77
            }
78
        }
79
80 9
        if (empty($message)) {
81
            throw ZonerSmsGatewayException::emptyMessage();
82
        }
83
	    $params = [
84 9
            'username' => $this->username,
85 9
            'password' => $this->password,
86 9
            'numberto' => $receiver,
87 9
            'numberfrom' => $sender,
88 9
            'message' => utf8_decode($message),
89
        ];
90
91 9
        $response = $this->httpClient()->post( self::ENDPOINT_URL, [
92 9
            'form_params' => $params,
93
        ]);
94 8
        if ($response->getStatusCode() === 200) {
95 8
            $body = $response->getBody();
96 8
            $statusAndCode = explode(' ', $body, 2);
97 8
            if ($statusAndCode[0] === 'OK') {
98 8
                return $statusAndCode[1];
99
            } elseif ($statusAndCode[0] === 'ERR') {
100
                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