Completed
Push — Feature-#16934 ( 9c7b40 )
by D.
05:27 queued 01:31
created

DutyHelper::sortList()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 5
nc 2
nop 0
crap 3
1
<?php
2
3
namespace SET\Handlers\Duty;
4
5
use Carbon\Carbon;
6
use SET\Duty;
7
8
/**
9
 * Class DutyBase.
10
 */
11
class DutyHelper
12
{
13
    public $list;
14
    public $lastWorkedUser = null;
15
    public $duty;
16
17 12
    public function __construct(Duty $duty)
18
    {
19 12
        $this->duty = $duty;
20 12
        $this->generateList();
21 12
    }
22
23 12
    public function generateList()
24
    {
25 12
        $this->queryList()
26 12
            ->getLastWorked()
27 12
            ->sortList()
28 12
            ->combineListWithDates()
29 12
            ->insertFromDutySwap();
30 12
    }
31
32 4
    public function iterateList()
33
    {
34 4
        $today = Carbon::today()->format('Y-m-d');
35
36 4
        if (isset($this->lastWorkedUser->pivot->last_worked) && $today == $this->lastWorkedUser->pivot->last_worked) {
37
            return;
38
        }
39
40 4
        $this->readyToRecordNextEntry($today);
41 4
    }
42
43 12
    public function sortList()
44
    {
45 12
        if (!is_null($this->lastWorkedUser)) {
46 11
            while ($this->list->first()->id != $this->lastWorkedUser->id) {
47 4
                $this->list->push($this->list->shift());
48
            }
49
        }
50
51 12
        return $this;
52
    }
53
54
    /**
55
     * Check duty cycle. Then determine if time to record next entry.
56
     *
57
     * @param string $today
58
     */
59 4
    private function readyToRecordNextEntry($today)
60
    {
61 4
        switch ($this->duty->cycle) {
62 4
            case 'daily':
63 1
                $this->recordNextEntry();
64 1
                break;
65 3
            case 'monthly':
66
                if (Carbon::today()->startOfMonth()->format('Y-m-d') == $today) {
67
                    $this->recordNextEntry();
68
                }
69
                break;
70
            default: //weekly
71 3
                if (Carbon::today()->startOfWeek()->format('Y-m-d') == $today) {
72
                    $this->recordNextEntry();
73
                }
74
        }
75 4
    }
76
}
77