DutyDates   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 51
ccs 0
cts 37
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getDates() 0 4 1
A buildDateArray() 0 14 3
A getStartDate() 0 10 3
A nextDate() 0 10 3
1
<?php
2
3
namespace SET\Handlers\Duty;
4
5
use Carbon\Carbon;
6
use SET\Duty;
7
8
class DutyDates
9
{
10
    private $duty;
11
12
    public function __construct(Duty $duty)
13
    {
14
        $this->duty = $duty;
15
    }
16
17
    public function getDates()
18
    {
19
        return $this->buildDateArray();
20
    }
21
22
    private function buildDateArray()
23
    {
24
        $array = [];
25
        $storeDate = $date = $this->getStartDate($this->duty->cycle);
26
27
        $count = $this->duty->has_groups ? $this->duty->groups()->get()->count() : $this->duty->users()->active()->get()->count();
28
29
        for ($i = 0; $i < $count; $i++) {
30
            $array[$i] = $storeDate->format('Y-m-d');
31
            $storeDate = $this->nextDate($this->duty->cycle, $date);
32
        }
33
34
        return $array;
35
    }
36
37
    private function getStartDate($cycle)
38
    {
39
        if ($cycle == 'monthly') {
40
            return Carbon::now()->startOfMonth();
41
        } elseif ($cycle == 'weekly') {
42
            return Carbon::now()->startOfWeek();
43
        }
44
45
        return Carbon::now()->startOfDay();
46
    }
47
48
    private function nextDate($cycle, Carbon $date)
49
    {
50
        if ($cycle == 'monthly') {
51
            return $date->addMonth();
52
        } elseif ($cycle == 'daily') {
53
            return $date->addDay();
54
        }
55
56
        return $date->addWeek();
57
    }
58
}
59