Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| Maintenance | |
0.00% |
0 / 22 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| isPlatformReady | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| enablePlatform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| disablePlatform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPlatformState | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| setPlatformState | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getStorage | |
0.00% |
0 / 7 |
|
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 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\tao\model\metadata\exception\InconsistencyConfigException; |
| 26 | |
| 27 | /** |
| 28 | * Class Maintenance |
| 29 | * |
| 30 | * Service to manage platform maintenance |
| 31 | * |
| 32 | * @package oat\tao\model\maintenance |
| 33 | */ |
| 34 | class Maintenance extends ConfigurableService |
| 35 | { |
| 36 | public const SERVICE_ID = 'tao/maintenance'; |
| 37 | |
| 38 | public const OPTION_PERSISTENCE = 'persistence'; |
| 39 | |
| 40 | /** |
| 41 | * Storage to store platform state |
| 42 | * |
| 43 | * @var MaintenanceStorage |
| 44 | */ |
| 45 | protected $storage; |
| 46 | |
| 47 | /** |
| 48 | * Check if platform is ready, if $state is null, it is retrieved from storage |
| 49 | * |
| 50 | * @param MaintenanceState|null $state |
| 51 | * @return bool |
| 52 | */ |
| 53 | public function isPlatformReady(MaintenanceState $state = null) |
| 54 | { |
| 55 | if (is_null($state)) { |
| 56 | $state = $this->getPlatformState(); |
| 57 | } |
| 58 | return ($state->getBooleanStatus() === true); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Enable the platform by updating storage |
| 63 | */ |
| 64 | public function enablePlatform() |
| 65 | { |
| 66 | $this->setPlatformState(MaintenanceState::LIVE_MODE); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Disable the platform by updating storage |
| 71 | */ |
| 72 | public function disablePlatform() |
| 73 | { |
| 74 | $this->setPlatformState(MaintenanceState::OFFLINE_MODE); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get the current maintenance state of the platform |
| 79 | * |
| 80 | * @return MaintenanceState |
| 81 | */ |
| 82 | public function getPlatformState() |
| 83 | { |
| 84 | try { |
| 85 | return $this->getStorage()->getCurrentPlatformState(); |
| 86 | } catch (\common_exception_NotFound $e) { |
| 87 | $this->enablePlatform(); |
| 88 | return new MaintenanceState( |
| 89 | [MaintenanceState::STATUS => MaintenanceState::LIVE_MODE] |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Update platform state |
| 96 | * |
| 97 | * @param $status |
| 98 | */ |
| 99 | protected function setPlatformState($status) |
| 100 | { |
| 101 | $state = new MaintenanceState([ |
| 102 | MaintenanceState::STATUS => $status |
| 103 | ]); |
| 104 | |
| 105 | $this->getStorage()->setPlatformState($state); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get the maintenance storage |
| 110 | * |
| 111 | * @return MaintenanceStorage |
| 112 | * @throws InconsistencyConfigException |
| 113 | */ |
| 114 | public function getStorage() |
| 115 | { |
| 116 | if (! $this->storage) { |
| 117 | if (! $this->hasOption(self::OPTION_PERSISTENCE)) { |
| 118 | throw new InconsistencyConfigException(__('Maintenance service must have a persistence option.')); |
| 119 | } |
| 120 | $this->storage = new MaintenanceStorage( |
| 121 | \common_persistence_Manager::getPersistence($this->getOption(self::OPTION_PERSISTENCE)) |
| 122 | ); |
| 123 | } |
| 124 | return $this->storage; |
| 125 | } |
| 126 | } |