DutyDates::buildDateArray()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 4
nop 0
crap 12
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