add function to format phonenumber using twilio (if configured)

This commit is contained in:
Julien Fastré 2019-10-30 22:56:53 +01:00
parent 87a01e4ab3
commit 241815e687
4 changed files with 118 additions and 1 deletions

View File

@ -87,4 +87,4 @@ Master branch
- fix errors in pagination
- fix search: usage of parenthesis
- add DQL function REPLACE for replacing in strings: "REPLACE(string, 'from', 'to')"
- add function to format phonenumber

View File

@ -53,6 +53,7 @@ class PhonenumberHelper
protected $cachePool;
const LOOKUP_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
const FORMAT_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
public function __construct(
@ -121,6 +122,64 @@ class PhonenumberHelper
return \in_array($validation, [ 'landline', 'voip' ]);
}
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) {
$this->logger->error("[phonenumber helper] Could not format number "
. "due to client error", [
"message" => $e->getResponseBodySummary($e->getResponse()),
"status_code" => $e->getResponse()->getStatusCode(),
"phonenumber" => $phonenumber
]);
return $phonenumber;
} catch (ServerException $e) {
$this->logger->error("[phonenumber helper] Could not format number "
. "due to server error", [
"message" => $e->getResponseBodySummary($e->getResponse()),
"status_code" => $e->getResponse()->getStatusCode(),
"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()) {

View File

@ -0,0 +1,52 @@
<?php
/*
* Copyright (C) 2019 Champs Libres Cooperative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\MainBundle\Phonenumber;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Chill\MainBundle\Phonenumber\PhonenumberHelper;
/**
*
*
*/
class Templating extends AbstractExtension
{
/**
*
* @var PhonenumberHelper
*/
protected $phonenumberHelper;
public function __construct(PhonenumberHelper $phonenumberHelper)
{
$this->phonenumberHelper = $phonenumberHelper;
}
public function getFilters()
{
return [
new TwigFilter('chill_format_phonenumber', [$this, 'formatPhonenumber'])
];
}
public function formatPhonenumber($phonenumber)
{
return $this->phonenumberHelper->format($phonenumber);
}
}

View File

@ -4,6 +4,12 @@ services:
$logger: '@Psr\Log\LoggerInterface'
$config: '%chill_main.phone_helper%'
$cachePool: '@cache.user_data'
Chill\MainBundle\Phonenumber\Templating:
arguments:
$phonenumberHelper: '@Chill\MainBundle\Phonenumber\PhonenumberHelper'
tags:
- { name: twig.extension }
Chill\MainBundle\Validation\Validator\ValidPhonenumber:
arguments: