Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| DeliveryExecutionCounterService | |
0.00% |
0 / 21 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| count | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| executionStateChanged | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| executionCreated | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getPersistence | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getStatusKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| refresh | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | namespace oat\taoDelivery\model\execution\Counter; |
| 23 | |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionState; |
| 26 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionCreated; |
| 27 | |
| 28 | /** |
| 29 | * Class DeliveryExecutionCounterService |
| 30 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 31 | */ |
| 32 | class DeliveryExecutionCounterService extends ConfigurableService implements DeliveryExecutionCounterInterface |
| 33 | { |
| 34 | public const OPTION_PERSISTENCE = 'persistence'; |
| 35 | //concatenation in constants allowed since php 5.6 |
| 36 | public const KEY_PREFIX = self::class . '_'; |
| 37 | |
| 38 | /** |
| 39 | * Get number of delivery executions of given status. |
| 40 | * @param $statusUri |
| 41 | * @return integer |
| 42 | */ |
| 43 | public function count($statusUri) |
| 44 | { |
| 45 | $persistence = $this->getPersistence(); |
| 46 | $key = $this->getStatusKey($statusUri); |
| 47 | return intval($persistence->get($key)); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param DeliveryExecutionState $event |
| 52 | */ |
| 53 | public function executionStateChanged(DeliveryExecutionState $event) |
| 54 | { |
| 55 | $fromStatusKey = $this->getStatusKey($event->getPreviousState()); |
| 56 | $toStatusKey = $this->getStatusKey($event->getState()); |
| 57 | $persistence = $this->getPersistence(); |
| 58 | |
| 59 | if ($persistence->exists($fromStatusKey) && $persistence->get($fromStatusKey) > 0) { |
| 60 | $persistence->decr($fromStatusKey); |
| 61 | } |
| 62 | |
| 63 | if (!$persistence->exists($toStatusKey)) { |
| 64 | $persistence->set($toStatusKey, 1); |
| 65 | } else { |
| 66 | $persistence->incr($toStatusKey); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param DeliveryExecutionCreated $event |
| 72 | * @throws \common_exception_NotFound |
| 73 | */ |
| 74 | public function executionCreated(DeliveryExecutionCreated $event) |
| 75 | { |
| 76 | $persistence = $this->getPersistence(); |
| 77 | $state = $event->getDeliveryExecution()->getState()->getUri(); |
| 78 | $toStatusKey = $this->getStatusKey($state); |
| 79 | if (!$persistence->exists($toStatusKey)) { |
| 80 | $persistence->set($toStatusKey, 1); |
| 81 | } else { |
| 82 | $persistence->incr($toStatusKey); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @return \common_persistence_KvDriver |
| 88 | */ |
| 89 | protected function getPersistence() |
| 90 | { |
| 91 | return $this->getServiceLocator() |
| 92 | ->get(\common_persistence_Manager::class)->getPersistenceById($this->getOption(self::OPTION_PERSISTENCE)); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @param string $statusUri |
| 97 | * @return string |
| 98 | */ |
| 99 | protected function getStatusKey($statusUri) |
| 100 | { |
| 101 | return self::KEY_PREFIX . $statusUri; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @param $statusUri |
| 106 | */ |
| 107 | public function refresh($statusUri) |
| 108 | { |
| 109 | //this implementation do not support refreshing |
| 110 | } |
| 111 | } |