Commit 82790608 authored by Mark Harding's avatar Mark Harding

(fix): twilio not getting correct config

parent 749d169b
Pipeline #213538611 failed with stages
in 2 minutes and 19 seconds
<?php
/**
* Minds SMS Service via Twilio
*/
......@@ -6,6 +7,7 @@
namespace Minds\Core\SMS\Services;
use Minds\Core\Di\Di;
use Minds\Core\Config;
use Minds\Core\SMS\Exceptions\InvalidPhoneException;
use Minds\Core\SMS\SMSServiceInterface;
use Twilio\Rest\Client as TwilioClient;
......@@ -14,17 +16,17 @@ class Twilio implements SMSServiceInterface
{
/** @var TwilioClient */
protected $client;
/** @var array */
/** @var Config */
protected $config;
/** @var string */
protected $from;
public function __construct($client = null, $config = null)
{
$this->config = $config ? $config : Di::_()->get('Config')->get('twilio');
$AccountSid = $this->config['account_sid'] ?: 'not set';
$AuthToken = $this->config['auth_token'] ?: 'not set';
$this->client = $client ? $client : new TwilioClient($AccountSid, $AuthToken);
$this->config = $config ?? Di::_()->get('Config');
$this->client = $client;
}
/**
......@@ -33,10 +35,10 @@ class Twilio implements SMSServiceInterface
* @return boolean
* @throws InvalidPhoneException
*/
public function verify($number)
public function verify($number): bool
{
try {
$phone_number = $this->client->lookups->v1->phoneNumbers($number)
$phone_number = $this->getClient()->lookups->v1->phoneNumbers($number)
->fetch(["type" => "carrier"]);
return $phone_number->carrier['type'] !== 'voip';
......@@ -49,17 +51,17 @@ class Twilio implements SMSServiceInterface
/**
* Send an sms
*/
public function send($number, $message)
public function send($number, $message): bool
{
$result = null;
try {
$result = $this->client->messages->create(
$result = $this->getClient()->messages->create(
$number,
[
'from' => $this->config['from'],
'body' => $message,
]
'from' => $this->getConfig()['from'],
'body' => $message,
]
);
} catch (\Exception $e) {
error_log("[guard] Twilio error: {$e->getMessage()}");
......@@ -67,4 +69,28 @@ class Twilio implements SMSServiceInterface
return $result ? $result->sid : false;
}
/**
* Get the twilio client
* @return TwilioClient
*/
private function getClient(): TwilioClient
{
if (!$this->client) {
$AccountSid = $this->getConfig()['account_sid'] ?: 'not set';
$AuthToken = $this->getConfig()['auth_token'] ?: 'not set';
$this->client = new TwilioClient($AccountSid, $AuthToken);
}
return $this->client;
}
/**
* Get the twilio config
* @return array
*/
private function getConfig(): array
{
return $this->config->get('twilio');
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment