Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.00% |
38 / 40 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| RdsToolsStateStorage | |
95.00% |
38 / 40 |
|
83.33% |
5 / 6 |
12 | |
0.00% |
0 / 1 |
| getPersistence | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getQueryBuilder | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| updateState | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| storeStates | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
5.20 | |||
| getStates | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| deleteStates | |
100.00% |
6 / 6 |
|
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) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiTest\models\runner\toolsStates; |
| 23 | |
| 24 | use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
| 25 | use Doctrine\DBAL\Query\QueryBuilder; |
| 26 | |
| 27 | class RdsToolsStateStorage extends ToolsStateStorage |
| 28 | { |
| 29 | /** |
| 30 | * Constants for the database creation and data access |
| 31 | * |
| 32 | */ |
| 33 | public const TABLE_NAME = 'runner_tool_states'; |
| 34 | public const COLUMN_DELIVERY_EXECUTION_ID = 'delivery_execution_id'; |
| 35 | public const COLUMN_TOOL_NAME = 'tool_name'; |
| 36 | public const COLUMN_TOOL_STATE = 'tool_state'; |
| 37 | |
| 38 | /** |
| 39 | * @return \common_persistence_SqlPersistence |
| 40 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 41 | */ |
| 42 | protected function getPersistence() |
| 43 | { |
| 44 | $persistenceId = $this->getOption(self::OPTION_PERSISTENCE) ?: 'default'; |
| 45 | return $this |
| 46 | ->getServiceLocator() |
| 47 | ->get(\common_persistence_Manager::SERVICE_ID) |
| 48 | ->getPersistenceById($persistenceId); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @return QueryBuilder |
| 53 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 54 | */ |
| 55 | private function getQueryBuilder() |
| 56 | { |
| 57 | /**@var \common_persistence_sql_pdo_mysql_Driver $driver */ |
| 58 | return $this->getPersistence()->getPlatForm()->getQueryBuilder(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Updates one state |
| 63 | * |
| 64 | * @param string $deliveryExecutionId |
| 65 | * @param string $toolName |
| 66 | * @param string $state |
| 67 | * @return bool whether the value has actually changed in the storage |
| 68 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 69 | */ |
| 70 | private function updateState($deliveryExecutionId, $toolName, $state) |
| 71 | { |
| 72 | $qb = $this->getQueryBuilder() |
| 73 | ->update(self::TABLE_NAME) |
| 74 | ->set(self::COLUMN_TOOL_STATE, ':state') |
| 75 | ->where(self::COLUMN_DELIVERY_EXECUTION_ID . ' = :delivery_execution_id') |
| 76 | ->andWhere(self::COLUMN_TOOL_NAME . ' = :tool_name') |
| 77 | ->setParameter('state', $state) |
| 78 | ->setParameter('delivery_execution_id', $deliveryExecutionId) |
| 79 | ->setParameter('tool_name', $toolName); |
| 80 | |
| 81 | return $qb->execute() !== 0; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Updates those states which are already persisted in the storage and inserts new ones |
| 86 | * |
| 87 | * @param string $deliveryExecutionId |
| 88 | * @param array $states |
| 89 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 90 | */ |
| 91 | public function storeStates($deliveryExecutionId, $states) |
| 92 | { |
| 93 | foreach ($states as $toolName => $state) { |
| 94 | $hasRowActuallyChanged = $this->updateState($deliveryExecutionId, $toolName, $state); |
| 95 | if (!$hasRowActuallyChanged) { |
| 96 | try { |
| 97 | $this->getPersistence()->insert(self::TABLE_NAME, [ |
| 98 | self::COLUMN_DELIVERY_EXECUTION_ID => $deliveryExecutionId, |
| 99 | self::COLUMN_TOOL_NAME => $toolName, |
| 100 | self::COLUMN_TOOL_STATE => $state, |
| 101 | ]); |
| 102 | } catch (\PDOException $exception) { |
| 103 | // when PDO implementation of RDS is used as a persistence |
| 104 | // unfortunately the exception is very broad so it can cover more than intended cases |
| 105 | } catch (UniqueConstraintViolationException $exception) { |
| 106 | // when DBAL implementation of RDS is used as a persistence |
| 107 | } |
| 108 | } |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param $deliveryExecutionId |
| 114 | * @return array |
| 115 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 116 | */ |
| 117 | public function getStates($deliveryExecutionId) |
| 118 | { |
| 119 | $qb = $this->getQueryBuilder() |
| 120 | ->select('*') |
| 121 | ->from(self::TABLE_NAME) |
| 122 | ->where(self::COLUMN_DELIVERY_EXECUTION_ID . ' = :delivery_execution_id') |
| 123 | ->setParameter('delivery_execution_id', $deliveryExecutionId); |
| 124 | |
| 125 | $returnValue = []; |
| 126 | |
| 127 | foreach ($qb->execute()->fetchAll() as $variable) { |
| 128 | $returnValue[$variable[self::COLUMN_TOOL_NAME]] = $variable[self::COLUMN_TOOL_STATE]; |
| 129 | } |
| 130 | |
| 131 | return $returnValue; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param string $deliveryExecutionId |
| 136 | * @return bool whether deleted successfully |
| 137 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 138 | */ |
| 139 | public function deleteStates($deliveryExecutionId) |
| 140 | { |
| 141 | $this->getQueryBuilder() |
| 142 | ->delete(self::TABLE_NAME) |
| 143 | ->where(self::COLUMN_DELIVERY_EXECUTION_ID . ' = :delivery_execution_id') |
| 144 | ->setParameter('delivery_execution_id', $deliveryExecutionId) |
| 145 | ->execute(); |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | } |