Completed
Pull Request — master (#6)
by Shawn
03:12
created

DutyGroups::insertFromDutySwap()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 0
1
<?php
2
3
namespace SET\Handlers\Duty;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Support\Facades\Gate;
8
use SET\Duty;
9
use SET\DutySwap;
10
11
class DutyGroups extends DutyHelper
12
{
13
    public function __construct(Duty $duty)
14
    {
15
        parent::__construct($duty);
16
    }
17
18
    public function HTMLOutput()
19
    {
20
        $newCollection = new Collection();
21
22
        foreach ($this->list as $entry) {
23
            $row = $this->buildHTMLUserRow($entry);
24
25
            $newCollection->push([
26
                'row'  => $row,
27
                'id'   => $entry['id'],
28
                'date' => $entry['date'],
29
            ]);
30
        }
31
32
        return $newCollection;
33
    }
34
35
    public function emailOutput()
36
    {
37
        $collection = $this->list->map(function ($value) {
38
            return [
39
                'users' => $value['group'],
40
                'date'  => $value['date'],
41
            ];
42
        });
43
44
        return $collection;
45
    }
46
47
    public function recordNextEntry()
48
    {
49
        $nextGroupID = $this->list->toArray()[1]['id'];
50
        $this->duty->groups()->updateExistingPivot($nextGroupID, ['last_worked' => Carbon::today()]);
51
    }
52
53
    public function getLastWorked()
54
    {
55
        $this->lastWorked = $this->duty->groups()->orderBy('duty_group.last_worked', 'DESC')->first();
56
57
        return $this;
58
    }
59
60
    public function queryList()
61
    {
62
        $this->list = $this->duty->groups()->orderBy('name')->get();
63
64
        return $this;
65
    }
66
67
    public function combineListWithDates()
68
    {
69
        $dates = (new DutyDates($this->duty))->getDates();
70
        $newList = new Collection();
71
        $count = $this->list->count();
72
73
        for ($i = 0; $i < $count; $i++) {
74
            $newList->push([
75
                'date'  => $dates[$i],
76
                'group' => $this->list[$i]->users()->get(),
77
                'id'    => $this->list[$i]->id,
78
            ]);
79
        }
80
81
        $this->list = $newList;
82
83
        return $this;
84
    }
85
86
    public function insertFromDutySwap()
87
    {
88
        $dutySwaps = DutySwap::where('duty_id', $this->duty->id)
89
            ->where('imageable_type', 'SET\Group')
90
            ->where('date', '>=', Carbon::now()->subMonth()) //Omit really old records.
91
            ->orderBy('date', 'ASC')
92
            ->get();
93
94
        foreach ($dutySwaps as $swap) {
95
            foreach ($this->list as $key => $entry) {
96
                if ($swap->date == $entry['date']) {
97
                    $this->list[$key] = [
98
                        'group' => $swap->imageable()->first()->users()->get(),
99
                        'id'    => $swap->imageable()->first()->id,
100
                        'date'  => $entry['date'],
101
                    ];
102
                }
103
            }
104
        }
105
    }
106
107
    /**
108
     * @param $entry
109
     *
110
     * @return string
111
     */
112
    private function buildHTMLUserRow($entry)
113
    {
114
        $row = '';
115
        foreach ($entry['group'] as $user) {
116
            if (Gate::allows('view')) {
117
                $row .= "<a href='".url('user', $user->id)."'>".$user->userFullName.'</a> & ';
118
            } else {
119
                $row .= $user->userFullName.' & ';
120
            }
121
        }
122
        $row = rtrim($row, '& ');
123
124
        return $row;
125
    }
126
}
127