1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/Jobs/User/VerifyPhone.php

84 lines
2.0 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\User;
use App\DataMapper\CompanySettings;
use App\DataMapper\DefaultSettings;
use App\Events\User\UserWasCreated;
use App\Libraries\MultiDB;
use App\Models\User;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class VerifyPhone implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesHash;
/**
* Create a new job instance.
*
* @param User $user
*/
2022-11-02 11:36:57 +01:00
public function __construct(private User $user){}
/**
* Execute the job.
*
* @return User|null
*/
public function handle() : void
{
2022-11-02 11:36:57 +01:00
MultiDB::checkUserEmailExists($this->user->email);
$sid = config('ninja.twilio_account_sid');
$token = config('ninja.twilio_auth_token');
if(!$sid)
return; // no twilio api credentials provided, bail.
2022-11-03 07:01:58 +01:00
$twilio = new \Twilio\Rest\Client($sid, $token);
2022-11-02 11:36:57 +01:00
$country = $this->user->account?->companies()?->first()?->country();
2022-11-02 11:36:57 +01:00
if(!$country || strlen($this->user->phone) < 2)
return;
$countryCode = $country->iso_3166_2;
try{
2022-11-02 11:36:57 +01:00
$phone_number = $twilio->lookups->v1->phoneNumbers($this->user->phone)
->fetch(["countryCode" => $countryCode]);
}
catch(\Exception $e) {
2022-11-02 11:36:57 +01:00
$this->user->verified_phone_number = false;
$this->user->save();
2022-11-06 21:44:19 +01:00
return;
}
if($phone_number && strlen($phone_number->phoneNumber) > 1)
{
2022-11-02 11:36:57 +01:00
$this->user->phone = $phone_number->phoneNumber;
$this->user->verified_phone_number = true;
$this->user->save();
}
}
}