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