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

DayPeriod   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 17
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addTo() 0 3 1
A countPeriodsPassed() 0 5 1
A __toString() 0 3 2
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