WeekInterval::getBusinessDays()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DavisPeixoto\ReportDates;
4
5
use DateInterval;
6
use DateTime;
7
use Exception;
8
9
class WeekInterval
10
{
11
    /**
12
     * @var DateTime $startDate
13
     */
14
    private $startDate;
15
16
    /**
17
     * @var DateTime $endDate
18
     */
19
    private $endDate;
20
21
    /**
22
     * @var DatesConfig $config
23
     */
24
    private $config;
25
26
    /**
27
     * WeekInterval constructor.
28
     *
29
     * @param DateTime $startDate
30
     * @param DateTime $endDate
31
     * @param DatesConfig $config
32
     */
33 6
    public function __construct(DateTime $startDate, DateTime $endDate, DatesConfig $config)
34
    {
35 6
        $this->startDate = $startDate;
36 6
        $this->endDate = $endDate;
37 6
        $this->config = $config;
38 6
    }
39
40
    /**
41
     * @return DateTime
42
     */
43
    public function getStartDate(): DateTime
44
    {
45
        return $this->startDate;
46
    }
47
48
    /**
49
     * @return DateTime
50
     */
51
    public function getEndDate(): DateTime
52
    {
53
        return $this->endDate;
54
    }
55
56
    /**
57
     * @return int
58
     */
59 6
    public function getBusinessDays(): int
60
    {
61 6
        return $this->countBusinessDays();
62
    }
63
64
    /**
65
     * @return int
66
     */
67 6
    public function getTotalDays(): int
68
    {
69 6
        return (int)$this->endDate->diff($this->startDate)->format('%a') + 1;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getWeekNumber(): string
76
    {
77
        return $this->endDate->format('W');
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function getYearWeek(): string
84
    {
85
        return $this->endDate->format('oW');
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getYearMonth(): string
92
    {
93
        return $this->endDate->format('Ym');
94
    }
95
96 6
    private function countBusinessDays()
97
    {
98 6
        $startDate = clone $this->startDate;
99 6
        $count = 0;
100
101 6
        while ($startDate <= $this->endDate) {
102 6
            if ($this->isBusinessDay($this->makeDayNumber($startDate))) {
0 ignored issues
show
Bug introduced by
It seems like $this->makeDayNumber($startDate) targeting DavisPeixoto\ReportDates...terval::makeDayNumber() can also be of type boolean; however, DavisPeixoto\ReportDates...terval::isBusinessDay() does only seem to accept object<DavisPeixoto\ReportDates\DayNumber>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103 6
                $count++;
104
            }
105
106 6
            $startDate = $this->incrementDay($startDate);
107
        }
108
109 6
        return $count;
110
    }
111
112
    /**
113
     * @param DayNumber $day
114
     * @return bool
115
     */
116 6
    private function isBusinessDay(DayNumber $day)
117
    {
118 6
        foreach ($this->config->getBusinessDays() as $businessDay) {
119 6
            if ($day->equals($businessDay)) {
120 6
                return true;
121
            }
122
        }
123
124 6
        return false;
125
    }
126
127
    /**
128
     * @param DateTime $day
129
     * @return bool|DayNumber
130
     */
131 6
    private function makeDayNumber(DateTime $day)
132
    {
133 6
        $values = DayNumber::values();
134 6
        foreach ($values as $dayNumber) {
135 6
            if ($dayNumber->getValue() === $day->format('w')) {
136 6
                return $dayNumber;
137
            }
138
        }
139
140
        return false;
141
    }
142
143
    /**
144
     * @param DateTime $date
145
     * @return DateTime
146
     * @throws Exception
147
     */
148 6
    private function incrementDay(DateTime $date)
149
    {
150 6
        $oneDay = new DateInterval('P1D');
151 6
        $oneHour = new DateInterval('PT1H');
152
153 6
        $date->add($oneDay);
154
155 6
        if ($date->format('h') === '01') {
156
            $date->sub($oneHour);
157
        }
158
159 6
        if ($date->format('h') === '23') {
160
            $date->add($oneHour);
161
        }
162
163 6
        return $date;
164
    }
165
}
166