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

DutyHelper   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 71
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generateList() 0 8 1
A getList() 0 4 1
A iterateList() 0 10 3
A sortList() 0 10 3
B readyToRecordNextEntry() 0 17 5
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