Time::addTime()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Arbor\Moment;
6
7
use Arbor\Moment\Exception\InvalidArgumentException;
8
use Arbor\Moment\Formatter\TimeFormatter;
9
10
/**
11
 * Class Time
12
 *
13
 * @package Arbor\Moment
14
 * @author Igor Vuckovic <[email protected]>
15
 */
16
class Time
17
{
18
    /** @var int */
19
    private $seconds;
20
21
    /** @var TimeFormatter */
22
    private $formatter;
23
24
    /**
25
     * @param int $seconds
26
     */
27 42
    private function __construct(int $seconds)
28
    {
29 42
        $this->seconds = $seconds;
30 42
        $this->formatter = new TimeFormatter($this);
31 42
    }
32
33
    /**
34
     * @param int $seconds
35
     * @return Time
36
     */
37 18
    public static function fromSeconds(int $seconds) : Time
38
    {
39 18
        return new self($seconds);
40
    }
41
42
    /**
43
     * @param string $time
44
     * @return Time
45
     * @throws InvalidArgumentException
46
     */
47 29
    public static function fromString(string $time) : Time
48
    {
49 29
        if (preg_match('/^(\d+):(\d{2})$/', $time, $matches)) {
50 15
            $seconds = ($matches[1] * 3600) + ($matches[2] * 60);
51 14
        } elseif (preg_match('/^(\d+):(\d{2}):(\d{2})$/', $time, $matches)) {
52 12
            $seconds = ($matches[1] * 3600) + ($matches[2] * 60) + $matches[3];
53
        } else {
54 2
            throw InvalidArgumentException::invalidTimeFormat($time);
55
        }
56
57 27
        return new self($seconds);
58
    }
59
60
    /**
61
     * @return int
62
     */
63 25
    public function getHours() : int
64
    {
65 25
        return (int)floor($this->seconds / 3600);
66
    }
67
68
    /**
69
     * @return int
70
     */
71 22
    public function getMinutes() : int
72
    {
73 22
        return (int)floor(($this->seconds - ($this->getHours() * 3600)) / 60);
74
    }
75
76
    /**
77
     * @return int
78
     */
79 9
    public function getSeconds() : int
80
    {
81 9
        return $this->seconds - ($this->getHours() * 3600) - ($this->getMinutes() * 60);
82
    }
83
84
    /**
85
     * @return int
86
     */
87 3
    public function getTotalHours() : int
88
    {
89 3
        return (int)floor($this->seconds / 3600);
90
    }
91
92
    /**
93
     * @return int
94
     */
95 3
    public function getTotalMinutes() : int
96
    {
97 3
        return (int)floor($this->seconds / 60);
98
    }
99
100
    /**
101
     * @return int
102
     */
103 10
    public function getTotalSeconds() : int
104
    {
105 10
        return $this->seconds;
106
    }
107
108
    /**
109
     * @return string
110
     */
111 10
    public function getShort() : string
112
    {
113 10
        return $this->formatter->getShort();
114
    }
115
116
    /**
117
     * @return string
118
     */
119 6
    public function getLong() : string
120
    {
121 6
        return $this->formatter->getLong();
122
    }
123
124
    /**
125
     * @param Time $time
126
     * @return Time
127
     */
128 4
    public function addTime(Time $time) : Time
129
    {
130 4
        $this->seconds += $time->getTotalSeconds();
131
132 4
        return $this;
133
    }
134
135
    /**
136
     * @param Time $time
137
     * @return Time
138
     */
139 3
    public function subTime(Time $time) : Time
140
    {
141 3
        $this->seconds -= $time->getTotalSeconds();
142
143 3
        return $this;
144
    }
145
146
    /**
147
     * @return string
148
     */
149 4
    public function __toString()
150
    {
151 4
        return $this->getShort();
152
    }
153
}
154