Features/new phones

This commit is contained in:
Jean-Francois Monfort
2021-04-02 12:11:20 +00:00
committed by Julien Fastré
parent 6ea45e4d16
commit 27c680bb03
14 changed files with 502 additions and 123 deletions

View File

@@ -26,146 +26,168 @@ use Psr\Cache\CacheItemPoolInterface;
/**
* Helper to some task linked to phonenumber.
*
* Currently, only Twilio is supported (https://www.twilio.com/lookup). A method
*
* Currently, only Twilio is supported (https://www.twilio.com/lookup). A method
* allow to check if the helper is configured for validation. This should be used
* before doing some validation.
*
*
*
*/
class PhonenumberHelper
{
/**
*
* @var Client
* Twilio client
*/
protected $twilioClient;
protected Client $twilioClient;
/**
*
* @var LoggerInterface
* TRUE if the client is properly configured
*/
protected $logger;
/**
*
* @var CacheItemPoolInterface
*/
protected $cachePool;
protected bool $isConfigured = false;
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,
$config,
LoggerInterface $logger
) {
$this->logger = $logger;
$this->cachePool = $cachePool;
if (\array_key_exists('twilio_sid', $config)
if (\array_key_exists('twilio_sid', $config)
&& !empty($config['twilio_sid'])
&& \array_key_exists('twilio_secret', $config)
&& !empty($config['twilio_secret'])) {
&& strlen($config['twilio_sid']) > 2
&& \array_key_exists('twilio_secret', $config)
&& !empty($config['twilio_secret'])
&& strlen($config['twilio_secret']) > 2
) {
$this->twilioClient = new Client([
'auth' => [ $config['twilio_sid'], $config['twilio_secret'] ]
]);
}
$this->isConfigured = TRUE;
}
}
/**
* Return true if the validation is configured and available.
*
*
* @return 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.
*
*
* @param string $phonenumber
* @return bool
*/
public function isValidPhonenumberMobile($phonenumber) : bool
{
$validation = $this->performTwilioLookup($phonenumber);
if (NULL === $validation) {
return false;
if (FALSE === $this->isPhonenumberValidationConfigured()) {
return true;
}
$validation = $this->performTwilioLookup($phonenumber);
if (NULL === $validation) {
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
* @return bool
*/
public function isValidPhonenumberLandOrVoip($phonenumber) : bool
{
$validation = $this->performTwilioLookup($phonenumber);
if (NULL === $validation) {
return false;
if (FALSE === $this->isPhonenumberValidationConfigured()) {
return true;
}
$validation = $this->performTwilioLookup($phonenumber);
if (NULL === $validation) {
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
* @return bool
*/
public function isValidPhonenumberAny($phonenumber) : bool
{
if (FALSE === $this->isPhonenumberValidationConfigured()) {
return true;
}
$validation = $this->performTwilioLookup($phonenumber);
;
if (NULL === $validation) {
return false;
}
return \in_array($validation, [ 'landline', 'voip', 'mobile' ]);
}
/**
* Get type (mobile, landline, ...) for phone number
*
* @param string $phonenumber
*
* @return string
*/
public function getType(string $phonenumber): string
{
return $this->performTwilioLookup($phonenumber) ?? 'unknown';
}
public function format($phonenumber)
{
return $this->performTwilioFormat($phonenumber);
}
protected function performTwilioFormat($phonenumber)
{
if (FALSE === $this->isPhonenumberValidationConfigured()) {
return $phonenumber;
}
// filter only number
$filtered = \preg_replace("/[^0-9]/", "", $phonenumber);
$item = $this->cachePool->getItem('pnum_format_nat_'.$filtered);
if ($item->isHit()) {
return $item->get();
}
try {
$response = $this->twilioClient->get(sprintf(self::FORMAT_URI, '+'.$filtered), [
'http_errors' => true,
]);
} catch (ClientException $e) {
$response = $e->getResponse();
$this->logger->error("[phonenumber helper] Could not format number "
@@ -174,7 +196,7 @@ class PhonenumberHelper
"status_code" => $response->getStatusCode(),
"phonenumber" => $phonenumber
]);
return $phonenumber;
} catch (ServerException $e) {
$response = $e->getResponse();
@@ -184,7 +206,7 @@ class PhonenumberHelper
"status_code" => $response->getStatusCode(),
"phonenumber" => $phonenumber
]);
return null;
} catch (ConnectException $e) {
$this->logger->error("[phonenumber helper] Could not format number "
@@ -192,38 +214,38 @@ class PhonenumberHelper
"message" => $e->getMessage(),
"phonenumber" => $phonenumber
]);
return null;
}
$format = \json_decode($response->getBody())->national_format;
$item
->set($format)
// expires after 3d
->expiresAfter(3600 * 24 * 3)
;
$this->cachePool->save($item);
return $format;
}
protected function performTwilioLookup($phonenumber)
{
if (FALSE === $this->isPhonenumberValidationConfigured()) {
return null;
}
// filter only number
$filtered = \preg_replace("/[^0-9]/", "", $phonenumber);
$item = $this->cachePool->getItem('pnum_'.$filtered);
if ($item->isHit()) {
return $item->get();
//return $item->get();
}
try {
$response = $this->twilioClient->get(sprintf(self::LOOKUP_URI, '+'.$filtered), [
'http_errors' => true,
@@ -241,7 +263,7 @@ class PhonenumberHelper
"status_code" => $response->getStatusCode(),
"phonenumber" => $phonenumber
]);
return null;
} catch (ConnectException $e) {
$this->logger->error("[phonenumber helper] Could not format number "
@@ -249,20 +271,20 @@ class PhonenumberHelper
"message" => $e->getMessage(),
"phonenumber" => $phonenumber
]);
return null;
}
$validation = \json_decode($response->getBody())->carrier->type;
$item
->set($validation)
// expires after 12h
->expiresAfter(3600 * 12)
;
$this->cachePool->save($item);
return $validation;
}
}