GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TimeBuilder::fromString()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
crap 4
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Speicher210\BusinessHours\Day\Time;
6
7
/**
8
 * Builder for Time.
9
 */
10
class TimeBuilder
11
{
12
    /**
13
     * Create a new Time from an array.
14
     *
15
     * @param array $data The data.
16
     * @return Time
17 36
     */
18
    public static function fromArray(array $data): Time
19 36
    {
20 3
        if (!isset($data['hours'])) {
21
            throw new \InvalidArgumentException('Array is not valid.');
22
        }
23 33
24 33
        return new Time(
25 33
            $data['hours'],
26 33
            $data['minutes'] ?? 0,
27 33
            $data['seconds'] ?? 0
28
        );
29
    }
30
31
    /**
32
     * Create a new time from a string.
33
     *
34
     * @param string $time The time as a string.
35
     * @return Time
36
     * @throws \InvalidArgumentException If the passed time is invalid.
37 123
     */
38
    public static function fromString($time): Time
39 123
    {
40 6
        if (empty($time)) {
41
            throw new \InvalidArgumentException('Invalid time "".');
42
        }
43
44 117
        try {
45 117
            $date = new \DateTime($time);
46 9
        } catch (\Exception $e) {
47
            throw new \InvalidArgumentException(\sprintf('Invalid time "%s".', $time), 0, $e);
48
        }
49 108
50 108
        $return = static::fromDate($date);
51 12
        if (\strpos($time, '24') === 0) {
52 9
            $return->setHours(24);
53
        }
54 105
55
        return $return;
56
    }
57
58
    /**
59
     * Create a new time from a date.
60
     *
61
     * @param \DateTime $date The date.
62
     * @return Time
63 156
     */
64
    public static function fromDate(\DateTime $date): Time
65 156
    {
66
        return new Time((int)$date->format('H'), (int)$date->format('i'), (int)$date->format('s'));
67
    }
68
69
    /**
70
     * Create a new time from seconds.
71
     *
72
     * @param integer $seconds The seconds.
73
     * @return Time
74 30
     */
75
    public static function fromSeconds(int $seconds): Time
76 30
    {
77
        if ($seconds < 0 || $seconds > 86400) {
78 30
            throw new \InvalidArgumentException(\sprintf('Invalid time "%s".', $seconds));
79 6
        }
80
81
        $data = [
82
            'hours' => (int)($seconds / 3600),
83 24
            'minutes' => ($seconds / 60) % 60,
84 24
            'seconds' => $seconds % 60
85
        ];
86 24
87
        return self::fromArray($data);
88 24
    }
89
}
90