Skip to content

Commit 46ef333

Browse files
Set feature flag to enable/disable sending SMS (#9)
If the flag is set to false, SMS details will be logged using LogAdapter.
1 parent df24c17 commit 46ef333

File tree

6 files changed

+105
-7
lines changed

6 files changed

+105
-7
lines changed

src/Adapters/Log/LogAdapter.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\Log;
4+
5+
use Illuminate\Support\Facades\Log;
6+
use Linkstreet\LaravelSms\Contracts\AdapterInterface;
7+
use Linkstreet\LaravelSms\Contracts\ResponseInterface;
8+
use Linkstreet\LaravelSms\Model\Device;
9+
10+
class LogAdapter implements AdapterInterface
11+
{
12+
public function send(Device $device, string $message): ResponseInterface
13+
{
14+
Log::debug('SMS', [
15+
'device' => $device->toArray(),
16+
'message' => $message,
17+
]);
18+
19+
return new LogResponse($device, $message);
20+
}
21+
}

src/Adapters/Log/LogResponse.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Linkstreet\LaravelSms\Adapters\Log;
4+
5+
use Linkstreet\LaravelSms\Contracts\ResponseInterface;
6+
use Linkstreet\LaravelSms\Model\Device;
7+
8+
class LogResponse implements ResponseInterface
9+
{
10+
private Device $device;
11+
private string $message;
12+
13+
public function __construct(Device $device, string $message)
14+
{
15+
$this->device = $device;
16+
$this->message = $message;
17+
}
18+
19+
public function getStatusCode(): int
20+
{
21+
return 200;
22+
}
23+
24+
public function getResponse()
25+
{
26+
return (object)[
27+
'device' => $this->device->toArray(),
28+
'message' => $this->message,
29+
'debug' => 'Mocked success response',
30+
];
31+
}
32+
33+
public function getRaw()
34+
{
35+
return $this;
36+
}
37+
38+
public function isSuccess(): bool
39+
{
40+
return true;
41+
}
42+
43+
public function isFailure(): bool
44+
{
45+
return !$this->isSuccess();
46+
}
47+
48+
public function getErrorCode()
49+
{
50+
return '';
51+
}
52+
53+
public function getErrorMessage()
54+
{
55+
return '';
56+
}
57+
58+
public function getReasonPhrase()
59+
{
60+
return '';
61+
}
62+
}

src/Config/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| Supported adapter: twilio, kap
1414
|
1515
*/
16+
'enabled' => env('SMS_ENABLED', true),
1617

1718
'default' => 'kap',
1819

src/Model/Device.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ public function getCountryIso(): string
5252
* Get device number without plus sign
5353
* @return string
5454
*/
55-
public function getNumberWithoutPlusSign()
55+
public function getNumberWithoutPlusSign(): string
5656
{
5757
return ltrim($this->getNumber(), '+');
5858
}
5959

6060
/**
6161
* @return array
6262
*/
63-
public function toArray()
63+
public function toArray(): array
6464
{
65-
return array($this);
65+
return get_object_vars($this);
6666
}
6767
}

src/SmsManager.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Linkstreet\LaravelSms;
44

55
use Linkstreet\LaravelSms\Adapters\Adapter;
6+
use Linkstreet\LaravelSms\Adapters\Log\LogAdapter;
67
use Linkstreet\LaravelSms\Contracts\AdapterInterface;
78
use Linkstreet\LaravelSms\Contracts\ResponseInterface;
89
use Linkstreet\LaravelSms\Exceptions\AdapterException;
@@ -177,6 +178,10 @@ public function resolveConnection(Device $device): string
177178
*/
178179
public function getAdapter(string $connection): AdapterInterface
179180
{
181+
if (false == $this->config['enabled']) {
182+
return new LogAdapter();
183+
}
184+
180185
$properties = $this->config['connections'][$connection];
181186

182187
$class = Adapter::find($properties['adapter']);
@@ -191,7 +196,7 @@ public function toArray(): array
191196
{
192197
return [
193198
'connection' => $this->connection,
194-
'device' => is_null($this->device) ?: $this->device->toArray(),
199+
'device' => $this->device,
195200
];
196201
}
197202
}

tests/SmsManagerTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Linkstreet\LaravelSms\Tests;
44

55
use Linkstreet\LaravelSms\Adapters\Adapter;
6+
use Linkstreet\LaravelSms\Adapters\Log\LogAdapter;
67
use Linkstreet\LaravelSms\Exceptions\AdapterException;
78
use Linkstreet\LaravelSms\Model\Device;
89
use Linkstreet\LaravelSms\SmsManager;
@@ -52,7 +53,7 @@ public function setDevice()
5253
$device = new Device('+10123456789', 'US');
5354

5455
$manager = new SmsManager($this->config);
55-
$m_device = $manager->to($device)->toArray()['device'][0];
56+
$m_device = $manager->to($device)->toArray()['device'];
5657

5758
$this->assertSame($m_device->getNumber(), $device->getNumber());
5859
$this->assertSame($m_device->getCountryIso(), $device->getCountryIso());
@@ -83,12 +84,20 @@ public function returnsConnectionAdapter()
8384
{
8485
$manager = new SmsManager($this->config);
8586

86-
$manager->to(new Device('+10123456789', 'US'));
87-
8887
$adapter = $manager->getAdapter($this->config['default']);
8988

9089
$default_adapter = $this->config['connections'][$this->config['default']]['adapter'];
9190

9291
$this->assertInstanceOf(Adapter::find($default_adapter), $adapter);
9392
}
93+
94+
/** @test */
95+
public function returnsLogAdapter()
96+
{
97+
$manager = new SmsManager(array_merge($this->config, ['enabled' => false]));
98+
99+
$adapter = $manager->getAdapter($this->config['default']);
100+
101+
$this->assertInstanceOf(LogAdapter::class, $adapter);
102+
}
94103
}

0 commit comments

Comments
 (0)