Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
4.17% covered (danger)
4.17%
1 / 24
11.11% covered (danger)
11.11%
1 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
QtiTimeStorage
4.17% covered (danger)
4.17%
1 / 24
11.11% covered (danger)
11.11%
1 / 9
138.74
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getStorageKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStorageKeyFromTestSessionId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStorageService
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 setStorageService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 store
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 load
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 delete
0.00% covered (danger)
0.00%
0 / 5
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) 2016-2017 (original work) Open Assessment Technologies SA ;
19 */
20
21namespace oat\taoQtiTest\models\runner\time;
22
23use oat\tao\model\state\StateStorage;
24use oat\taoQtiTest\models\runner\StorageManager;
25use oat\taoTests\models\runner\time\TimeStorage;
26
27/**
28 * Class QtiTimeStorage
29 * @package oat\taoQtiTest\models\runner\time
30 * @author Jean-Sébastien Conan <jean-sebastien.conan@taotesting.com>
31 */
32class QtiTimeStorage implements TimeStorage, QtiTimeStorageFormatAware
33{
34    use QtiTimeStorageFormatAwareTrait;
35
36    /**
37     * Prefix used to identify the data slot in the storage
38     */
39    public const STORAGE_PREFIX = 'timer_';
40
41    /**
42     * Local cache used to maintain data in memory while the request is running
43     * @var array
44     */
45    protected $cache = null;
46
47    /**
48     * The assessment test session identifier
49     * @var string
50     */
51    protected $testSessionId;
52
53    /**
54     * The assessment test user identifier
55     * @var string
56     */
57    protected $userId;
58
59    /**
60     * @var StateStorage
61     */
62    protected $storageService;
63
64    /**
65     * QtiTimeStorage constructor.
66     * @param string $testSessionId
67     * @param string $userId
68     */
69    public function __construct($testSessionId, $userId)
70    {
71        $this->testSessionId = $testSessionId;
72        $this->userId = $userId;
73    }
74
75    /**
76     * Gets the key identifying the storage for the provided user
77     * @return string
78     */
79    protected function getStorageKey()
80    {
81        return self::getStorageKeyFromTestSessionId($this->testSessionId);
82    }
83
84    /**
85     * Storage Key from Test Session Id
86     *
87     * Returns the Storage Key corresponding to a fiven $testSessionId
88     *
89     * @param string $testSessionId
90     * @return string
91     */
92    public static function getStorageKeyFromTestSessionId($testSessionId)
93    {
94        return self::STORAGE_PREFIX . $testSessionId;
95    }
96
97    /**
98     * Gets the user key to access the storage
99     * @return string
100     */
101    protected function getUserKey()
102    {
103        return $this->userId;
104    }
105
106    /**
107     * Gets the StateStorage service
108     * @return StateStorage
109     */
110    public function getStorageService()
111    {
112        if (!$this->storageService) {
113            $this->storageService = \tao_models_classes_service_StateStorage::singleton();
114        }
115        return $this->storageService;
116    }
117
118    /**
119     * Sets the StateStorage service
120     * @param StateStorage $storageService
121     */
122    public function setStorageService($storageService)
123    {
124        $this->storageService = $storageService;
125    }
126
127    /**
128     * Stores the timer data
129     * @param array $data
130     * @return TimeStorage
131     */
132    public function store($data)
133    {
134        $this->cache[$this->testSessionId] = &$data;
135        $encodedData = $this->getStorageFormat()->encode($data);
136
137        $this->getStorageService()->set($this->getUserKey(), $this->getStorageKey(), $encodedData);
138        \common_Logger::d(sprintf('QtiTimer: Stored %d bytes into state storage', strlen($encodedData)));
139
140        return $this;
141    }
142
143    /**
144     * Loads the timer data from the storage
145     * @return array
146     */
147    public function load()
148    {
149        if (!isset($this->cache[$this->testSessionId])) {
150            $encodedData = $this->getStorageService()->get($this->getUserKey(), $this->getStorageKey());
151            \common_Logger::d(sprintf('QtiTimer: Loaded %d bytes from state storage', strlen($encodedData)));
152
153            $this->cache[$this->testSessionId] = $this->getStorageFormat()->decode($encodedData);
154        }
155
156        return $this->cache[$this->testSessionId];
157    }
158
159    /**
160     * @inheritdoc
161     */
162    public function delete()
163    {
164        $storage = $this->getStorageService();
165        $result = $this->getStorageService()->del($this->getUserKey(), $this->getStorageKey());
166        if ($storage instanceof StorageManager) {
167            $result = $storage->persist($this->getUserKey());
168        }
169        return $result;
170    }
171}