1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SET\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use Illuminate\Support\Facades\Mail; |
7
|
|
|
use SET\Duty; |
8
|
|
|
use SET\Handlers\Duty\DutyList; |
9
|
|
|
use SET\Mail\EmailAdminSummary; |
10
|
|
|
use SET\Setting; |
11
|
|
|
|
12
|
|
|
class ProcessMonday extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The name and signature of the console command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'emails:monday'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The console command description. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $description = 'Process all commands for monday & send single email to Reporter'; |
27
|
|
|
|
28
|
|
|
protected $classesToProcess = [ |
29
|
|
|
'notes' => SendReminders::class, |
30
|
|
|
'visits' => ExpiringVisits::class, |
31
|
|
|
'records' => RenewTraining::class, |
32
|
|
|
'destroyed' => DeleteUsers::class, |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function handle() |
41
|
|
|
{ |
42
|
|
|
foreach ($this->classesToProcess as $key => $class) { |
43
|
|
|
$mailArray[$key] = (new $class())->handle()->getList(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$mailArray['dutyLists'] = $this->getDutyList(); |
|
|
|
|
47
|
|
|
|
48
|
|
|
//Send FSO a summary email with all the lists we retrieved. |
49
|
|
|
$this->sendReporterEmail($mailArray); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function sendReporterEmail($array) |
53
|
|
|
{ |
54
|
|
|
$reportAddress = Setting::where('name', 'report_address')->first(); |
55
|
|
|
|
56
|
|
|
Mail::to($reportAddress->secondary, $reportAddress->primary)->send(new EmailAdminSummary($array)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
private function getDutyList() |
63
|
|
|
{ |
64
|
|
|
$duties = Duty::all(); |
65
|
|
|
|
66
|
|
|
return $duties->map(function ($item) { |
67
|
|
|
$userDateArray = (new DutyList($item))->emailOutput(); |
68
|
|
|
$userDateArray->put('duty', $item); |
69
|
|
|
|
70
|
|
|
return $userDateArray; |
71
|
|
|
}); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.