2
2
3
3
namespace Linkstreet \LaravelSms \Adapters \Kap ;
4
4
5
- use GuzzleHttp \Client ;
6
5
use GuzzleHttp \Psr7 \Request ;
7
- use Illuminate \Support \Facades \Config ;
8
- use Linkstreet \LaravelSms \Adapters \BaseAdapter ;
6
+ use Linkstreet \LaravelSms \Adapters \HttpClient ;
9
7
use Linkstreet \LaravelSms \Contracts \AdapterInterface ;
8
+ use Linkstreet \LaravelSms \Contracts \ResponseInterface ;
10
9
use Linkstreet \LaravelSms \Exceptions \AdapterException ;
10
+ use Linkstreet \LaravelSms \Model \Device ;
11
11
12
12
/**
13
13
* KapAdapter
14
14
*/
15
- class KapAdapter extends BaseAdapter implements AdapterInterface
15
+ class KapAdapter implements AdapterInterface
16
16
{
17
+ use HttpClient;
18
+
19
+ /**
20
+ * @var array
21
+ */
22
+ private $ config ;
23
+
17
24
/**
18
25
* Create a instance of Kap Adapter
19
- *
20
- * @param array|null configuration for KAP adapter
26
+ * @param array configuration for KAP adapter
21
27
*/
22
- public function __construct ($ config = null )
28
+ public function __construct (array $ config )
23
29
{
24
- $ this ->config = $ this ->requiredConfig ($ config );
25
- $ this ->setClient (new Client ());
30
+ $ this ->config = $ config ;
31
+
32
+ $ this ->setClient ();
26
33
}
27
34
28
35
/**
29
36
* {@inheritdoc}
30
37
*/
31
- public function send ($ devices , $ message )
38
+ public function send (Device $ device , string $ message ): ResponseInterface
32
39
{
33
- $ this ->response = $ this ->client ->send ($ this ->buildRequest (), $ this ->buildOptions ($ devices , $ message ));
34
- return new KapResponse ($ devices , $ this ->response );
40
+ $ this ->checkForMissingConfiguration ();
41
+
42
+ $ response = $ this ->client ->send ($ this ->buildRequest (), $ this ->buildOptions ($ device , $ message ));
43
+
44
+ return new KapResponse ($ device , $ response );
35
45
}
36
46
37
47
/**
38
48
* Build Guzzle request object
39
- *
40
49
* @return Request
41
50
*/
42
- private function buildRequest ()
51
+ private function buildRequest (): Request
43
52
{
44
- return new Request ('POST ' , 'http ://api.kapsystem.com/api/v3/sendsms/json ' );
53
+ return new Request ('POST ' , 'https ://api.kapsystem.com/api/v3/sendsms/json ' );
45
54
}
46
55
47
56
/**
48
57
* Build Guzzle query options with json payload
49
- *
50
- * @param DeviceCollection $devices
51
- * @param Message $message
58
+ * @param Device $device
59
+ * @param string $message
52
60
* @return array
53
61
*/
54
- private function buildOptions ($ devices , $ message )
62
+ private function buildOptions (Device $ device , string $ message ): array
55
63
{
56
- $ recipients = [];
57
-
58
- foreach ($ devices as $ device ) {
59
- $ recipients []['gsm ' ] = $ device ->getNumber ();
60
- }
61
-
62
64
return [
63
65
'debug ' => false ,
64
- 'timeout ' => 10 ,
66
+ 'verify ' => false ,
67
+ 'timeout ' => 20 ,
65
68
'json ' => [
66
69
'authentication ' => [
67
70
'username ' => $ this ->config ['username ' ],
@@ -70,43 +73,27 @@ private function buildOptions($devices, $message)
70
73
'messages ' => [
71
74
[
72
75
'sender ' => $ this ->config ['sender ' ],
73
- 'text ' => $ message-> getMessage () ,
76
+ 'text ' => $ message ,
74
77
'type ' => 'longSMS ' ,
75
- 'recipients ' => $ recipients
78
+ 'recipients ' => [
79
+ ['gsm ' => $ device ->getNumberWithoutPlusSign ()]
80
+ ],
76
81
]
77
82
]
78
83
]
79
84
];
80
85
}
81
86
82
87
/**
83
- * Check & retrieve the config for KAP adapter
84
- *
85
- * @param array|null configuration for KAP adapter
86
- * @return array
88
+ * Check for valid configuration
87
89
* @throws AdapterException
88
90
*/
89
- private function requiredConfig ( $ config = null )
91
+ private function checkForMissingConfiguration ( )
90
92
{
91
- if (is_null ($ config )) {
92
- $ config = Config::get ('sms.adapter.kap ' );
93
- }
94
-
95
- // Check for username in the config
96
- if (!isset ($ config ['username ' ]) || empty ($ config ['username ' ])) {
97
- throw new AdapterException ('Invalid username ' );
98
- }
99
-
100
- // Check for password in the config
101
- if (!isset ($ config ['password ' ]) || empty ($ config ['password ' ])) {
102
- throw new AdapterException ('Invalid password ' );
103
- }
93
+ $ config = $ this ->config ;
104
94
105
- // Check for sender id in the config
106
- if (!isset ($ config ['sender ' ]) || empty ($ config ['sender ' ])) {
107
- throw new AdapterException ('Invalid sender id ' );
95
+ if (!isset ($ config ['username ' ], $ config ['password ' ], $ config ['sender ' ])) {
96
+ throw AdapterException::missingConfiguration ();
108
97
}
109
-
110
- return $ config ;
111
98
}
112
99
}
0 commit comments