Remove obsolete services from ChillMainBundle.

This commit is contained in:
Pol Dellaiera 2021-05-19 19:23:52 +02:00
parent c9eaf3afac
commit b4c8d4c240
No known key found for this signature in database
GPG Key ID: D476DFE9C67467CA
5 changed files with 0 additions and 417 deletions

View File

@ -162,7 +162,6 @@ class ChillMainExtension extends Extension implements
$loader->load('services/notification.yaml'); $loader->load('services/notification.yaml');
$loader->load('services/redis.yaml'); $loader->load('services/redis.yaml');
$loader->load('services/command.yaml'); $loader->load('services/command.yaml');
$loader->load('services/phonenumber.yaml');
$loader->load('services/cache.yaml'); $loader->load('services/cache.yaml');
$loader->load('services/templating.yaml'); $loader->load('services/templating.yaml');
$loader->load('services/timeline.yaml'); $loader->load('services/timeline.yaml');

View File

@ -1,276 +0,0 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Phonenumber;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Exception\ServerException;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Log\LoggerInterface;
use function array_key_exists;
use function in_array;
use function json_decode;
use function preg_replace;
use function strlen;
/**
* Helper to some task linked to phonenumber.
*
* 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
{
public const FORMAT_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
public const LOOKUP_URI = 'https://lookups.twilio.com/v1/PhoneNumbers/%s';
protected CacheItemPoolInterface $cachePool;
/**
* TRUE if the client is properly configured.
*/
protected bool $isConfigured = false;
protected LoggerInterface $logger;
/**
* Twilio client.
*/
protected Client $twilioClient;
public function __construct(
CacheItemPoolInterface $cachePool,
$config,
LoggerInterface $logger
) {
$this->logger = $logger;
$this->cachePool = $cachePool;
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'])
&& strlen($config['twilio_secret']) > 2
) {
$this->twilioClient = new Client([
'auth' => [$config['twilio_sid'], $config['twilio_secret']],
]);
$this->isConfigured = true;
}
}
public function format($phonenumber)
{
return $this->performTwilioFormat($phonenumber);
}
/**
* Get type (mobile, landline, ...) for phone number.
*/
public function getType(string $phonenumber): string
{
return $this->performTwilioLookup($phonenumber) ?? 'unknown';
}
/**
* Return true if the validation is configured and available.
*/
public function isPhonenumberValidationConfigured(): bool
{
return $this->isConfigured;
}
/**
* Return true if the phonenumber is a landline or voip phone. Return always true
* if the validation is not configured.
*
* @param string $phonenumber
*/
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'], true);
}
/**
* Return true if the phonenumber is a landline or voip phone. Return always true
* if the validation is not configured.
*
* @param string $phonenumber
*/
public function isValidPhonenumberLandOrVoip($phonenumber): bool
{
if (false === $this->isPhonenumberValidationConfigured()) {
return true;
}
$validation = $this->performTwilioLookup($phonenumber);
if (null === $validation) {
return true;
}
return in_array($validation, ['landline', 'voip'], true);
}
/**
* REturn true if the phoennumber is a mobile phone. Return always true
* if the validation is not configured.
*
* @param string $phonenumber
*/
public function isValidPhonenumberMobile($phonenumber): bool
{
if (false === $this->isPhonenumberValidationConfigured()) {
return true;
}
$validation = $this->performTwilioLookup($phonenumber);
if (null === $validation) {
return true;
}
return 'mobile' === $validation;
}
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 '
. 'due to client error', [
'message' => $response->getBody()->getContents(),
'status_code' => $response->getStatusCode(),
'phonenumber' => $phonenumber,
]);
return $phonenumber;
} catch (ServerException $e) {
$response = $e->getResponse();
$this->logger->error('[phonenumber helper] Could not format number '
. 'due to server error', [
'message' => $response->getBody()->getContents(),
'status_code' => $response->getStatusCode(),
'phonenumber' => $phonenumber,
]);
return null;
} catch (ConnectException $e) {
$this->logger->error('[phonenumber helper] Could not format number '
. 'due to connect error', [
'message' => $e->getMessage(),
'phonenumber' => $phonenumber,
]);
return null;
}
$format = json_decode($response->getBody()->getContents())->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();
}
try {
$response = $this->twilioClient->get(sprintf(self::LOOKUP_URI, '+' . $filtered), [
'http_errors' => true,
'query' => [
'Type' => 'carrier',
],
]);
} catch (ClientException $e) {
return 'invalid';
} catch (ServerException $e) {
$response = $e->getResponse();
$this->logger->error('[phonenumber helper] Could not perform validation '
. 'due to server error', [
'message' => $response->getBody()->getContents(),
'status_code' => $response->getStatusCode(),
'phonenumber' => $phonenumber,
]);
return null;
} catch (ConnectException $e) {
$this->logger->error('[phonenumber helper] Could not format number '
. 'due to connect error', [
'message' => $e->getMessage(),
'phonenumber' => $phonenumber,
]);
return null;
}
$validation = json_decode($response->getBody()->getContents())->carrier->type;
$item
->set($validation)
// expires after 12h
->expiresAfter(3600 * 12);
$this->cachePool->save($item);
return $validation;
}
}

View File

@ -1,38 +0,0 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* @Annotation
*/
class PhonenumberConstraint extends Constraint
{
public $notLandlineMessage = 'This is not a landline phonenumber';
public $notMobileMessage = 'This is not a mobile phonenumber';
public $notValidMessage = 'This is not a valid phonenumber';
/**
* The type of phone: landline (not able to receive sms) or mobile (can receive sms).
*
* @var string 'landline', 'mobile' or 'any'
*/
public $type;
public function validatedBy()
{
return \Chill\MainBundle\Validation\Validator\ValidPhonenumber::class;
}
}

View File

@ -1,81 +0,0 @@
<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Validation\Validator;
use Chill\MainBundle\Phonenumber\PhonenumberHelper;
use LogicException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
class ValidPhonenumber extends ConstraintValidator
{
protected $logger;
/**
* @var PhonenumberHelper
*/
protected $phonenumberHelper;
public function __construct(
LoggerInterface $logger,
PhonenumberHelper $phonenumberHelper
) {
$this->phonenumberHelper = $phonenumberHelper;
$this->logger = $logger;
}
/**
* @param string $value
* @param \Chill\MainBundle\Validation\Constraint\PhonenumberConstraint $constraint
*/
public function validate($value, Constraint $constraint)
{
if (false === $this->phonenumberHelper->isPhonenumberValidationConfigured()) {
$this->logger->debug('[phonenumber] skipping validation due to not configured helper');
return;
}
if (empty($value)) {
return;
}
switch ($constraint->type) {
case 'landline':
$isValid = $this->phonenumberHelper->isValidPhonenumberLandOrVoip($value);
$message = $constraint->notLandlineMessage;
break;
case 'mobile':
$isValid = $this->phonenumberHelper->isValidPhonenumberMobile($value);
$message = $constraint->notMobileMessage;
break;
case 'any':
$isValid = $this->phonenumberHelper->isValidPhonenumberAny($value);
$message = $constraint->notValidMessage;
break;
default:
throw new LogicException(sprintf("This type '%s' is not implemented. "
. "Possible values are 'mobile', 'landline' or 'any'", $constraint->type));
}
if (false === $isValid) {
$this->context->addViolation($message, ['%phonenumber%' => $value]);
}
}
}

View File

@ -1,21 +0,0 @@
services:
_defaults:
autowire: true
autoconfigure: true
Chill\MainBundle\Phonenumber\PhonenumberHelper:
arguments:
$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:
$phonenumberHelper: '@Chill\MainBundle\Phonenumber\PhonenumberHelper'
tags:
- { name: validator.constraint_validator }