Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
14.63% |
12 / 82 |
|
20.00% |
3 / 15 |
CRAP | |
0.00% |
0 / 1 |
| InstantActionQueue | |
14.63% |
12 / 82 |
|
20.00% |
3 / 15 |
552.18 | |
0.00% |
0 / 1 |
| getEventManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| perform | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
12 | |||
| getPosition | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| clearAbandonedPositions | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| getTrend | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| isActionEnabled | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| queue | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| dequeue | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| getPersistence | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| getTtl | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getQueueKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getActionConfig | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| getPositions | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getRestrictions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| checkRestrictions | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
| 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) 2018 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\actionQueue\implementation; |
| 24 | |
| 25 | use oat\oatbox\event\EventManager; |
| 26 | use oat\oatbox\session\SessionService; |
| 27 | use oat\tao\model\actionQueue\ActionQueue; |
| 28 | use oat\oatbox\service\ConfigurableService; |
| 29 | use oat\tao\model\actionQueue\QueuedAction; |
| 30 | use oat\tao\model\actionQueue\ActionQueueException; |
| 31 | use oat\oatbox\user\User; |
| 32 | use oat\tao\model\actionQueue\restriction\BasicRestriction; |
| 33 | use oat\tao\model\actionQueue\event\InstantActionOnQueueEvent; |
| 34 | use oat\tao\model\actionQueue\event\ActionQueueTrendEvent; |
| 35 | |
| 36 | /** |
| 37 | * |
| 38 | * Interface InstantActionQueue |
| 39 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 40 | * @package oat\tao\model\actionQueue |
| 41 | */ |
| 42 | class InstantActionQueue extends ConfigurableService implements ActionQueue |
| 43 | { |
| 44 | public const QUEUE_TREND = 'queue_trend'; |
| 45 | |
| 46 | /** |
| 47 | * @return EventManager |
| 48 | */ |
| 49 | protected function getEventManager() |
| 50 | { |
| 51 | return $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * @param QueuedAction $action |
| 57 | * @param User $user |
| 58 | * @return boolean |
| 59 | * @throws |
| 60 | */ |
| 61 | public function perform(QueuedAction $action, User $user = null) |
| 62 | { |
| 63 | $this->propagate($action); |
| 64 | if ($user === null) { |
| 65 | $user = $this->getServiceLocator()->get(SessionService::SERVICE_ID)->getCurrentUser(); |
| 66 | } |
| 67 | $result = false; |
| 68 | $actionConfig = $this->getActionConfig($action); |
| 69 | $restrictions = $this->getRestrictions($actionConfig); |
| 70 | $allowExecution = $this->checkRestrictions($restrictions); |
| 71 | |
| 72 | if ($allowExecution) { |
| 73 | $actionResult = $action([]); |
| 74 | $action->setResult($actionResult); |
| 75 | $result = true; |
| 76 | $this->dequeue($action, $user); |
| 77 | } else { |
| 78 | $this->queue($action, $user); |
| 79 | } |
| 80 | return $result; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Note that this method is not transaction safe so there may be collisions. |
| 85 | * This implementation supposed to provide approximate position in the queue |
| 86 | * @param QueuedAction $action |
| 87 | * @param User $user |
| 88 | * @return integer |
| 89 | * @throws |
| 90 | */ |
| 91 | public function getPosition(QueuedAction $action, User $user = null) |
| 92 | { |
| 93 | $action->setServiceLocator($this->getServiceManager()); |
| 94 | $positions = $this->getPositions($action); |
| 95 | return count($positions); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @inheritdoc |
| 100 | */ |
| 101 | public function clearAbandonedPositions(QueuedAction $action) |
| 102 | { |
| 103 | $action->setServiceLocator($this->getServiceManager()); |
| 104 | $key = $this->getQueueKey($action); |
| 105 | $positions = $this->getPositions($action); |
| 106 | $edgeTime = time() - $this->getTtl($action); |
| 107 | $newPositions = array_filter($positions, function ($val) use ($edgeTime) { |
| 108 | return $val > $edgeTime; |
| 109 | }); |
| 110 | $this->getPersistence()->set($key, json_encode($newPositions)); |
| 111 | $this->getPersistence()->set(get_class($action) . self::QUEUE_TREND, 0); |
| 112 | return count($positions) - count($newPositions); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @return int |
| 117 | */ |
| 118 | public function getTrend(QueuedAction $action) |
| 119 | { |
| 120 | $trend = $this->getPersistence()->get(get_class($action) . self::QUEUE_TREND); |
| 121 | return (int)$trend; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @param QueuedAction $action |
| 126 | * @return bool |
| 127 | * @throws ActionQueueException |
| 128 | */ |
| 129 | public function isActionEnabled(QueuedAction $action): bool |
| 130 | { |
| 131 | $actionConfig = $this->getActionConfig($action); |
| 132 | foreach ($this->getRestrictions($actionConfig) as $restriction => $value) { |
| 133 | if ($value !== 0) { |
| 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @param QueuedAction $action |
| 143 | * @param User $user |
| 144 | * @throws \common_Exception |
| 145 | */ |
| 146 | protected function queue(QueuedAction $action, User $user) |
| 147 | { |
| 148 | $key = $this->getQueueKey($action); |
| 149 | $positions = $this->getPositions($action); |
| 150 | $positions[$user->getIdentifier()] = time(); |
| 151 | $this->getPersistence()->set($key, json_encode($positions)); |
| 152 | $this->getEventManager()->trigger(new InstantActionOnQueueEvent($key, $user, $positions, 'queue', $action)); |
| 153 | if ($this->getTrend($action) >= 0) { |
| 154 | $this->getEventManager()->trigger(new ActionQueueTrendEvent($action, true)); |
| 155 | $this->getPersistence()->set(get_class($action) . self::QUEUE_TREND, -1); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @param QueuedAction $action |
| 161 | * @param User $user |
| 162 | * @throws \common_Exception |
| 163 | */ |
| 164 | protected function dequeue(QueuedAction $action, User $user) |
| 165 | { |
| 166 | $key = $this->getQueueKey($action); |
| 167 | $positions = $this->getPositions($action); |
| 168 | if (array_key_exists($user->getIdentifier(), $positions)) { |
| 169 | // now we sure that this user has been queued |
| 170 | unset($positions[$user->getIdentifier()]); |
| 171 | $this->getEventManager()->trigger( |
| 172 | new InstantActionOnQueueEvent($key, $user, $positions, 'dequeue', $action) |
| 173 | ); |
| 174 | $this->getPersistence()->set($key, json_encode($positions)); |
| 175 | |
| 176 | if ($this->getTrend($action) <= 0) { |
| 177 | $this->getEventManager()->trigger(new ActionQueueTrendEvent($action, false)); |
| 178 | $this->getPersistence()->set(get_class($action) . self::QUEUE_TREND, 1); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * @return \common_persistence_KeyValuePersistence |
| 185 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 186 | */ |
| 187 | protected function getPersistence() |
| 188 | { |
| 189 | $persistenceId = $this->getOption(self::OPTION_PERSISTENCE); |
| 190 | return $this |
| 191 | ->getServiceManager() |
| 192 | ->get(\common_persistence_Manager::SERVICE_ID) |
| 193 | ->getPersistenceById($persistenceId); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * @param QueuedAction $action |
| 198 | * @throws |
| 199 | * @return integer |
| 200 | */ |
| 201 | protected function getTtl(QueuedAction $action) |
| 202 | { |
| 203 | $actionConfig = $this->getActionConfig($action); |
| 204 | $ttl = (int) (isset($actionConfig[self::ACTION_PARAM_TTL]) ? $actionConfig[self::ACTION_PARAM_TTL] : 0); |
| 205 | return $ttl; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * @param QueuedAction $action |
| 210 | * @return string |
| 211 | */ |
| 212 | protected function getQueueKey(QueuedAction $action) |
| 213 | { |
| 214 | return self::class . '_' . $action->getId(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @param QueuedAction $action |
| 219 | * @throws ActionQueueException in action was not registered in the config |
| 220 | * @return array |
| 221 | */ |
| 222 | protected function getActionConfig(QueuedAction $action) |
| 223 | { |
| 224 | $actions = $this->getOption(self::OPTION_ACTIONS); |
| 225 | if (!isset($actions[$action->getId()])) { |
| 226 | throw new ActionQueueException( |
| 227 | __('Action `%s` is not configured in the action queue service', $action->getId()) |
| 228 | ); |
| 229 | } |
| 230 | return $actions[$action->getId()]; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @param QueuedAction $action |
| 235 | * @return array |
| 236 | */ |
| 237 | protected function getPositions(QueuedAction $action) |
| 238 | { |
| 239 | $key = $this->getQueueKey($action); |
| 240 | $positions = json_decode($this->getPersistence()->get($key), true); |
| 241 | if (!$positions) { |
| 242 | $positions = []; |
| 243 | } |
| 244 | return $positions; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @param array $actionConfig |
| 249 | * @return array |
| 250 | */ |
| 251 | private function getRestrictions(array $actionConfig): array |
| 252 | { |
| 253 | return $actionConfig['restrictions'] ?? []; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @param array $restrictions |
| 258 | * @return bool |
| 259 | */ |
| 260 | private function checkRestrictions(array $restrictions): bool |
| 261 | { |
| 262 | $allowExecution = true; |
| 263 | |
| 264 | foreach ($restrictions as $restrictionClass => $value) { |
| 265 | if (class_exists($restrictionClass) && is_subclass_of($restrictionClass, BasicRestriction::class)) { |
| 266 | /** @var BasicRestriction $restriction */ |
| 267 | $restriction = new $restrictionClass(); |
| 268 | $this->propagate($restriction); |
| 269 | $allowExecution = $allowExecution && $restriction->doesComply($value); |
| 270 | } |
| 271 | } |
| 272 | return $allowExecution; |
| 273 | } |
| 274 | } |