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.

Time::fromString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\X509\Certificate;
6
7
use Sop\ASN1\Element;
8
use Sop\ASN1\Type\Primitive\GeneralizedTime;
9
use Sop\ASN1\Type\Primitive\UTCTime;
10
use Sop\ASN1\Type\TimeType;
11
use Sop\X509\Feature\DateTimeHelper;
12
13
/**
14
 * Implements *Time* ASN.1 type.
15
 *
16
 * @see https://tools.ietf.org/html/rfc5280#section-4.1
17
 */
18
class Time
19
{
20
    use DateTimeHelper;
21
22
    /**
23
     * Datetime.
24
     *
25
     * @var \DateTimeImmutable
26
     */
27
    protected $_dt;
28
29
    /**
30
     * Time ASN.1 type tag.
31
     *
32
     * @var int
33
     */
34
    protected $_type;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param \DateTimeImmutable $dt
40
     */
41 35
    public function __construct(\DateTimeImmutable $dt)
42
    {
43 35
        $this->_dt = $dt;
44 35
        $this->_type = self::_determineType($dt);
45 35
    }
46
47
    /**
48
     * Initialize from ASN.1.
49
     *
50
     * @param TimeType $el
51
     *
52
     * @return self
53
     */
54 23
    public static function fromASN1(TimeType $el): self
55
    {
56 23
        $obj = new self($el->dateTime());
57 23
        $obj->_type = $el->tag();
58 23
        return $obj;
59
    }
60
61
    /**
62
     * Initialize from date string.
63
     *
64
     * @param null|string $time
65
     * @param null|string $tz
66
     *
67
     * @return self
68
     */
69 13
    public static function fromString(?string $time, ?string $tz = null): self
70
    {
71 13
        return new self(self::_createDateTime($time, $tz));
72
    }
73
74
    /**
75
     * Get datetime.
76
     *
77
     * @return \DateTimeImmutable
78
     */
79 50
    public function dateTime(): \DateTimeImmutable
80
    {
81 50
        return $this->_dt;
82
    }
83
84
    /**
85
     * Generate ASN.1.
86
     *
87
     * @throws \UnexpectedValueException
88
     *
89
     * @return TimeType
90
     */
91 70
    public function toASN1(): TimeType
92
    {
93 70
        $dt = $this->_dt;
94 70
        switch ($this->_type) {
95 70
            case Element::TYPE_UTC_TIME:
96 67
                return new UTCTime($dt);
97 3
            case Element::TYPE_GENERALIZED_TIME:
98
                // GeneralizedTime must not contain fractional seconds
99
                // (rfc5280 4.1.2.5.2)
100 2
                if (0 !== intval($dt->format('u'))) {
101
                    // remove fractional seconds (round down)
102 1
                    $dt = self::_roundDownFractionalSeconds($dt);
103
                }
104 2
                return new GeneralizedTime($dt);
105
        }
106 1
        throw new \UnexpectedValueException(
107 1
            'Time type ' . Element::tagToName($this->_type) . ' not supported.');
108
    }
109
110
    /**
111
     * Determine whether to use UTCTime or GeneralizedTime ASN.1 type.
112
     *
113
     * @param \DateTimeImmutable $dt
114
     *
115
     * @return int Type tag
116
     */
117 35
    protected static function _determineType(\DateTimeImmutable $dt): int
118
    {
119 35
        if ($dt->format('Y') >= 2050) {
120 3
            return Element::TYPE_GENERALIZED_TIME;
121
        }
122 32
        return Element::TYPE_UTC_TIME;
123
    }
124
}
125