Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
68 / 68
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
TimeStamp
100.00% covered (success)
100.00%
68 / 68
100.00% covered (success)
100.00%
9 / 9
32
100.00% covered (success)
100.00%
1 / 1
 expirationFromNow
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
9
 checkExpiration
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
 asDateTime
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 isExpired
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 fromSQLFormat
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
 toSQLFormat
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 fromXMLFormat
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 now
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 nowString
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/** @noinspection PhpUnhandledExceptionInspection */
3declare(strict_types=1);
4// TODO unit Test
5
6class TimeStamp {
7  static public function expirationFromNow(int $validToTimestamp = 0, int $validForMinutes = 0): int {
8    $timeZone = new DateTimeZone(SystemConfig::$system_timezone);
9
10    if ($validForMinutes > 0) {
11      $validToFromNowOn = new DateTime(self::nowString(), $timeZone);
12      $validToFromNowOn->modify("$validForMinutes minutes");
13    }
14
15    if ($validToTimestamp > 0) {
16      $validTo = new DateTime("now", $timeZone);
17      $validTo->setTimestamp($validToTimestamp);
18    }
19
20    if (isset($validToFromNowOn) and !isset($validTo)) {
21      return $validToFromNowOn->getTimestamp();
22    }
23
24    if (isset($validTo) and !isset($validToFromNowOn)) {
25      return $validTo->getTimestamp();
26    }
27
28    if (isset($validToFromNowOn) and isset($validTo)) {
29      return min($validTo->getTimestamp(), $validToFromNowOn->getTimestamp());
30    }
31
32    return 0;
33  }
34
35  static public function checkExpiration(int $validFromTimestamp = 0, int $validToTimestamp = 0): void {
36    switch (self::isExpired($validFromTimestamp, $validToTimestamp)->type) {
37      case ExpirationStateType::Expired:
38        $validTo = self::asDateTime($validToTimestamp);
39        throw new HttpError(
40          "Testing Period for this login is over since {$validTo->format(SystemConfig::$language_dateFormat)}",
41          410
42        );
43      case ExpirationStateType::Scheduled:
44        $validFrom = self::asDateTime($validFromTimestamp);
45        throw new HttpError(
46          "Testing Period for this login has not yet started and will begin at {$validFrom->format(SystemConfig::$language_dateFormat)}",
47          401
48        );
49    }
50  }
51
52  static private function asDateTime(int $timestamp): DateTime {
53    $timeZone = new DateTimeZone(SystemConfig::$system_timezone);
54    $dateTime = new DateTime("now", $timeZone);
55    $dateTime->setTimestamp($timestamp);
56    return $dateTime;
57  }
58
59  static public function isExpired(int $validFromTimestamp = 0, int $validToTimestamp = 0): ExpirationState {
60    $timeZone = new DateTimeZone(SystemConfig::$system_timezone);
61    $now = new DateTime(self::nowString(), $timeZone);
62
63    if ($validToTimestamp > 0) {
64      $validTo = self::asDateTime($validToTimestamp);
65      if ($validTo < $now) {
66        return new ExpirationState(ExpirationStateType::Expired, $validToTimestamp);
67      }
68    }
69
70    if ($validFromTimestamp > 0) {
71      $validFrom = self::asDateTime($validFromTimestamp);
72      if ($validFrom > $now) {
73        return new ExpirationState(ExpirationStateType::Scheduled, $validFromTimestamp);
74      }
75    }
76
77    return new ExpirationState(ExpirationStateType::None);
78  }
79
80  static public function fromSQLFormat(?string $sqlFormatTimestamp): int {
81    if (!$sqlFormatTimestamp) {
82      return 0;
83    }
84
85    $timeZone = new DateTimeZone(SystemConfig::$system_timezone);
86
87    // TODO remove this workaround. problem: date is stored in differently ways in table admintokens and others
88    if (is_numeric($sqlFormatTimestamp) and ((int) $sqlFormatTimestamp > 1000000)) {
89      $dateTime = new DateTime("now", $timeZone);
90      $dateTime->setTimestamp((int) $sqlFormatTimestamp);
91      return $dateTime->getTimestamp();
92    }
93
94    $dateTime = DateTime::createFromFormat("Y-m-d H:i:s", $sqlFormatTimestamp, $timeZone);
95    return $dateTime ? $dateTime->getTimestamp() : 0;
96  }
97
98  static public function toSQLFormat(int $timestamp): ?string {
99    if ($timestamp <= 0) {
100      return null;
101    }
102
103    $timeZone = new DateTimeZone(SystemConfig::$system_timezone);
104    $dateTime = new DateTime('now', $timeZone);
105    $dateTime->setTimestamp($timestamp);
106    return $dateTime->format("Y-m-d H:i:s");
107  }
108
109  static public function fromXMLFormat(?string $xmlFormatTimestamp): int {
110    if (!$xmlFormatTimestamp) {
111      return 0;
112    }
113
114    $timeZone = new DateTimeZone(SystemConfig::$system_timezone);
115    $dateTime = DateTime::createFromFormat("d/m/Y H:i", $xmlFormatTimestamp, $timeZone);
116    return $dateTime ? $dateTime->getTimestamp() : 0;
117  }
118
119  static public function now(): int {
120    $timeZone = new DateTimeZone(SystemConfig::$system_timezone);
121    $dateTime = new DateTime(self::nowString(), $timeZone);
122    return $dateTime->getTimestamp();
123  }
124
125  static private function nowString(): string {
126    return (isset(SystemConfig::$debug_useStaticTime) and (SystemConfig::$debug_useStaticTime))
127      ? SystemConfig::$debug_useStaticTime
128      : 'now';
129  }
130}