Completed
Push — master ( 0984d0...c9941e )
by Shawn
02:33
created

DutyUsers::htmlOutput()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
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 DutyUsers extends DutyHelper
12
{
13
    public function __construct(Duty $duty)
14
    {
15
        parent::__construct($duty);
16
    }
17
18
    /**
19
     * Generate a collection to be used for our view 'duty.show'.
20
     *
21
     * @return Collection
22
     */
23
    public function htmlOutput()
24
    {
25
        $newCollection = new Collection();
26
27
        foreach ($this->list as $entry) {
28
            if (Gate::allows('view')) {
29
                $rowvalue = "<a href='".url('user', $entry['user']->id)."'>".$entry['user']->userFullName.'</a>';
30
            } else {
31
                $rowvalue = $entry['user']->userFullName;
32
            }
33
            $newCollection->push([
34
                'row'  => $rowvalue,
35
                'id'   => $entry['user']->id,
36
                'date' => $entry['date'],
37
            ]);
38
        }
39
40
        return $newCollection;
41
    }
42
43
    /**
44
     * Generate a collection to be used for emails. View is either emails.duty_future or emails.duty_today.
45
     *
46
     * @return \Illuminate\Support\Collection
47
     */
48
    public function emailOutput()
49
    {
50
        $collection = $this->list->map(function ($value) {
51
            return [
52
                'users' => new Collection([$value['user']]),
53
                'date'  => $value['date'],
54
            ];
55
        });
56
57
        return $collection;
58
    }
59
60
    /**
61
     * Get function for the list. List is stored on the helper class.
62
     *
63
     * @return Collection
64
     */
65
    public function getList()
66
    {
67
        return $this->list;
68
    }
69
70
    /**
71
     * Grab the next user to work the duty roster and record them in our database so they are the current worker.
72
     */
73 View Code Duplication
    public function recordNextEntry()
74
    {
75
        if ($this->list->count() < 2) {
76
            return;
77
        }
78
79
        $nextUser = $this->list->toArray()[1]['user'];
80
        $this->duty->users()->updateExistingPivot($nextUser->id, ['last_worked' => Carbon::today()]);
81
    }
82
83
    /**
84
     * Get the current user in our database who is working the duty roster.
85
     *
86
     * @return DutyUsers
87
     */
88
    public function getLastWorked()
89
    {
90
        $this->lastWorkedUser = $this->duty->users()->orderBy('duty_user.last_worked', 'DESC')->orderBy('last_name')->first();
91
92
        return $this;
93
    }
94
95
    /**
96
     * Get a list of all users for a specific duty sorted by the user's last name.
97
     *
98
     * @return DutyUsers
99
     */
100
    public function queryList()
101
    {
102
        $this->list = $this->duty->users()->orderBy('last_name')->get();
103
104
        return $this;
105
    }
106
107
    /**
108
     * Take our list of users and merge them with dates so that each user is assigned a duty date.
109
     *
110
     * @return DutyUsers
111
     */
112
    public function combineListWithDates()
113
    {
114
        $dates = (new DutyDates($this->duty))->getDates();
115
        $count = $this->list->count();
116
        $newList = new Collection();
117
118
        for ($i = 0; $i < $count; $i++) {
119
            $newList->push([
120
                'date' => $dates[$i],
121
                'user' => $this->list[$i],
122
            ]);
123
        }
124
        $this->list = $newList;
125
126
        return $this;
127
    }
128
129
    /**
130
     * Query a list of users who we swapped around and insert them into our current duty list of users.
131
     */
132
    public function insertFromDutySwap()
133
    {
134
        $dutySwaps = DutySwap::where('duty_id', $this->duty->id)
135
            ->where('imageable_type', 'SET\User')
136
            ->where('date', '>=', Carbon::now()->subMonth())  //Omit really old records.
137
            ->orderBy('date', 'ASC')
138
            ->get();
139
140
        foreach ($dutySwaps as $swap) {
141
            foreach ($this->list as $key => $entry) {
142
                if ($swap->date == $entry['date']) {
143
                    $this->list[$key] = [
144
                        'user' => $swap->imageable()->first(),
145
                        'date' => $entry['date'],
146
                    ];
147
                }
148
            }
149
        }
150
151
        return $this;
152
    }
153
}
154