Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResendMultipleEmails
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace App\Console\Commands;
4
5use App\Models\TblResendEmails;
6use Illuminate\Console\Command;
7use Illuminate\Support\Facades\Log;
8
9class ResendMultipleEmails extends Command
10{
11    /**
12     * The name and signature of the console command.
13     *
14     * @var string
15     */
16    protected $signature = 'command:resend-emails-bg {type?} {user_id?}';
17
18    /**
19     * The console command description.
20     *
21     * @var string
22     */
23    protected $description = 'Command description';
24
25    /**
26     * Execute the console command.
27     */
28    public function handle(): void
29    {
30        try {
31
32            $type = (int) ($this->argument('type') ?? 0);
33            $userId = (int) ($this->argument('user_id') ?? 1);
34
35            $resendEmails = TblResendEmails::where('type', $type)->get();
36
37            if (count($resendEmails) > 0) {
38                
39                if($type == 3){
40                    $resendEmails->chunk(100)->each(function ($chunk) use ($type): void {
41                        $userId = $this->argument('user_id') ?? 1;
42                        foreach ($chunk as $email) {
43
44                            $quoteId = $email->quotation_id;
45                            $phpBinary = '/usr/bin/php';
46                            $artisanPath = escapeshellarg(base_path('artisan'));
47                            $commandName = 'command:resend-acceptance-emails';
48
49                            $command = sprintf(
50                                '%s %s %s %d > /dev/null 2>&1 &',
51                                $phpBinary,
52                                $artisanPath,
53                                $commandName.' '.$quoteId,
54                                $userId
55                            );
56
57                            exec($command);
58                        }
59                        unset($chunk);
60                        gc_collect_cycles();
61                    });
62                }else{
63                    $resendEmails->chunk(100)->each(function ($chunk) use ($type): void {
64                    
65                        foreach ($chunk as $email) {
66
67                            $quoteId = $email->quotation_id;
68                            $phpBinary = '/usr/bin/php';
69                            $artisanPath = escapeshellarg(base_path('artisan'));
70                            $commandName = 'command:resend-emails';
71
72                            $command = sprintf(
73                                '%s %s %s %d > /dev/null 2>&1 &',
74                                $phpBinary,
75                                $artisanPath,
76                                $commandName.' '.$quoteId,
77                                $type
78                            );
79
80                            exec($command);
81                        }
82
83                        unset($chunk);
84                        gc_collect_cycles();
85                    });
86                }
87            }
88
89        } catch (\Exception $e) {
90            Log::channel('resend_emails')->error('[ResendMultipleEmails] '.$e->getMessage());
91        }
92    }
93}