Passed
Push — master ( c30421...54de7b )
by Kauri
53s
created

PaymentPeriods::getPeriodsLengths()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Kauri\Loan;
6
7
/**
8
 * Class PaymentPeriods
9
 * @package Kauri\Loan
10
 */
11
class PaymentPeriods implements PaymentPeriodsInterface
12
{
13
    /**
14
     * @var array
15
     */
16
    private $periods = array();
17
    /**
18
     * @var int
19
     */
20
    private $averagePeriodLenght;
21
22
    /**
23
     * PaymentPeriods constructor.
24
     * @param int $averagePeriodLenght
25
     */
26 7
    public function __construct(int $averagePeriodLenght)
27
    {
28 7
        $this->averagePeriodLenght = $averagePeriodLenght;
29 7
    }
30
31
    /**
32
     * @param PeriodInterface $period
33
     * @param int|null $sequenceNo
34
     */
35 7
    public function add(PeriodInterface $period, int $sequenceNo = null): void
36
    {
37 7
        if (is_null($sequenceNo)) {
38 4
            $sequenceNo = $this->getNoOfPeriods() + 1;
39
        }
40
41 7
        $period->setSequenceNo($sequenceNo);
42 7
        $this->periods[$sequenceNo] = $period;
43 7
    }
44
45
    /**
46
     * @param int $calculationMode
47
     * @return array
48
     */
49 5
    public function getPeriodsLengths(int $calculationMode = self::CALCULATION_MODE_AVERAGE): array
50
    {
51 5
        $periodsLengths = array();
52
53
        /** @var PeriodInterface $period */
54 5
        foreach ($this->getPeriods() as $period) {
55 5
            $length = $period->getLength();
56
57 5
            if ($calculationMode == self::CALCULATION_MODE_AVERAGE)
58
            {
59 5
                $length = $this->averagePeriodLenght;
60
            }
61 5
            $periodsLengths[$period->getSequenceNo()] = $length;
62
        }
63
64 5
        return $periodsLengths;
65
    }
66
67
    /**
68
     * @return array
69
     */
70 7
    public function getPeriods(): array
71
    {
72 7
        return $this->periods;
73
    }
74
75
    /**
76
     * @return int
77
     */
78 4
    public function getNoOfPeriods(): int
79
    {
80 4
        return count($this->periods);
81
    }
82
}
83