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

DutyHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 88.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 66
ccs 30
cts 34
cp 0.8824
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A generateList() 0 8 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 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