invoiceninja/app/Http/ValidationRules/User/HasValidPhoneNumber.php

79 lines
1.8 KiB
PHP
Raw Normal View History

2022-11-02 21:30:25 +11:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-29 09:21:40 +11:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2022-11-02 21:30:25 +11:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\ValidationRules\User;
use Illuminate\Contracts\Validation\Rule;
/**
* Class HasValidPhoneNumber.
*/
class HasValidPhoneNumber implements Rule
{
public $message;
public function __construct()
{
}
2023-02-16 12:36:09 +11:00
public function message()
{
2022-11-03 09:06:14 +11:00
return [
'phone' => ctrans('texts.phone_validation_error'),
];
}
2022-11-02 21:30:25 +11:00
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
2023-02-16 12:36:09 +11:00
$sid = config('ninja.twilio_account_sid');
$token = config('ninja.twilio_auth_token');
2023-02-16 12:36:09 +11:00
if (!$sid) {
return true;
}
2022-11-02 21:30:25 +11:00
2023-02-16 12:36:09 +11:00
if (is_null($value)) {
2022-11-07 07:44:19 +11:00
return false;
2023-02-16 12:36:09 +11:00
}
2022-11-07 07:44:19 +11:00
2023-02-16 12:36:09 +11:00
$twilio = new \Twilio\Rest\Client($sid, $token);
2022-11-02 21:30:25 +11:00
2023-02-16 12:36:09 +11:00
$country = auth()->user()->account?->companies()?->first()?->country();
2022-11-02 21:30:25 +11:00
2023-02-16 12:36:09 +11:00
if (!$country || strlen(auth()->user()->phone) < 2) {
return true;
}
2022-11-02 21:30:25 +11:00
2023-02-16 12:36:09 +11:00
$countryCode = $country->iso_3166_2;
2023-02-16 12:36:09 +11:00
try {
$phone_number = $twilio->lookups->v1->phoneNumbers($value)
->fetch(["countryCode" => $countryCode]);
2022-11-02 21:30:25 +11:00
$user = auth()->user();
request()->merge(['validated_phone' => $phone_number->phoneNumber ]);
2023-02-16 12:36:09 +11:00
$user->verified_phone_number = false;
2022-11-02 21:30:25 +11:00
$user->save();
return true;
2023-02-16 12:36:09 +11:00
} catch(\Exception $e) {
return false;
}
2022-11-02 21:30:25 +11:00
}
}