Crap. Tonight is raid night and I am already late.

This commit is contained in:
2023-11-01 18:10:40 -04:00
commit 5e1dca4eb2
7 changed files with 249 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
namespace Siteworx\Sms\Adapters;
interface AdapterInterface
{
/**
* @param string $number
* @param string $message
* @return string uuid of message
*/
public function sendText(string $number, string $message): string;
/**
* @param string $number
* @return bool
*/
public function validatePhoneNumber(string $number): bool;
}

56
src/Adapters/Log.php Normal file
View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace Siteworx\Sms\Adapters;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* Class Log
* @package Siteworx\Library\Sms\Adapters
*/
final class Log implements AdapterInterface
{
private string $logLevel;
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger, array $options = [])
{
$this->logger = $logger;
$this->logLevel = $options['logLevel'] ?? LogLevel::INFO;
}
/**
* @param string $number
* @param string $message
* @return string
* @throws \Exception
*/
public function sendText(string $number, string $message): string
{
$this->logger->{$this->logLevel}(sprintf('[Text Message] (%s): "%s"', $number, $message));
return sprintf(
'%04X%04X-%04X-%04X-%04X-%04X%04X%04X',
random_int(0, 65535),
random_int(0, 65535),
random_int(0, 65535),
random_int(16384, 20479),
random_int(32768, 49151),
random_int(0, 65535),
random_int(0, 65535),
random_int(0, 65535)
);
}
public function validatePhoneNumber(string $number): bool
{
preg_match('/\+1-[\d]{3}-[\d]{3}-[\d]{4}/', $number, $output_array);
return \count($output_array) === 1;
}
}

49
src/Adapters/Sns.php Normal file
View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Siteworx\Sms\Adapters;
use Aws\Sns\SnsClient;
/**
* Class Sns
* @package Siteworx\Library\Sms\Adapters
*/
final class Sns implements AdapterInterface
{
private SnsClient $snsClient;
public function __construct(array $options)
{
if (!isset($options['version'])) {
throw new \InvalidArgumentException('AWS sdk version is required');
}
if (!isset($options['region'])) {
throw new \InvalidArgumentException('AWS sdk region is required');
}
$this->snsClient = new SnsClient($options);
}
public function sendText(string $number, string $message): string
{
$result = $this->snsClient->publish([
'Message' => $message,
'PhoneNumber' => $number,
]);
$messageId = $result->get('MessageId');
return $messageId ?? '';
}
public function validatePhoneNumber(string $number): bool
{
preg_match('/\+1-[\d]{3}-[\d]{3}-[\d]{4}/', $number, $output_array);
return \count($output_array) === 1;
}
}

38
src/TextMessenger.php Normal file
View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Siteworx\Sms;
use Siteworx\Sms\Adapters\AdapterInterface;
final class TextMessenger
{
private AdapterInterface $adapter;
public const ADAPTERS = [
'log' => Log::class,
'sns' => Sns::class
];
public function __construct(AdapterInterface $adapter)
{
$this->adapter = $adapter;
}
/**
* @param string $number
* @param string $message
* @return string
* @throws \InvalidArgumentException
*/
public function sendText(string $number, string $message): string
{
if ($this->adapter->validatePhoneNumber($number) === false) {
throw new \InvalidArgumentException('Invalid phone number for adapter: ' . $number);
}
return $this->adapter->sendText($number, $message);
}
}