1 <?php
2 namespace DevStrefa\Esemeser;
3
4 5 6 7 8 9 10 11 12 13
14
15 class Esemeser
16 {
17 18 19
20 const SEND_URL='https://esemeser.pl/0api/wyslij.php';
21
22 23 24
25 const CHECK_URL='https://esemeser.pl/0api/sprawdz.php';
26
27
28 private $account;
29 private $login;
30 private $password;
31 private $requestMethod;
32
33
34 35 36 37 38 39 40
41 public function __construct($account=null,$login=null,$password=null)
42 {
43 if ($account != null){
44 $this->setAccount($account);
45 }
46
47 if ($login!=null){
48 $this->setLogin($login);
49 }
50
51 if ($password!=null){
52 $this->setPassword($password);
53 }
54
55
56 $this->requestMethod='fgc';
57
58 }
59
60 61 62 63 64 65
66 public function setRequestMethod($requestMethod){
67 if ($requestMethod != 'fgc' && $requestMethod !='curl'){
68 throw new \InvalidArgumentException('Invalid Request Method');
69 }
70 }
71
72 73 74 75
76 private function makeRequest($url)
77 {
78 if ($this->requestMethod == 'fgc'){
79
80 return file_get_contents($url);
81
82 } elseif ($this->requestMethod == 'curl'){
83
84 $curl = curl_init();
85 curl_setopt_array($curl, array(
86 CURLOPT_RETURNTRANSFER => 1,
87 CURLOPT_URL => $url
88 ));
89
90 return curl_exec($curl);
91 }
92 }
93
94 95 96 97 98 99 100 101
102 public function checkBalance($messageType=null)
103 {
104 $query=array(
105 'konto'=>$this->getAccount(),
106 'login'=>$this->getLogin(),
107 'haslo'=>$this->getPassword()
108 );
109
110 if ($messageType!==null){
111 if (MessageType::typeIsValid($messageType)) {
112 $query['rodzaj'] =$messageType;
113 } else{
114 throw new \InvalidArgumentException('Incorrect Message Type');
115 }
116 }
117
118 $queryString=http_build_query($query);
119
120 $result=$this->makeRequest(self::CHECK_URL.'?'.$queryString);
121
122 if ($result < 0){
123
124 switch ($result){
125
126 case '-1':
127 throw new \Exception('Incorrect Account name');
128 break;
129 case '-2':
130 throw new \Exception('Incorrect login or password');
131 break;
132 default:
133 throw new \Exception('Unknown Error');
134 break;
135
136 }
137
138 } else {
139
140 return $result;
141
142 }
143
144 }
145
146 147 148 149 150 151 152
153 public function send(Message $message)
154 {
155 $query=array(
156 'konto'=>$this->getAccount(),
157 'login'=>$this->getLogin(),
158 'haslo'=>$this->getPassword(),
159 'nazwa'=>$message->getClientName(),
160 'telefon'=>$message->getPhoneNumber(),
161 'tresc'=>$message->getMessage()
162 );
163
164 if ($message->getMessageType() !== null){
165 $query['rodzaj']=$message->getMessageType();
166 }
167
168 $queryString=http_build_query($query);
169
170 $result=$this->makeRequest(self::SEND_URL.'?'.$queryString);
171
172 if ($result !='OK'){
173
174 switch ($result){
175
176 case '-1':
177 throw new \Exception('Incorrect Account name');
178 break;
179 case '-2':
180 throw new \Exception('Incorrect login or password');
181 break;
182 case '-3':
183 throw new \Exception('Incorrect phone number');
184 break;
185 case 'NIE':
186 throw new \Exception('Message wasn\'t sent');
187 break;
188 default:
189 throw new \Exception('Unknown Error');
190 break;
191
192 }
193
194 } else {
195
196 return true;
197
198 }
199
200 }
201
202 203 204 205
206 public function getAccount()
207 {
208 return $this->account;
209 }
210
211 212 213 214 215
216 public function setAccount($account)
217 {
218 $this->account = $account;
219 return $this;
220 }
221
222 223 224 225
226 public function getLogin()
227 {
228 return $this->login;
229 }
230
231 232 233 234 235
236 public function setLogin($login)
237 {
238 $this->login = $login;
239 return $this;
240 }
241
242 243 244 245
246 public function getPassword()
247 {
248 return $this->password;
249 }
250
251 252 253 254 255
256 public function setPassword($password)
257 {
258 $this->password = $password;
259 return $this;
260 }
261
262
263
264
265
266 }