From 82f95d27455145d515c7969ecc93f2814beeadd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Sun, 28 Mar 2021 23:37:22 +0200 Subject: [PATCH] fix behaviour when twilio is not configured --- .../Phonenumber/PhonenumberHelper.php | 56 +++++++++++-------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php b/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php index 9f2787abb..c2ab59442 100644 --- a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php +++ b/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php @@ -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 {