fix behaviour when twilio is not configured

This commit is contained in:
Julien Fastré 2021-03-28 23:37:22 +02:00
parent 2f4f1f2c55
commit 82f95d2745

View File

@ -36,27 +36,22 @@ use Psr\Cache\CacheItemPoolInterface;
class PhonenumberHelper
{
/**
*
* @var Client
* Twilio client
*/
protected $twilioClient;
protected Client $twilioClient;
/**
*
* @var LoggerInterface
* TRUE if the client is properly configured
*/
protected $logger;
protected bool $isConfigured = false;
/**
*
* @var CacheItemPoolInterface
*/
protected $cachePool;
protected LoggerInterface $logger;
protected CacheItemPoolInterface $cachePool;
const LOOKUP_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
const FORMAT_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
public function __construct(
CacheItemPoolInterface $cachePool,
$config,
@ -67,12 +62,16 @@ class PhonenumberHelper
if (\array_key_exists('twilio_sid', $config)
&& !empty($config['twilio_sid'])
&& strlen($config['twilio_sid']) > 2
&& \array_key_exists('twilio_secret', $config)
&& !empty($config['twilio_secret'])) {
&& !empty($config['twilio_secret'])
&& strlen($config['twilio_secret']) > 2
) {
$this->twilioClient = new Client([
'auth' => [ $config['twilio_sid'], $config['twilio_secret'] ]
]);
$this->isConfigured = TRUE;
}
}
@ -84,11 +83,11 @@ class PhonenumberHelper
*/
public function isPhonenumberValidationConfigured() : bool
{
return NULL !== $this->twilioClient;
return $this->isConfigured;
}
/**
* REturn true if the phoennumber is a mobile phone. Return always false
* REturn true if the phoennumber is a mobile phone. Return always true
* if the validation is not configured.
*
* @param string $phonenumber
@ -96,17 +95,21 @@ class PhonenumberHelper
*/
public function isValidPhonenumberMobile($phonenumber) : bool
{
if (FALSE === $this->isPhonenumberValidationConfigured()) {
return true;
}
$validation = $this->performTwilioLookup($phonenumber);
if (NULL === $validation) {
return false;
return true;
}
return $validation === 'mobile';
}
/**
* Return true if the phonenumber is a landline or voip phone. Return always false
* Return true if the phonenumber is a landline or voip phone. Return always true
* if the validation is not configured.
*
* @param string $phonenumber
@ -114,17 +117,21 @@ class PhonenumberHelper
*/
public function isValidPhonenumberLandOrVoip($phonenumber) : bool
{
if (FALSE === $this->isPhonenumberValidationConfigured()) {
return true;
}
$validation = $this->performTwilioLookup($phonenumber);
if (NULL === $validation) {
return false;
return true;
}
return \in_array($validation, [ 'landline', 'voip' ]);
}
/**
* Return true if the phonenumber is a landline or voip phone. Return always false
* Return true if the phonenumber is a landline or voip phone. Return always true
* if the validation is not configured.
*
* @param string $phonenumber
@ -132,8 +139,11 @@ class PhonenumberHelper
*/
public function isValidPhonenumberAny($phonenumber) : bool
{
if (FALSE === $this->isPhonenumberValidationConfigured()) {
return true;
}
$validation = $this->performTwilioLookup($phonenumber);
;
if (NULL === $validation) {
return false;
}
@ -150,7 +160,7 @@ class PhonenumberHelper
*/
public function getType(string $phonenumber): string
{
return $this->performTwilioLookup($phonenumber);
return $this->performTwilioLookup($phonenumber) ?? 'unknown';
}
public function format($phonenumber)
@ -233,7 +243,7 @@ class PhonenumberHelper
$item = $this->cachePool->getItem('pnum_'.$filtered);
if ($item->isHit()) {
return $item->get();
//return $item->get();
}
try {