ProcessMonday::handle()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 9
cp 0
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
use SET\User;
12
13
class ProcessMonday extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'emails:monday';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Process all commands for monday & send single email to Reporter';
28
29
    protected $classesToProcess = [
30
        'notes'     => SendReminders::class,
31
        'visits'    => ExpiringVisits::class,
32
        'records'   => RenewTraining::class,
33
    ];
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $mailArray = [];
43
44
        foreach ($this->classesToProcess as $key => $class) {
45
            $mailArray[$key] = (new $class())->handle()->getList();
46
        }
47
48
        $mailArray['dutyLists'] = $this->getDutyList();
49
50
        //Send FSO a summary email with all the lists we retrieved.
51
        $this->sendReporterEmail($mailArray);
52
    }
53
54
    private function sendReporterEmail($array)
55
    {
56
        $reportAddress = Setting::get('summary_recipient', null);
57
        $recipientEmails = User::whereIn('id', $reportAddress)->get()->pluck('email');
58
        if ($reportAddress !== null) {
59
            Mail::to($recipientEmails)->send(new EmailAdminSummary($array));
60
        }
61
    }
62
63
    /**
64
     * @return mixed
65
     */
66
    private function getDutyList()
67
    {
68
        $duties = Duty::all();
69
70
        return $duties->map(function ($item) {
71
            $userDateArray = (new DutyList($item))->emailOutput();
72
            $userDateArray->put('duty', $item);
73
74
            return $userDateArray;
75
        });
76
    }
77
}
78