Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResendMultipleEmails
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 2
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 43
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     * Create a new command instance.
27     *
28     * @return void
29     */
30    public function __construct()
31    {
32        parent::__construct();
33    }
34
35    /**
36     * Execute the console command.
37     *
38     * @return int
39     */
40    public function handle()
41    {
42        try {
43
44            $type = (int) ($this->argument('type') ?? 0);
45            $userId = (int) ($this->argument('user_id') ?? 1);
46
47            $resendEmails = TblResendEmails::where('type', $type)->get();
48
49            if (count($resendEmails) > 0) {
50
51                if ($type == 3) {
52                    $resendEmails->chunk(100)->each(function ($chunk) {
53                        $userId = $this->argument('user_id') ?? 1;
54
55                        foreach ($chunk as $email) {
56
57                            $quoteId = $email->quotation_id;
58                            $phpBinary = '/usr/bin/php';
59                            $artisanPath = escapeshellarg(base_path('artisan'));
60                            $commandName = 'command:resend-acceptance-emails';
61
62                            $command = sprintf(
63                                '%s %s %s %d > /dev/null 2>&1 &',
64                                $phpBinary,
65                                $artisanPath,
66                                $commandName.' '.$quoteId,
67                                $userId
68                            );
69
70                            exec($command);
71                        }
72
73                        unset($chunk);
74                        gc_collect_cycles();
75                    });
76                } else {
77                    $resendEmails->chunk(100)->each(function ($chunk) use ($type) {
78
79                        foreach ($chunk as $email) {
80
81                            $quoteId = $email->quotation_id;
82                            $phpBinary = '/usr/bin/php';
83                            $artisanPath = escapeshellarg(base_path('artisan'));
84                            $commandName = 'command:resend-emails';
85
86                            $command = sprintf(
87                                '%s %s %s %d > /dev/null 2>&1 &',
88                                $phpBinary,
89                                $artisanPath,
90                                $commandName.' '.$quoteId,
91                                $type
92                            );
93
94                            exec($command);
95                        }
96
97                        unset($chunk);
98                        gc_collect_cycles();
99                    });
100                }
101            }
102
103        } catch (\Exception $e) {
104            Log::channel('resend_emails')->error('[ResendMultipleEmails] '.$e->getMessage());
105        }
106
107        return 0;
108    }
109}