1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Pavel |
5
|
|
|
* Date: 2015-05-09 |
6
|
|
|
* Time: 20:29 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace ScayTrase\WebSMS\Connection; |
10
|
|
|
|
11
|
|
|
use ScayTrase\WebSMS\Driver\DriverInterface; |
12
|
|
|
use ScayTrase\WebSMS\Driver\FormDriver; |
13
|
|
|
use ScayTrase\WebSMS\Exception\CommunicationException; |
14
|
|
|
use ScayTrase\WebSMS\Exception\DeliveryException; |
15
|
|
|
use ScayTrase\WebSMS\Message\MessageInterface; |
16
|
|
|
|
17
|
|
|
abstract class AbstractWebSMSConnection implements ConnectionInterface, WebSmsApiParams |
18
|
|
|
{ |
19
|
|
|
const TEST_DISABLED = 0; |
20
|
|
|
const TEST_ENABLED = 1; |
21
|
|
|
const TEST_SPECIAL = -1; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
protected $username; |
25
|
|
|
/** @var string */ |
26
|
|
|
protected $password; |
27
|
|
|
/** @var int */ |
28
|
|
|
protected $test = self::TEST_DISABLED; |
29
|
|
|
|
30
|
|
|
/** @var float */ |
31
|
|
|
protected $balance; |
32
|
|
|
|
33
|
|
|
/** @var DriverInterface */ |
34
|
|
|
protected $driver; |
35
|
|
|
/** @var FormDriver */ |
36
|
|
|
protected $balanceDriver; |
37
|
|
|
/** @var array */ |
38
|
|
|
private $lastStatus; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return float |
42
|
|
|
*/ |
43
|
|
|
public function getBalance() |
44
|
|
|
{ |
45
|
|
|
return $this->balance; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param DriverInterface $driver |
50
|
|
|
* @param $username |
51
|
|
|
* @param $password |
52
|
|
|
* @param int $test |
53
|
|
|
*/ |
54
|
|
|
public function __construct(DriverInterface $driver, $username, $password, $test = self::TEST_DISABLED) |
55
|
|
|
{ |
56
|
|
|
$this->driver = $driver; |
57
|
|
|
$this->username = $username; |
58
|
|
|
$this->password = $password; |
59
|
|
|
$this->test = $test; |
60
|
|
|
$this->balanceDriver = new FormDriver(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param MessageInterface $message |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
* @throws DeliveryException |
68
|
|
|
*/ |
69
|
|
|
protected function doSendRequest(MessageInterface $message) |
70
|
|
|
{ |
71
|
|
|
$status = $this->driver->doSendRequest( |
72
|
|
|
array( |
73
|
|
|
static::PARAM_Username => $this->username, |
74
|
|
|
static::PARAM_Password => $this->password, |
75
|
|
|
static::PARAM_Test => $this->test, |
76
|
|
|
static::PARAM_Recipients => $message->getRecipient(), |
77
|
|
|
static::PARAM_Message => $message->getMessage(), |
78
|
|
|
) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
if ($status[DriverInterface::NORMALIZED_CODE] !== DriverInterface::STATUS_OK) { |
82
|
|
|
throw new DeliveryException( |
83
|
|
|
$message, |
84
|
|
|
$status[DriverInterface::NORMALIZED_MESSAGE], |
85
|
|
|
$status[DriverInterface::NORMALIZED_CODE] |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->lastStatus = $status; |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getLastStatus() |
98
|
|
|
{ |
99
|
|
|
return $this->lastStatus; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool Verifies connection properties |
104
|
|
|
* @throws CommunicationException |
105
|
|
|
*/ |
106
|
|
|
public function verify() |
107
|
|
|
{ |
108
|
|
|
$this->balance = (float)$this->balanceDriver->doBalanceRequest( |
109
|
|
|
array( |
110
|
|
|
static::PARAM_Username => $this->username, |
111
|
|
|
static::PARAM_Password => $this->password, |
112
|
|
|
static::PARAM_Test => $this->test, |
113
|
|
|
static::PARAM_ResponseFormat => static::FORMAT_Text, |
114
|
|
|
) |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
return $this->balance > 0; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|