Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
88.89% |
40 / 45 |
|
77.78% |
7 / 9 |
CRAP | |
0.00% |
0 / 1 |
| AbstractStateService | |
88.89% |
40 / 45 |
|
77.78% |
7 / 9 |
15.31 | |
0.00% |
0 / 1 |
| legacyTransition | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getInitialStatus | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| createDeliveryExecution | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| reactivateExecution | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| setState | |
83.33% |
20 / 24 |
|
0.00% |
0 / 1 |
5.12 | |||
| getStorageEngine | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSessionService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getEventManager | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| emitEvent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getReactivableStates | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| isStateInteractive | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 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 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoDelivery\model\execution; |
| 23 | |
| 24 | use common_exception_NotFound; |
| 25 | use common_session_SessionManager; |
| 26 | use oat\oatbox\event\Event; |
| 27 | use oat\oatbox\event\EventManager; |
| 28 | use oat\oatbox\log\LoggerAwareTrait; |
| 29 | use oat\oatbox\service\ConfigurableService; |
| 30 | use oat\oatbox\session\SessionService; |
| 31 | use oat\oatbox\user\User; |
| 32 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionCreated; |
| 33 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionReactivated; |
| 34 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionState as DeliveryExecutionStateEvent; |
| 35 | use oat\taoDelivery\models\classes\execution\event\DeliveryExecutionStateContext; |
| 36 | |
| 37 | /** |
| 38 | * Class AbstractStateService |
| 39 | * @package oat\taoDelivery |
| 40 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 41 | */ |
| 42 | abstract class AbstractStateService extends ConfigurableService implements StateServiceInterface |
| 43 | { |
| 44 | use LoggerAwareTrait; |
| 45 | |
| 46 | public const OPTION_REACTIVABLE_STATES = 'reactivableStates'; |
| 47 | |
| 48 | private const DEFAULT_REACTIVABLE_STATES = [ |
| 49 | DeliveryExecutionInterface::STATE_TERMINATED, |
| 50 | ]; |
| 51 | |
| 52 | private const INTERACTIVE_STATES = [ |
| 53 | DeliveryExecutionInterface::STATE_ACTIVE, |
| 54 | DeliveryExecutionInterface::STATE_PAUSED, |
| 55 | ]; |
| 56 | |
| 57 | /** |
| 58 | * Legacy function to ensure all calls to setState use |
| 59 | * the correct transition instead |
| 60 | * |
| 61 | * @param DeliveryExecution $deliveryExecution |
| 62 | * @param string $state |
| 63 | * |
| 64 | * @return bool |
| 65 | */ |
| 66 | abstract public function legacyTransition(DeliveryExecution $deliveryExecution, $state); |
| 67 | |
| 68 | /** |
| 69 | * Get the status new delivery executions should be started with |
| 70 | * |
| 71 | * @param string $deliveryId |
| 72 | * @param User $user |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | abstract public function getInitialStatus($deliveryId, User $user); |
| 77 | |
| 78 | /** |
| 79 | * @inheritDoc |
| 80 | */ |
| 81 | public function createDeliveryExecution($deliveryId, User $user, $label) |
| 82 | { |
| 83 | $status = $this->getInitialStatus($deliveryId, $user); |
| 84 | $deliveryExecution = $this |
| 85 | ->getStorageEngine() |
| 86 | ->spawnDeliveryExecution($label, $deliveryId, $user->getIdentifier(), $status); |
| 87 | // trigger event |
| 88 | $event = new DeliveryExecutionCreated($deliveryExecution, $user); |
| 89 | $this->getEventManager()->trigger($event); |
| 90 | |
| 91 | return $deliveryExecution; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @inheritDoc |
| 96 | */ |
| 97 | public function reactivateExecution(DeliveryExecution $deliveryExecution, $reason = null) |
| 98 | { |
| 99 | $executionState = $deliveryExecution->getState()->getUri(); |
| 100 | $result = false; |
| 101 | |
| 102 | if (in_array($executionState, $this->getReactivableStates(), true)) { |
| 103 | $this->setState($deliveryExecution, DeliveryExecution::STATE_PAUSED, $reason); |
| 104 | $result = true; |
| 105 | } |
| 106 | |
| 107 | return $result; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param DeliveryExecution $deliveryExecution |
| 112 | * @param string $state |
| 113 | * @param string|array|null $reason |
| 114 | * |
| 115 | * @return bool |
| 116 | * |
| 117 | * @throws common_exception_NotFound |
| 118 | */ |
| 119 | protected function setState(DeliveryExecution $deliveryExecution, string $state, $reason = null): bool |
| 120 | { |
| 121 | $previousState = $deliveryExecution->getState()->getUri(); |
| 122 | if ($previousState === $state) { |
| 123 | $this->logWarning( |
| 124 | "Delivery execution {$deliveryExecution->getIdentifier()} already in state {$state}" |
| 125 | ); |
| 126 | |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | $result = $deliveryExecution->getImplementation()->setState($state); |
| 131 | |
| 132 | $context = null; |
| 133 | |
| 134 | if ($state === DeliveryExecutionInterface::STATE_FINISHED) { |
| 135 | $user = common_session_SessionManager::getSession()->getUser(); |
| 136 | $context = new DeliveryExecutionStateContext([ |
| 137 | DeliveryExecutionStateContext::PARAM_USER => $user |
| 138 | ]); |
| 139 | } |
| 140 | |
| 141 | $this->emitEvent(new DeliveryExecutionStateEvent($deliveryExecution, $state, $previousState, $context)); |
| 142 | $this->logDebug(sprintf('DeliveryExecutionState from %s to %s triggered', $previousState, $state)); |
| 143 | |
| 144 | if (!$this->isStateInteractive($previousState) && $this->isStateInteractive($state)) { |
| 145 | $this->emitEvent( |
| 146 | new DeliveryExecutionReactivated( |
| 147 | $deliveryExecution, |
| 148 | $this->getSessionService()->getCurrentUser(), |
| 149 | $reason |
| 150 | ) |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | return $result; |
| 155 | } |
| 156 | |
| 157 | protected function getStorageEngine(): DeliveryExecutionService |
| 158 | { |
| 159 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 160 | return $this->getServiceLocator()->get(DeliveryExecutionService::SERVICE_ID); |
| 161 | } |
| 162 | |
| 163 | private function getSessionService(): SessionService |
| 164 | { |
| 165 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 166 | return $this->getServiceLocator()->get(SessionService::SERVICE_ID); |
| 167 | } |
| 168 | |
| 169 | private function getEventManager(): EventManager |
| 170 | { |
| 171 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
| 172 | return $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 173 | } |
| 174 | |
| 175 | private function emitEvent(Event $event): void |
| 176 | { |
| 177 | $this->getEventManager()->trigger($event); |
| 178 | } |
| 179 | |
| 180 | private function getReactivableStates(): array |
| 181 | { |
| 182 | if (!$this->hasOption(self::OPTION_REACTIVABLE_STATES)) { |
| 183 | return self::DEFAULT_REACTIVABLE_STATES; |
| 184 | } |
| 185 | |
| 186 | return $this->getOption(self::OPTION_REACTIVABLE_STATES); |
| 187 | } |
| 188 | |
| 189 | private function isStateInteractive(string $state): bool |
| 190 | { |
| 191 | return in_array($state, self::INTERACTIVE_STATES, true); |
| 192 | } |
| 193 | } |