1 | <?php |
||
12 | class Client |
||
13 | { |
||
14 | /** |
||
15 | * $apiEndpoint. |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | public $apiEndpoint = 'http://api.every8d.com/API21/HTTP'; |
||
20 | |||
21 | /** |
||
22 | * $credit. |
||
23 | * |
||
24 | * @var float |
||
25 | */ |
||
26 | public $credit = null; |
||
27 | |||
28 | /** |
||
29 | * $userId. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $userId; |
||
34 | |||
35 | /** |
||
36 | * $password. |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $password; |
||
41 | |||
42 | /** |
||
43 | * $httpClient. |
||
44 | * |
||
45 | * @var \Http\Client\HttpClient |
||
46 | */ |
||
47 | protected $httpClient; |
||
48 | |||
49 | /** |
||
50 | * $messageFactory. |
||
51 | * |
||
52 | * @var \Http\Message\MessageFactory |
||
53 | */ |
||
54 | protected $messageFactory; |
||
55 | |||
56 | /** |
||
57 | * __construct. |
||
58 | * |
||
59 | * @param string $userId |
||
60 | * @param string $password |
||
61 | * @param \Http\Client\HttpClient $httpClient |
||
62 | * @param \Http\Message\MessageFactory $messageFactory |
||
63 | */ |
||
64 | 4 | public function __construct($userId, $password, HttpClient $httpClient = null, MessageFactory $messageFactory = null) |
|
71 | |||
72 | /** |
||
73 | * setCredit. |
||
74 | * |
||
75 | * @param string $credit |
||
76 | */ |
||
77 | 2 | protected function setCredit($credit) |
|
83 | |||
84 | /** |
||
85 | * credit. |
||
86 | * |
||
87 | * @return float |
||
88 | */ |
||
89 | 3 | public function credit() |
|
90 | { |
||
91 | 3 | if (is_null($this->credit) === false) { |
|
92 | 1 | return $this->credit; |
|
93 | } |
||
94 | |||
95 | 2 | $response = $this->doRequest('getCredit.ashx', [ |
|
96 | 2 | 'UID' => $this->userId, |
|
97 | 2 | 'PWD' => $this->password, |
|
98 | 2 | ]); |
|
99 | |||
100 | 2 | if ($this->isValidResponse($response) === false) { |
|
101 | 1 | throw new DomainException($response, 500); |
|
102 | } |
||
103 | |||
104 | 1 | return $this->setCredit($response) |
|
105 | 1 | ->credit; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * send. |
||
110 | * |
||
111 | * @param array $params |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | 2 | public function send($params) |
|
116 | { |
||
117 | 2 | $response = $this->doRequest('sendSMS.ashx', array_filter([ |
|
118 | 2 | 'UID' => $this->userId, |
|
119 | 2 | 'PWD' => $this->password, |
|
120 | 2 | 'SB' => isset($params['subject']) ? $params['subject'] : null, |
|
121 | 2 | 'MSG' => $params['text'], |
|
122 | 2 | 'DEST' => $params['to'], |
|
123 | 2 | 'ST' => isset($params['ST']) ? Carbon::parse($params['ST'])->format('YmdHis') : null, |
|
124 | 2 | ])); |
|
125 | |||
126 | 2 | if ($this->isValidResponse($response) === false) { |
|
127 | 1 | throw new DomainException($response, 500); |
|
128 | } |
||
129 | |||
130 | 1 | list($credit, $sended, $cost, $unsend, $batchId) = explode(',', $response); |
|
131 | |||
132 | return [ |
||
133 | 1 | 'credit' => $this->setCredit($credit)->credit, |
|
134 | 1 | 'sended' => (int) $sended, |
|
135 | 1 | 'cost' => (float) $cost, |
|
136 | 1 | 'unsend' => (int) $unsend, |
|
137 | 1 | 'batchId' => $batchId, |
|
138 | 1 | ]; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * isValidResponse. |
||
143 | * |
||
144 | * @param string $response |
||
145 | * |
||
146 | * @return bool |
||
147 | */ |
||
148 | 4 | protected function isValidResponse($response) |
|
152 | |||
153 | /** |
||
154 | * doRequest. |
||
155 | * |
||
156 | * @param string $uri |
||
157 | * @param array $params |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | 4 | protected function doRequest($uri, $params) |
|
173 | } |
||
174 |