Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| MaintenanceState | |
0.00% |
0 / 32 |
|
0.00% |
0 / 10 |
506 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| toArray | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setEndTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getStartTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getBooleanStatus | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getDuration | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getDateTime | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
30 | |||
| checkData | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 Open Assessment Technologies SA |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\maintenance; |
| 23 | |
| 24 | class MaintenanceState |
| 25 | { |
| 26 | public const ID = 'id'; |
| 27 | public const STATUS = 'status'; |
| 28 | public const START_TIME = 'start'; |
| 29 | public const END_TIME = 'end'; |
| 30 | |
| 31 | public const LIVE_MODE = 'on'; |
| 32 | public const OFFLINE_MODE = 'off'; |
| 33 | |
| 34 | public const DATEDIFF_FORMAT = '%y years, %m months, %d days %H:%I:%S'; |
| 35 | |
| 36 | protected static $availableStatus = [self::LIVE_MODE, self::OFFLINE_MODE]; |
| 37 | |
| 38 | /** |
| 39 | * The id to identify a Maintenance state |
| 40 | * |
| 41 | * @var integer |
| 42 | */ |
| 43 | protected $id; |
| 44 | |
| 45 | /** |
| 46 | * The datetime when MaintenanceState was begun |
| 47 | * |
| 48 | * @var \DateTime |
| 49 | */ |
| 50 | protected $startTime; |
| 51 | |
| 52 | /** |
| 53 | * The datetime when MaintenanceState was ended |
| 54 | * |
| 55 | * @var \DateTime |
| 56 | */ |
| 57 | protected $endTime = null; |
| 58 | |
| 59 | /** |
| 60 | * The maintenance status, must be be in self::$availableStatus |
| 61 | * |
| 62 | * @var mixed |
| 63 | */ |
| 64 | protected $status; |
| 65 | |
| 66 | |
| 67 | /** |
| 68 | * MaintenanceState constructor. |
| 69 | * |
| 70 | * @param array $data |
| 71 | */ |
| 72 | public function __construct(array $data) |
| 73 | { |
| 74 | $this->checkData($data); |
| 75 | $this->id = isset($data[self::ID]) ? $data[self::ID] : 1; |
| 76 | $this->status = $data[self::STATUS]; |
| 77 | |
| 78 | if (isset($data[self::START_TIME])) { |
| 79 | $this->startTime = $this->getDateTime($data[self::START_TIME]); |
| 80 | } else { |
| 81 | $this->startTime = new \DateTime(); |
| 82 | } |
| 83 | |
| 84 | if (isset($data[self::END_TIME])) { |
| 85 | $this->endTime = $this->getDateTime($data[self::END_TIME]); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Return the Maintenance state as array, Datetime are converted to timestamp |
| 91 | * |
| 92 | * @return array |
| 93 | */ |
| 94 | public function toArray() |
| 95 | { |
| 96 | $data = [ |
| 97 | self::ID => $this->id, |
| 98 | self::STATUS => $this->status, |
| 99 | self::START_TIME => $this->startTime->getTimestamp(), |
| 100 | ]; |
| 101 | |
| 102 | if (! is_null($this->endTime)) { |
| 103 | $data[self::END_TIME] = $this->endTime->getTimestamp(); |
| 104 | } |
| 105 | |
| 106 | return $data; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return int|mixed |
| 111 | */ |
| 112 | public function getId() |
| 113 | { |
| 114 | return $this->id; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @param $id |
| 119 | */ |
| 120 | public function setId($id) |
| 121 | { |
| 122 | $this->id = $id; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @param $endTime |
| 127 | */ |
| 128 | public function setEndTime($endTime) |
| 129 | { |
| 130 | $this->endTime = $this->getDateTime($endTime); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @return \DateTime |
| 135 | */ |
| 136 | public function getStartTime() |
| 137 | { |
| 138 | return $this->startTime; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return bool |
| 143 | */ |
| 144 | public function getBooleanStatus() |
| 145 | { |
| 146 | return $this->status === self::LIVE_MODE ? true : false; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return \DateInterval |
| 151 | */ |
| 152 | public function getDuration() |
| 153 | { |
| 154 | $endTime = $this->endTime ?: new \DateTime(); |
| 155 | return $this->startTime->diff($endTime); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Transform a string|Datetime to Datetime |
| 160 | * |
| 161 | * @param $dateTime |
| 162 | * @return \DateTime |
| 163 | * @throws \common_Exception |
| 164 | */ |
| 165 | protected function getDateTime($dateTime) |
| 166 | { |
| 167 | if ($dateTime instanceof \DateTime) { |
| 168 | return $dateTime; |
| 169 | } |
| 170 | |
| 171 | if ((is_string($dateTime) && (int) $dateTime > 0) || is_numeric($dateTime)) { |
| 172 | return (new \DateTime())->setTimestamp($dateTime); |
| 173 | } |
| 174 | |
| 175 | throw new \common_Exception(__('A date has to be a Datetime or timestamp')); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Check data of constructor input |
| 180 | * |
| 181 | * @param array $data |
| 182 | * @throws \common_Exception |
| 183 | */ |
| 184 | protected function checkData(array $data) |
| 185 | { |
| 186 | if (! isset($data[self::STATUS]) || ! in_array($data[self::STATUS], self::$availableStatus)) { |
| 187 | throw new \common_Exception( |
| 188 | __('A maintenance status must have a STATUS: "%s" or "%s"', self::LIVE_MODE, self::OFFLINE_MODE) |
| 189 | ); |
| 190 | } |
| 191 | } |
| 192 | } |