Completed
Push — master ( c9941e...0c7698 )
by Shawn
02:47
created

DutyHelper::readyToRecordNextEntry()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 8.8571
cc 5
eloc 12
nc 5
nop 1
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
    public function __construct(Duty $duty)
18
    {
19
        $this->duty = $duty;
20
        $this->generateList();
21
    }
22
23
    public function generateList()
24
    {
25
        $this->queryList()
26
            ->getLastWorked()
27
            ->sortList()
28
            ->combineListWithDates()
29
            ->insertFromDutySwap();
30
    }
31
32
    public function getList()
33
    {
34
        return $this->list;
35
    }
36
37
    public function iterateList()
38
    {
39
        $today = Carbon::today()->format('Y-m-d');
40
41
        if (isset($this->lastWorkedUser->pivot->last_worked) && $today == $this->lastWorkedUser->pivot->last_worked) {
42
            return;
43
        }
44
45
        $this->readyToRecordNextEntry($today);
46
    }
47
48
    public function sortList()
49
    {
50
        if (!is_null($this->lastWorkedUser)) {
51
            while ($this->list->first()->id != $this->lastWorkedUser->id) {
52
                $this->list->push($this->list->shift());
53
            }
54
        }
55
56
        return $this;
57
    }
58
59
    /**
60
     * Check duty cycle. Then determine if time to record next entry.
61
     *
62
     * @param $today
63
     */
64
    private function readyToRecordNextEntry($today)
65
    {
66
        switch ($this->duty->cycle) {
67
            case 'daily':
68
                $this->recordNextEntry();
1 ignored issue
show
Bug introduced by
The method recordNextEntry() does not exist on SET\Handlers\Duty\DutyHelper. Did you maybe mean readyToRecordNextEntry()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
69
                break;
70
            case 'weekly':
71
                if (Carbon::today()->startOfWeek()->format('Y-m-d') == $today) {
72
                    $this->recordNextEntry();
1 ignored issue
show
Bug introduced by
The method recordNextEntry() does not exist on SET\Handlers\Duty\DutyHelper. Did you maybe mean readyToRecordNextEntry()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
73
                }
74
                break;
75
            default: //monthly
76
                if (Carbon::today()->startOfMonth()->format('Y-m-d') == $today) {
77
                    $this->recordNextEntry();
1 ignored issue
show
Bug introduced by
The method recordNextEntry() does not exist on SET\Handlers\Duty\DutyHelper. Did you maybe mean readyToRecordNextEntry()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
78
                }
79
        }
80
    }
81
}
82