DutyGroups::recordNextEntry()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 0
cts 8
cp 0
rs 9.9666
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 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
    /**
48
     * Get function for the list. List is stored on the helper class.
49
     *
50
     * @return Collection
51
     */
52
    public function getList()
53
    {
54
        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 54 which is incompatible with the return type documented by SET\Handlers\Duty\DutyGroups::getList of type Illuminate\Database\Eloquent\Collection.
Loading history...
55
    }
56
57 View Code Duplication
    public function recordNextEntry()
58
    {
59
        if ($this->list->count() < 2) {
60
            return;
61
        }
62
63
        $nextGroupID = $this->list->toArray()[1]['id'];
64
        $this->duty->groups()->updateExistingPivot($nextGroupID, ['last_worked' => Carbon::today()]);
65
    }
66
67
    public function getLastWorked()
68
    {
69
        $this->lastWorkedUser = $this->duty->groups()->orderBy('duty_group.last_worked', 'DESC')->first();
70
71
        return $this;
72
    }
73
74
    public function queryList()
75
    {
76
        $this->list = $this->duty->groups()->orderBy('name')->get();
77
78
        return $this;
79
    }
80
81
    /**
82
     * Take our list of groups and merge them with dates so that each group is assigned a duty date.
83
     *
84
     * @return DutyGroups
85
     */
86
    public function combineListWithDates()
87
    {
88
        $dates = (new DutyDates($this->duty))->getDates();
89
        $newDatesList = array_values(array_diff($dates, $this->swapDates->toArray()));
90
        $count = $this->list->count();
91
        $dateCounter = 0;
92
93
        for ($i = 0; $i < $count; $i++) {
94
            // Does list[i] already have date assigned? Is yes, skip assignment
95
            if (!empty($this->list[$i]['date'])) {
96
                continue;
97 View Code Duplication
            } else {
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...
98
                $this->list[$i] = [
99
                    'group' => $this->list[$i]['group'],
100
                    'id'    => $this->list[$i]['id'],
101
                    'date'  => $newDatesList[$dateCounter++],
102
                ];
103
            }
104
        }
105
        $this->list = $this->list->sortBy('date');
106
107
        return $this;
108
    }
109
110
    /**
111
     * Query a list of groups who we swapped around and insert them into our current duty list of groups.
112
     */
113
    public function insertFromDutySwap()
114
    {
115
        $dutySwaps = DutySwap::where('duty_id', $this->duty->id)
116
            ->where('imageable_type', 'SET\Group')
117
            ->where('date', '>=', Carbon::now()->subDays(6)) //Omit really old records.
118
            ->orderBy('date', 'ASC')
119
            ->get();
120
121
        $this->convertListToCollection();
122
        $this->swapDates = new Collection();
123
124
        foreach ($dutySwaps as $swap) {
125
            $key = key($this->list->where('id', $swap->imageable_id)->toArray());
0 ignored issues
show
Bug introduced by
The method where cannot be called on $this->list (of type array<integer|string,arr...,"id":"?","date":"?"}>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
126
            if (!is_null($key)) {
127
                $this->swapDates->push($swap->date);
128
                $this->list[$key] = [
129
                    'group' => $swap->imageable()->first()->users()->active()->get(),
130
                    'id'    => $swap->imageable()->first()->id,
131
                    'date'  => $swap->date,
132
                ];
133
            }
134
        }
135
136
        return $this;
137
    }
138
139
    /**
140
     * Convert the list of groups into a collection of group users, group id and date.
141
     */
142
    private function convertListToCollection()
143
    {
144
        $count = $this->list->count();
0 ignored issues
show
Bug introduced by
The method count cannot be called on $this->list (of type array<integer|string,arr...,"id":"?","date":"?"}>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
145
        $newList = new Collection();
146
        for ($i = 0; $i < $count; $i++) {
147
            $newList->push([
148
                'group' => $this->list[$i]->users()->active()->get(),
0 ignored issues
show
Bug introduced by
The method users cannot be called on $this->list[$i] (of type array<string,?,{"group":"?","id":"?","date":"?"}>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
149
                'id'    => $this->list[$i]->id,
150
                'date'  => '',
151
            ]);
152
        }
153
        $this->list = $newList;
154
    }
155
156
    /**
157
     * @param $entry
158
     *
159
     * @return string
160
     */
161
    private function buildHTMLUserRow($entry)
162
    {
163
        $row = '';
164
        foreach ($entry['group'] as $user) {
165
            if (Gate::allows('view')) {
166
                $row .= "<a href='".url('user', $user->id)."'>".$user->userFullName.'</a> & ';
167
            } else {
168
                $row .= $user->userFullName.' & ';
169
            }
170
        }
171
        $row = rtrim($row, '& ');
172
173
        return $row;
174
    }
175
}
176