Schedule::generatePeriods()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 3
1
<?php
2
/**
3
 * @author: Vova Lando <[email protected]>
4
 * @package: LoanPaymentsCalculator
5
 * @subpackage:
6
 * @created: 14/06/2017 16:31
7
 */
8
namespace cog\LoanPaymentsCalculator\Schedule;
9
10
use cog\LoanPaymentsCalculator\DateProvider\DateProvider;
11
use cog\LoanPaymentsCalculator\Period\Period;
12
13
class Schedule
14
{
15
    /**
16
     * @var \DateTime
17
     */
18
    private $scheduleStartDate;
19
20
    /**
21
     * @var int
22
     */
23
    private $numberOfPeriods;
24
25
    /**
26
     * @var DateProvider
27
     */
28
    private $dateProvider;
29
30 5
    public function __construct(\DateTime $scheduleStartDate, $numberOfPeriods, DateProvider $dateProvider)
31
    {
32 5
        $this->scheduleStartDate = $scheduleStartDate;
33 5
        $this->numberOfPeriods = $numberOfPeriods;
34 5
        $this->dateProvider = $dateProvider;
35 5
    }
36
37 5
    public function generatePeriods()
38
    {
39 5
        $periods = [];
40 5
        for ($i = 0; $i < $this->numberOfPeriods; $i++) {
41 5
            $startDate = $i === 0 ? $this->scheduleStartDate : $periods[$i - 1]->endDate;
42 5
            $endDate = $this->dateProvider->calculate($startDate);
43 5
            $periods[$i] = new Period($startDate, $endDate);
44
        }
45
46 5
        return $periods;
47
    }
48
}
49