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

DutyUsers::queryList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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
    public function recordNextEntry()
74
    {
75
        $nextUser = $this->list->toArray()[1]['user'];
76
        $this->duty->users()->updateExistingPivot($nextUser->id, ['last_worked' => Carbon::today()]);
77
    }
78
79
    /**
80
     * Get the current user in our database who is working the duty roster.
81
     *
82
     * @return DutyUsers
83
     */
84
    public function getLastWorked()
85
    {
86
        $this->lastWorked = $this->duty->users()->orderBy('duty_user.last_worked', 'DESC')->orderBy('last_name')->first();
87
88
        return $this;
89
    }
90
91
    /**
92
     * Get a list of all users for a specific duty sorted by the user's last name.
93
     *
94
     * @return DutyUsers
95
     */
96
    public function queryList()
97
    {
98
        $this->list = $this->duty->users()->orderBy('last_name')->get();
99
100
        return $this;
101
    }
102
103
    /**
104
     * Take our list of users and merge them with dates so that each user is assigned a duty date.
105
     *
106
     * @return DutyUsers
107
     */
108
    public function combineListWithDates()
109
    {
110
        $dates = (new DutyDates($this->duty))->getDates();
111
        $count = $this->list->count();
112
        $newList = new Collection();
113
114
        for ($i = 0; $i < $count; $i++) {
115
            $newList->push([
116
                'date' => $dates[$i],
117
                'user' => $this->list[$i],
118
            ]);
119
        }
120
        $this->list = $newList;
121
122
        return $this;
123
    }
124
125
    /**
126
     * Query a list of users who we swapped around and insert them into our current duty list of users.
127
     */
128
    public function insertFromDutySwap()
129
    {
130
        $dutySwaps = DutySwap::where('duty_id', $this->duty->id)
131
            ->where('imageable_type', 'SET\User')
132
            ->where('date', '>=', Carbon::now()->subMonth())  //Omit really old records.
133
            ->orderBy('date', 'ASC')
134
            ->get();
135
136
        foreach ($dutySwaps as $swap) {
137
            foreach ($this->list as $key => $entry) {
138
                if ($swap->date == $entry['date']) {
139
                    $this->list[$key] = [
140
                        'user' => $swap->imageable()->first(),
141
                        'date' => $entry['date'],
142
                    ];
143
                }
144
            }
145
        }
146
147
        return $this;
148
    }
149
}
150