Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 32 |
| SendMail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 2 |
110 | |
0.00% |
0 / 32 |
| __construct | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 7 |
|||
| handle | |
0.00% |
0 / 1 |
72 | |
0.00% |
0 / 25 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Mail\Jobs; |
| 4 | |
| 5 | use Exception; |
| 6 | use Qmp\Laravel\Mail\MailSender; |
| 7 | use Illuminate\Bus\Queueable; |
| 8 | use Illuminate\Queue\SerializesModels; |
| 9 | use Illuminate\Queue\InteractsWithQueue; |
| 10 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 11 | use Illuminate\Foundation\Bus\Dispatchable; |
| 12 | use Illuminate\Support\Facades\Log; |
| 13 | use Illuminate\Support\Facades\Mail; |
| 14 | use Illuminate\Support\Facades\Redis; |
| 15 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\CreateParentProcess; |
| 16 | use Qmp\Laravel\CommandsLaravel\Middleware\Library\Monitoring\UpdateParentProcess; |
| 17 | use Qmp\Laravel\ToolsLaravel\Traits\Timer; |
| 18 | |
| 19 | class SendMail extends \Jobiden implements ShouldQueue |
| 20 | { |
| 21 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Timer; |
| 22 | |
| 23 | /** |
| 24 | * Undocumented variable |
| 25 | * |
| 26 | * @var integer |
| 27 | */ |
| 28 | public $tries = 3; |
| 29 | |
| 30 | //public $timeout = 60; |
| 31 | |
| 32 | /** |
| 33 | * Undocumented variable |
| 34 | * |
| 35 | * @var [type] |
| 36 | */ |
| 37 | public $mailSender; |
| 38 | |
| 39 | /** |
| 40 | * Create a new job instance. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public function __construct(MailSender $mailSender) |
| 45 | { |
| 46 | $this->mailSender = $mailSender; |
| 47 | |
| 48 | if (env('MAIL_LOG_QUEUE', 1) === 1) { |
| 49 | $this->middleware = [ |
| 50 | CreateParentProcess::class, |
| 51 | UpdateParentProcess::class |
| 52 | ]; |
| 53 | } else { |
| 54 | $this->cronTableMiddleware = []; |
| 55 | } |
| 56 | |
| 57 | $this->onConnection('emails'); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Execute the job. |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public function handle() |
| 66 | { |
| 67 | if (env('MAIL_LOG_QUEUE', 1) === 1) { |
| 68 | $this->startTimer('total'); |
| 69 | |
| 70 | $data = $this->middlewareData(UpdateParentProcess::class) |
| 71 | ->transfert(CreateParentProcess::class, ['id']); |
| 72 | } |
| 73 | try { |
| 74 | |
| 75 | // Allow only 2 emails every 1 second |
| 76 | Redis::throttle('emails')->allow(1)->every(1)->then(function () { |
| 77 | |
| 78 | if ($this->mailSender->get('type') === 'smtp') { |
| 79 | $mail = Mail::to($this->mailSender->get('to')); |
| 80 | if ($this->mailSender->get('cc')) { |
| 81 | $mail->cc($this->mailSender->get('cc')); |
| 82 | } |
| 83 | if ($this->mailSender->get('bcc')) { |
| 84 | $mail->bcc($this->mailSender->get('bcc')); |
| 85 | } |
| 86 | |
| 87 | $mail->send($this->mailSender->get('content')); |
| 88 | } else { |
| 89 | // TODO Implement local Router |
| 90 | Log::debug('Send Email: ' . var_export($this->mailSender, true)); |
| 91 | Log::debug('Content Email: ' . var_export($this->mailSender->get('content')->render(), true)); |
| 92 | } |
| 93 | |
| 94 | }, function () { |
| 95 | // Could not obtain lock; this job will be re-queued |
| 96 | return $this->release(2); |
| 97 | }); |
| 98 | if (env('MAIL_LOG_QUEUE', 1) === 1) { |
| 99 | $data->log = [ |
| 100 | 'duration' => $this->getTimer('total'), |
| 101 | ]; |
| 102 | } |
| 103 | } catch (Exception $e) { |
| 104 | if (env('MAIL_LOG_QUEUE', 1) === 1) { |
| 105 | $data->error = $this->reportException($e); |
| 106 | } else { |
| 107 | Log::debug(var_export($e, true)); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | } |