Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MaintenanceStorage
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 5
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setPlatformState
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getHistory
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 getCurrentPlatformState
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getDriver
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
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
22namespace oat\tao\model\maintenance;
23
24class MaintenanceStorage
25{
26    public const LAST_MODE = 'last';
27
28    public const PREFIX = 'maintenance_';
29
30    /**
31     * Driver to access KeyValue storage
32     *
33     * @var \common_persistence_KeyValuePersistence
34     */
35    protected $driver;
36
37    /**
38     * MaintenanceStorage constructor.
39     *
40     * @param \common_persistence_KeyValuePersistence $driver
41     */
42    public function __construct(\common_persistence_KeyValuePersistence $driver)
43    {
44        $this->driver = $driver;
45    }
46
47    /**
48     * Persist the maintenance state
49     *
50     * If old maintenance exists, set key with old state id
51     * Persist new maintenance state with key 'last'
52     *
53     * @param MaintenanceState $state
54     */
55    public function setPlatformState(MaintenanceState $state)
56    {
57        if ($previous = $this->getDriver()->get(self::PREFIX . self::LAST_MODE)) {
58            $currentState = new MaintenanceState(json_decode($previous, true));
59            $currentState->setEndTime($state->getStartTime());
60            $this->getDriver()->set(self::PREFIX . $currentState->getId(), json_encode($currentState->toArray()));
61
62            $state->setId($currentState->getId() + 1);
63        }
64
65
66        $this->getDriver()->set(self::PREFIX . self::LAST_MODE, json_encode($state->toArray()));
67    }
68
69    /**
70     * Get maintenance history as list of state
71     *
72     * @todo Return an arrayIterator
73     * @return array
74     */
75    public function getHistory()
76    {
77        $history = [
78            1 => new MaintenanceState(json_decode($this->getDriver()->get(self::PREFIX . self::LAST_MODE), true))
79        ];
80
81        $i = 2;
82        while ($data = json_decode($this->getDriver()->get(self::PREFIX . $i), true)) {
83            $history[$i] = new MaintenanceState($data);
84            $i++;
85        }
86        return $history;
87    }
88
89    /**
90     * Get the current state of the platform
91     *
92     * @return MaintenanceState
93     * @throws \common_exception_NotFound If no state is set
94     */
95    public function getCurrentPlatformState()
96    {
97        $data = json_decode($this->getDriver()->get(self::PREFIX . self::LAST_MODE), true);
98        if (! $data) {
99            throw new \common_exception_NotFound();
100        }
101        return new MaintenanceState($data);
102    }
103
104    /**
105     * Get the driver to access KeyValue persistence
106     *
107     * @return \common_persistence_KeyValuePersistence
108     * @throws \common_Exception
109     */
110    protected function getDriver()
111    {
112        if (! $this->driver) {
113            throw new \common_Exception(__('Maintenance storage driver is not set'));
114        }
115        return $this->driver;
116    }
117}