2020-05-16 04:04:24 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs\Mail;
|
|
|
|
|
|
|
|
use App\Libraries\Google\Google;
|
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Providers\MailServiceProvider;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
|
|
|
|
class BaseMailerJob implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
|
2020-05-16 12:26:16 +02:00
|
|
|
public function setMailDriver(string $driver)
|
2020-05-16 04:04:24 +02:00
|
|
|
{
|
|
|
|
switch ($driver) {
|
|
|
|
case 'default':
|
|
|
|
break;
|
|
|
|
case 'gmail':
|
|
|
|
$this->setGmailMailer();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-16 12:26:16 +02:00
|
|
|
public function setGmailMailer()
|
2020-05-16 04:04:24 +02:00
|
|
|
{
|
|
|
|
$sending_user = $this->entity->client->getSetting('gmail_sending_user_id');
|
|
|
|
|
|
|
|
$user = User::find($sending_user);
|
|
|
|
|
|
|
|
$google = (new Google())->init();
|
2020-05-16 13:13:32 +02:00
|
|
|
$google->getClient()->setAccessToken(json_encode($user->oauth_user_token));
|
2020-05-16 04:04:24 +02:00
|
|
|
|
2020-05-16 12:26:16 +02:00
|
|
|
if ($google->getClient()->isAccessTokenExpired()) {
|
2020-05-16 04:04:24 +02:00
|
|
|
$google->refreshToken($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now that our token is refresh and valid we can boot the
|
|
|
|
* mail driver at runtime and also set the token which will persist
|
|
|
|
* just for this request.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Config::set('mail.driver', 'gmail');
|
2020-05-16 13:13:32 +02:00
|
|
|
Config::set('services.gmail.token', $user->oauth_user_token->access_token);
|
2020-05-16 12:26:16 +02:00
|
|
|
|
2020-05-16 04:04:24 +02:00
|
|
|
(new MailServiceProvider(app()))->register();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|