Completed
Push — master ( 6374b3...243f6e )
by D.
17:36 queued 12:41
created

DutyHelper::readyToRecordNextEntry()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.3906

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 9
cts 12
cp 0.75
rs 8.8571
cc 5
eloc 12
nc 5
nop 1
crap 5.3906
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 5
                $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 3
                    $this->recordNextEntry();
73
                }
74
        }
75 4
    }
76
}
77