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.
Passed
Push — master ( 95c197...564e5e )
by Joni
04:32
created

TimeType   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 90
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A fromString() 0 12 3
A _createTimeZone() 0 6 2
A _getLastDateTimeImmutableErrorsStr() 0 4 1
A dateTime() 0 3 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\ASN1\Type;
6
7
use Sop\ASN1\Element;
8
9
/**
10
 * Base class for all types representing a point in time.
11
 */
12
abstract class TimeType extends Element
13
{
14
    /**
15
     * UTC timezone.
16
     *
17
     * @var string
18
     */
19
    const TZ_UTC = 'UTC';
20
21
    /**
22
     * Date and time.
23
     *
24
     * @var \DateTimeImmutable
25
     */
26
    protected $_dateTime;
27
28
    /**
29
     * Constructor.
30
     *
31
     * @param \DateTimeImmutable $dt
32
     */
33 22
    public function __construct(\DateTimeImmutable $dt)
34
    {
35 22
        $this->_dateTime = $dt;
36 22
    }
37
38
    /**
39
     * Initialize from datetime string.
40
     *
41
     * @see http://php.net/manual/en/datetime.formats.php
42
     *
43
     * @param string      $time Time string
44
     * @param null|string $tz   timezone, if null use default
45
     *
46
     * @throws \RuntimeException
47
     *
48
     * @return self
49
     */
50 6
    public static function fromString(string $time, ?string $tz = null): self
51
    {
52
        try {
53 6
            if (!isset($tz)) {
54 4
                $tz = date_default_timezone_get();
55
            }
56 6
            return new static(
57 6
                new \DateTimeImmutable($time, self::_createTimeZone($tz)));
58 2
        } catch (\Exception $e) {
59 2
            throw new \RuntimeException(
60
                'Failed to create DateTime: ' .
61 2
                self::_getLastDateTimeImmutableErrorsStr(), 0, $e);
62
        }
63
    }
64
65
    /**
66
     * Get the date and time.
67
     *
68
     * @return \DateTimeImmutable
69
     */
70 7
    public function dateTime(): \DateTimeImmutable
71
    {
72 7
        return $this->_dateTime;
73
    }
74
75
    /**
76
     * Create DateTimeZone object from string.
77
     *
78
     * @param string $tz
79
     *
80
     * @throws \UnexpectedValueException If timezone is invalid
81
     *
82
     * @return \DateTimeZone
83
     */
84 27
    protected static function _createTimeZone(string $tz): \DateTimeZone
85
    {
86
        try {
87 27
            return new \DateTimeZone($tz);
88 1
        } catch (\Exception $e) {
89 1
            throw new \UnexpectedValueException('Invalid timezone.', 0, $e);
90
        }
91
    }
92
93
    /**
94
     * Get last error caused by DateTimeImmutable.
95
     *
96
     * @return string
97
     */
98 3
    protected static function _getLastDateTimeImmutableErrorsStr(): string
99
    {
100 3
        $errors = \DateTimeImmutable::getLastErrors()['errors'];
101 3
        return implode(', ', $errors);
102
    }
103
}
104