DateProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 76
ccs 16
cts 20
cp 0.8
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A calculate() 0 11 3
A getNextBusinessDay() 0 8 2
A getPreviousBusinessDay() 0 8 2
1
<?php
2
3
/**
4
 * @author: Vova Lando <[email protected]>
5
 * @package: LoanPaymentsCalculator
6
 * @subpackage: DateProvider
7
 * @created: 14/06/2017 14:55
8
 */
9
namespace cog\LoanPaymentsCalculator\DateProvider;
10
11
use cog\LoanPaymentsCalculator\DateProvider\DateDetermineStrategy\DateDetermineStrategyInterface;
12
use cog\LoanPaymentsCalculator\DateProvider\HolidayProvider\HolidayProvider;
13
14
class DateProvider
15
{
16
    /**
17
     * @var DateDetermineStrategyInterface
18
     */
19
    private $dateDetermineStrategy;
20
21
    /**
22
     * @var HolidayProvider
23
     */
24
    private $holidayProvider;
25
26
    /**
27
     * @var bool
28
     */
29
    private $shiftForward;
30
31
    /**
32
     * DateProvider constructor.
33
     *
34
     * @param DateDetermineStrategyInterface $dateDetermineStrategy
35
     * @param HolidayProvider                $holidayProvider
36
     * @param bool                           $shiftForward
37
     */
38 5
    public function __construct(DateDetermineStrategyInterface $dateDetermineStrategy, HolidayProvider $holidayProvider, $shiftForward)
39
    {
40 5
        $this->dateDetermineStrategy = $dateDetermineStrategy;
41 5
        $this->holidayProvider = $holidayProvider;
42 5
        $this->shiftForward = $shiftForward;
43 5
    }
44
45
    /**
46
     * @param \DateTime $startDate
47
     *
48
     * @return \DateTime
49
     */
50 5
    public function calculate(\DateTime $startDate)
51
    {
52 5
        $calculatedDate = $this->dateDetermineStrategy->calculateNextDate($startDate);
53 5
        if ($this->holidayProvider->isHoliday($calculatedDate)) {
54 3
            $calculatedDate = $this->shiftForward ?
55 3
                $this->getNextBusinessDay($calculatedDate) :
56 3
                $this->getPreviousBusinessDay($calculatedDate);
57
        }
58
59 5
        return $calculatedDate;
60
    }
61
62
    /**
63
     * @param \DateTime $date
64
     *
65
     * @return \DateTime
66
     */
67 3
    private function getNextBusinessDay(\DateTime $date)
68
    {
69
        do {
70 3
            $date->modify('+1 day');
71 3
        } while ($this->holidayProvider->isHoliday($date));
72
73 3
        return $date;
74
    }
75
76
    /**
77
     * @param \DateTime $date
78
     *
79
     * @return \DateTime
80
     */
81
    private function getPreviousBusinessDay(\DateTime $date)
82
    {
83
        do {
84
            $date->modify('-1 day');
85
        } while ($this->holidayProvider->isHoliday($date));
86
87
        return $date;
88
    }
89
}
90