Passed
Push — master ( a64e2a...e71307 )
by Dmitry
08:00
created

DayPeriod::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\charge\modifiers\addons;
12
13
use DateInterval;
14
use DateTimeImmutable;
15
16
/**
17
 * Day period addon
18
 *
19
 * @author Dmytro Naumenko <[email protected]>
20
 */
21
class DayPeriod extends Period
22
{
23
    public function countPeriodsPassed(DateTimeImmutable $since, DateTimeImmutable $time): float
24
    {
25
        $diff = $time->diff($since);
26
27
        return $diff->format('%r%a') / $this->value;
28
    }
29
30
    public function addTo(DateTimeImmutable $since): DateTimeImmutable
31
    {
32
        return $since->add(new DateInterval("P{$this->value}D"));
33
    }
34
35
    public function __toString(): string
36
    {
37
        return $this->value . ' day' . ($this->value > 1 ? 's' : '');
38
    }
39
}
40