Completed
Push — master ( cbc4d3...9920f2 )
by Sheela
08:14
created

DutyUsers::convertListToCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
            $rowvalue = $entry['user']->userFullName;
29
30
            if (Gate::allows('view')) {
31
                $rowvalue = "<a href='".url('user', $entry['user']->id)."'>".$entry['user']->userFullName.'</a>';
32
            }
33
34
            $newCollection->push([
35
                'row'  => $rowvalue,
36
                'id'   => $entry['user']->id,
37
                'date' => $entry['date'],
38
            ]);
39
        }
40
        
41
        return $newCollection;
42
    }
43
44
    /**
45
     * Generate a collection to be used for emails. View is either emails.duty_future or emails.duty_today.
46
     *
47
     * @return \Illuminate\Support\Collection
48
     */
49
    public function emailOutput()
50
    {
51
        $collection = $this->list->map(function ($value) {
52
            return [
53
                'users' => new Collection([$value['user']]),
54
                'date'  => $value['date'],
55
            ];
56
        });
57
58
        return $collection;
59
    }
60
61
    /**
62
     * Get function for the list. List is stored on the helper class.
63
     *
64
     * @return Collection
65
     */
66
    public function getList()
67
    {
68
        return $this->list;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->list; of type array<*,array>|Illuminat...ase\Eloquent\Collection adds the type array<*,array> to the return on line 68 which is incompatible with the return type documented by SET\Handlers\Duty\DutyUsers::getList of type Illuminate\Database\Eloquent\Collection.
Loading history...
69
    }
70
71
    /**
72
     * Grab the next user to work the duty roster and record them in our database so they are the current worker.
73
     */
74 View Code Duplication
    public function recordNextEntry()
75
    {
76
        if ($this->list->count() < 2) {
77
            return;
78
        }
79
80
        $nextUser = $this->list->toArray()[1]['user'];
81
        $this->duty->users()->updateExistingPivot($nextUser->id, ['last_worked' => Carbon::today()]);
82
    }
83
84
    /**
85
     * Get the current user in our database who is working the duty roster.
86
     *
87
     * @return DutyUsers
88
     */
89
    public function getLastWorked()
90
    {
91
        $this->lastWorkedUser = $this->duty->users()->orderBy('duty_user.last_worked', 'DESC')->orderBy('last_name')->first();
92
93
        return $this;
94
    }
95
96
    /**
97
     * Get a list of all users for a specific duty sorted by the user's last name.
98
     *
99
     * @return DutyUsers
100
     */
101
    public function queryList()
102
    {
103
        $this->list = $this->duty->users()->active()->orderBy('last_name')->get();
104
105
        return $this;
106
    }
107
108
     /**
109
     * Take our list of users and merge them with dates so that each user is assigned a duty date.
110
     *
111
     * @return DutyUsers
112
     */
113
    public function combineListWithDates()
114
    {
115
        $dates = (new DutyDates($this->duty))->getDates();        
116
        $newDatesList = array_values(array_diff($dates, $this->swapDates->toArray())); 
117
        
118
        $count = $this->list->count();
119
        $dateCounter = 0;
120
        for ($i = 0; $i < $count; $i++) {
121
            // Does list[i] already have date assigned? Is yes, skip assignment
122 View Code Duplication
            if (!empty($this->list[$i]['date'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
                continue;
124
            } else {
125
                $this->list[$i] = [
126
                    'date' => $newDatesList[$dateCounter++],
127
                    'user' => $this->list[$i]['user']
128
                ];             
129
            }
130
        }        
131
        $this->list = $this->list->sortBy('date');
132
        return $this;
133
    }
134
    
135
    /**
136
     * Query a list of users who we swapped around and insert them into our current duty list of users.
137
     */
138
    public function insertFromDutySwap()
139
    {
140
        $dutySwaps = DutySwap::where('duty_id', $this->duty->id)
141
            ->where('imageable_type', 'SET\User')
142
            ->where('date', '>=', Carbon::now()->subMonth())  //Omit really old records.
143
            ->orderBy('date', 'ASC')
144
            ->get();
145
146
        $this->convertListToCollection();
147
           
148
        $this->swapDates = new Collection();
149
        foreach ($dutySwaps as $swap) {
150
            $key = key($this->list->pluck('user')->where('id', $swap->imageable_id)->toArray());
151
            if (! is_null($key)) {
152
                $this->swapDates->push($swap->date);
153
                $this->list[$key] = [
154
                        'date' => $swap->date,
155
                        'user' => $swap->imageable()->first(),                        
156
                ];
157
            }
158
        }
159
        return $this;
160
    }
161
    
162
    /**
163
     * Convert the list of users into a collection of date and users
164
     */
165
    private function convertListToCollection()
166
    {
167
        $count = $this->list->count();
168
        $newList = new Collection();
169
        for ($i = 0; $i < $count; $i++) {
170
            $newList->push([
171
                'date' => '',
172
                'user' => $this->list[$i],
173
            ]);
174
        }
175
        $this->list = $newList;
176
    }
177
}
178