Skip to content

Commit 53c43dc

Browse files
Add single method to send sms
1 parent 63e9df9 commit 53c43dc

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Linkstreet\LaravelSms\Exceptions;
4+
5+
class MessageException extends \Exception
6+
{
7+
/**
8+
* Message not found
9+
* @return static
10+
*/
11+
public static function notFound()
12+
{
13+
return new static('Message not found');
14+
}
15+
}

src/SmsManager.php

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Linkstreet\LaravelSms\Contracts\ResponseInterface;
88
use Linkstreet\LaravelSms\Exceptions\AdapterException;
99
use Linkstreet\LaravelSms\Exceptions\DeviceException;
10+
use Linkstreet\LaravelSms\Exceptions\MessageException;
1011
use Linkstreet\LaravelSms\Model\Device;
1112

1213
/**
@@ -25,6 +26,11 @@ class SmsManager
2526
*/
2627
private $device;
2728

29+
/**
30+
* @var string
31+
*/
32+
private $message;
33+
2834
/**
2935
* @var array
3036
*/
@@ -49,6 +55,7 @@ public static function create(array $config)
4955
}
5056

5157
/**
58+
* Set connection
5259
* @param string $connection
5360
* @return self
5461
* @throws AdapterException
@@ -65,8 +72,7 @@ public function connection(string $connection): self
6572
}
6673

6774
/**
68-
* Add the device collection
69-
*
75+
* Add device
7076
* @param \Linkstreet\LaravelSms\Model\Device $device
7177
* @return self
7278
*/
@@ -78,21 +84,54 @@ public function to(Device $device): self
7884
}
7985

8086
/**
81-
* Send the message
87+
* Add message
8288
* @param string $message
89+
* @return $this
90+
*/
91+
public function message(string $message): self
92+
{
93+
$this->message = $message;
94+
95+
return $this;
96+
}
97+
98+
/**
99+
* Dispatch the sms message via adapter
83100
* @return ResponseInterface
84101
* @throws AdapterException
85102
* @throws DeviceException
103+
* @throws MessageException
86104
*/
87-
public function send(string $message): ResponseInterface
105+
public function dispatch(): ResponseInterface
88106
{
89107
if (null === $this->device) {
90108
throw DeviceException::notFound();
91109
}
92110

111+
if (null === $this->message) {
112+
throw MessageException::notFound();
113+
}
114+
93115
$this->connection = $this->connection ?? $this->resolveConnection($this->device);
94116

95-
return $this->getAdapter($this->connection)->send($this->device, $message);
117+
return $this->getAdapter($this->connection)->send($this->device, $this->message);
118+
}
119+
120+
/**
121+
* Send a sms
122+
* @param Device $device
123+
* @param string $message
124+
* @return ResponseInterface
125+
* @throws AdapterException
126+
* @throws DeviceException
127+
* @throws MessageException
128+
*/
129+
public function send(Device $device, string $message): ResponseInterface
130+
{
131+
$this->device = $device;
132+
$this->message = $message;
133+
134+
return $this->dispatch();
96135
}
97136

98137
/**

0 commit comments

Comments
 (0)