Skip to content

Commit f319920

Browse files
Complete package restructure (#2)
* Complete package restructure Restructured whole package to support twilio sms service and dynamical prediction based on gateway priority configuration. * DOC: PHP Doc fix * TASK: Reformat code
1 parent 304e948 commit f319920

30 files changed

+1056
-537
lines changed

src/Adapters/Adapter.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Linkstreet\LaravelSms\Adapters;
4+
5+
use Linkstreet\LaravelSms\Exceptions\AdapterException;
6+
7+
class Adapter
8+
{
9+
/**
10+
* List of adapter implementations
11+
*
12+
* @var array
13+
*/
14+
private static $list = [
15+
'twilio' => Twilio\TwilioAdapter::class,
16+
'kap' => Kap\KapAdapter::class,
17+
];
18+
19+
/**
20+
* @param string $adapter
21+
* @return mixed|static
22+
* @throws AdapterException
23+
*/
24+
public static function find(string $adapter)
25+
{
26+
return isset(self::$list[$adapter])
27+
? self::$list[$adapter]
28+
: AdapterException::notFound($adapter);
29+
}
30+
31+
/**
32+
* @return array
33+
*/
34+
public static function all()
35+
{
36+
return self::$list;
37+
}
38+
}

src/Adapters/BaseAdapter.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Adapters/HttpClient.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Linkstreet\LaravelSms\Adapters;
4+
5+
use GuzzleHttp\Client;
6+
7+
trait HttpClient
8+
{
9+
protected $client;
10+
11+
/**
12+
* @param Client|null $client
13+
* @return self
14+
*/
15+
public function setClient($client = null)
16+
{
17+
$this->client = $client ?? new Client();
18+
19+
return $this;
20+
}
21+
}

src/Adapters/Kap/KapAdapter.php

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,69 @@
22

33
namespace Linkstreet\LaravelSms\Adapters\Kap;
44

5-
use GuzzleHttp\Client;
65
use GuzzleHttp\Psr7\Request;
7-
use Illuminate\Support\Facades\Config;
8-
use Linkstreet\LaravelSms\Adapters\BaseAdapter;
6+
use Linkstreet\LaravelSms\Adapters\HttpClient;
97
use Linkstreet\LaravelSms\Contracts\AdapterInterface;
8+
use Linkstreet\LaravelSms\Contracts\ResponseInterface;
109
use Linkstreet\LaravelSms\Exceptions\AdapterException;
10+
use Linkstreet\LaravelSms\Model\Device;
1111

1212
/**
1313
* KapAdapter
1414
*/
15-
class KapAdapter extends BaseAdapter implements AdapterInterface
15+
class KapAdapter implements AdapterInterface
1616
{
17+
use HttpClient;
18+
19+
/**
20+
* @var array
21+
*/
22+
private $config;
23+
1724
/**
1825
* Create a instance of Kap Adapter
19-
*
20-
* @param array|null configuration for KAP adapter
26+
* @param array configuration for KAP adapter
2127
*/
22-
public function __construct($config = null)
28+
public function __construct(array $config)
2329
{
24-
$this->config = $this->requiredConfig($config);
25-
$this->setClient(new Client());
30+
$this->config = $config;
31+
32+
$this->setClient();
2633
}
2734

2835
/**
2936
* {@inheritdoc}
3037
*/
31-
public function send($devices, $message)
38+
public function send(Device $device, string $message): ResponseInterface
3239
{
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);
3545
}
3646

3747
/**
3848
* Build Guzzle request object
39-
*
4049
* @return Request
4150
*/
42-
private function buildRequest()
51+
private function buildRequest(): Request
4352
{
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');
4554
}
4655

4756
/**
4857
* Build Guzzle query options with json payload
49-
*
50-
* @param DeviceCollection $devices
51-
* @param Message $message
58+
* @param Device $device
59+
* @param string $message
5260
* @return array
5361
*/
54-
private function buildOptions($devices, $message)
62+
private function buildOptions(Device $device, string $message): array
5563
{
56-
$recipients = [];
57-
58-
foreach ($devices as $device) {
59-
$recipients[]['gsm'] = $device->getNumber();
60-
}
61-
6264
return [
6365
'debug' => false,
64-
'timeout' => 10,
66+
'verify' => false,
67+
'timeout' => 20,
6568
'json' => [
6669
'authentication' => [
6770
'username' => $this->config['username'],
@@ -70,43 +73,27 @@ private function buildOptions($devices, $message)
7073
'messages' => [
7174
[
7275
'sender' => $this->config['sender'],
73-
'text' => $message->getMessage(),
76+
'text' => $message,
7477
'type' => 'longSMS',
75-
'recipients' => $recipients
78+
'recipients' => [
79+
['gsm' => $device->getNumberWithoutPlusSign()]
80+
],
7681
]
7782
]
7883
]
7984
];
8085
}
8186

8287
/**
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
8789
* @throws AdapterException
8890
*/
89-
private function requiredConfig($config = null)
91+
private function checkForMissingConfiguration()
9092
{
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;
10494

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();
10897
}
109-
110-
return $config;
11198
}
11299
}

0 commit comments

Comments
 (0)