diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index fa561b7e7..cfeb1530e 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -162,7 +162,6 @@ class ChillMainExtension extends Extension implements $loader->load('services/notification.yaml'); $loader->load('services/redis.yaml'); $loader->load('services/command.yaml'); - $loader->load('services/phonenumber.yaml'); $loader->load('services/cache.yaml'); $loader->load('services/templating.yaml'); $loader->load('services/timeline.yaml'); diff --git a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php b/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php deleted file mode 100644 index 0eae018a8..000000000 --- a/src/Bundle/ChillMainBundle/Phonenumber/PhonenumberHelper.php +++ /dev/null @@ -1,276 +0,0 @@ -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; - } -} diff --git a/src/Bundle/ChillMainBundle/Validation/Constraint/PhonenumberConstraint.php b/src/Bundle/ChillMainBundle/Validation/Constraint/PhonenumberConstraint.php deleted file mode 100644 index 27fbe2f50..000000000 --- a/src/Bundle/ChillMainBundle/Validation/Constraint/PhonenumberConstraint.php +++ /dev/null @@ -1,38 +0,0 @@ -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]); - } - } -} diff --git a/src/Bundle/ChillMainBundle/config/services/phonenumber.yaml b/src/Bundle/ChillMainBundle/config/services/phonenumber.yaml deleted file mode 100644 index 46fa853ee..000000000 --- a/src/Bundle/ChillMainBundle/config/services/phonenumber.yaml +++ /dev/null @@ -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 }