Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| DeliveryServerService | |
0.00% |
0 / 31 |
|
0.00% |
0 / 7 |
210 | |
0.00% |
0 / 1 |
| singleton | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getResumableStates | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getResumableDeliveries | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| initResultServer | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDeliveryContainer | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getResultStoreWrapper | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getResultServerService | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| 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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoDelivery\model\execution; |
| 23 | |
| 24 | use common_Exception; |
| 25 | use Laminas\ServiceManager\ServiceLocatorAwareInterface; |
| 26 | use oat\oatbox\user\User; |
| 27 | use oat\oatbox\service\ServiceManager; |
| 28 | use oat\oatbox\service\ConfigurableService; |
| 29 | use oat\taoDelivery\model\RuntimeService; |
| 30 | use oat\taoDelivery\model\container\ExecutionContainer; |
| 31 | use oat\taoResultServer\models\classes\ResultServerService; |
| 32 | use oat\taoResultServer\models\classes\ResultStorageWrapper; |
| 33 | |
| 34 | /** |
| 35 | * Service to manage the execution of deliveries |
| 36 | * |
| 37 | * @access public |
| 38 | * @author Joel Bout, <joel@taotesting.com> |
| 39 | * @package taoDelivery |
| 40 | */ |
| 41 | class DeliveryServerService extends ConfigurableService |
| 42 | { |
| 43 | /** @deprecated */ |
| 44 | public const CONFIG_ID = 'taoDelivery/deliveryServer'; |
| 45 | |
| 46 | public const SERVICE_ID = 'taoDelivery/deliveryServer'; |
| 47 | |
| 48 | public const OPTION_RESULT_SERVER_SERVICE_FACTORY = 'resultServerServiceFactory'; |
| 49 | |
| 50 | /** @var ResultServerService */ |
| 51 | public $resultServerService = null; |
| 52 | |
| 53 | public static function singleton() |
| 54 | { |
| 55 | return ServiceManager::getServiceManager()->get(self::SERVICE_ID); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Return the states a delivery execution can be resumed from |
| 60 | * @return string[] |
| 61 | */ |
| 62 | public function getResumableStates() |
| 63 | { |
| 64 | return [ |
| 65 | DeliveryExecution::STATE_ACTIVE, |
| 66 | DeliveryExecution::STATE_PAUSED |
| 67 | ]; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get resumable (active) deliveries. |
| 72 | * @param User $user User instance. If not given then all deliveries will be returned regardless of user URI. |
| 73 | * @return \oat\taoDelivery\model\execution\DeliveryExecution [] |
| 74 | */ |
| 75 | public function getResumableDeliveries(User $user) |
| 76 | { |
| 77 | $deliveryExecutionService = ServiceProxy::singleton(); |
| 78 | $resumable = []; |
| 79 | |
| 80 | foreach ($this->getResumableStates() as $state) { |
| 81 | $executions = $deliveryExecutionService->getDeliveryExecutionsByStatus($user->getIdentifier(), $state); |
| 82 | |
| 83 | foreach ($executions as $execution) { |
| 84 | $delivery = $execution->getDelivery(); |
| 85 | if ($delivery->exists()) { |
| 86 | $resumable[] = $execution; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return $resumable; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Initialize the result server for a given execution |
| 96 | * |
| 97 | * @param $compiledDelivery |
| 98 | * @param string $deliveryExecutionId |
| 99 | */ |
| 100 | public function initResultServer($compiledDelivery, $deliveryExecutionId, $userUri) |
| 101 | { |
| 102 | $this->getResultServerService()->initResultServer($compiledDelivery, $deliveryExecutionId, $userUri); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns the container for the delivery execution |
| 107 | * |
| 108 | * @param DeliveryExecution $deliveryExecution |
| 109 | * @return ExecutionContainer |
| 110 | * @throws common_Exception |
| 111 | */ |
| 112 | public function getDeliveryContainer(DeliveryExecution $deliveryExecution) |
| 113 | { |
| 114 | $runtimeService = $this->getServiceLocator()->get(RuntimeService::SERVICE_ID); |
| 115 | $deliveryContainer = $runtimeService->getDeliveryContainer($deliveryExecution->getDelivery()->getUri()); |
| 116 | return $deliveryContainer->getExecutionContainer($deliveryExecution); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param string $deliveryExecutionId id expectected, but still accepts delivery executions for backward |
| 121 | * compatibility |
| 122 | */ |
| 123 | public function getResultStoreWrapper($deliveryExecutionId): ResultStorageWrapper |
| 124 | { |
| 125 | if ($deliveryExecutionId instanceof DeliveryExecutionInterface) { |
| 126 | $deliveryExecutionId = $deliveryExecutionId->getIdentifier(); |
| 127 | } |
| 128 | /** @var ResultServerService $resultService */ |
| 129 | $resultService = $this->getResultServerService(); |
| 130 | return new ResultStorageWrapper($deliveryExecutionId, $resultService->getResultStorage()); |
| 131 | } |
| 132 | |
| 133 | private function getResultServerService(): ResultServerService |
| 134 | { |
| 135 | if (null !== $this->resultServerService) { |
| 136 | return $this->resultServerService; |
| 137 | } |
| 138 | |
| 139 | $factory = $this->getOption(self::OPTION_RESULT_SERVER_SERVICE_FACTORY); |
| 140 | |
| 141 | if ($factory instanceof ResultServerServiceFactoryInterface) { |
| 142 | if ($factory instanceof ServiceLocatorAwareInterface) { |
| 143 | $this->propagate($factory); |
| 144 | } |
| 145 | $this->resultServerService = $factory->create(); |
| 146 | } else { |
| 147 | $this->resultServerService = $this->getServiceLocator()->get(ResultServerService::SERVICE_ID); |
| 148 | } |
| 149 | |
| 150 | return $this->resultServerService; |
| 151 | } |
| 152 | } |