DutyHelper::readyToRecordNextEntry()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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