getOption(self::OPTION_PERSISTENCE) ?: 'default'; return $this->getServiceLocator()->get(\common_persistence_Manager::SERVICE_ID)->getPersistenceById($persistenceId); } /** * @return QueryBuilder * @throws \oat\oatbox\service\exception\InvalidServiceManagerException */ private function getQueryBuilder() { /**@var \common_persistence_sql_pdo_mysql_Driver $driver */ return $this->getPersistence()->getPlatForm()->getQueryBuilder(); } /** * Updates one state * * @param string $deliveryExecutionId * @param string $toolName * @param string $state * @return bool whether the value has actually changed in the storage * @throws \oat\oatbox\service\exception\InvalidServiceManagerException */ private function updateState($deliveryExecutionId, $toolName, $state) { $qb = $this->getQueryBuilder() ->update(self::TABLE_NAME) ->set(self::COLUMN_TOOL_STATE, ':state') ->where(self::COLUMN_DELIVERY_EXECUTION_ID . ' = :delivery_execution_id') ->andWhere(self::COLUMN_TOOL_NAME . ' = :tool_name') ->setParameter('state', $state) ->setParameter('delivery_execution_id', $deliveryExecutionId) ->setParameter('tool_name', $toolName); return $qb->execute() !== 0; } /** * Updates those states which are already persisted in the storage and inserts new ones * * @param string $deliveryExecutionId * @param array $states * @throws \oat\oatbox\service\exception\InvalidServiceManagerException */ public function storeStates($deliveryExecutionId, $states) { foreach ($states as $toolName => $state) { $hasRowActuallyChanged = $this->updateState($deliveryExecutionId, $toolName, $state); if (!$hasRowActuallyChanged) { try { $this->getPersistence()->insert(self::TABLE_NAME, [ self::COLUMN_DELIVERY_EXECUTION_ID => $deliveryExecutionId, self::COLUMN_TOOL_NAME => $toolName, self::COLUMN_TOOL_STATE => $state, ]); } catch (\PDOException $exception) { // when PDO implementation of RDS is used as a persistence // unfortunately the exception is very broad so it can cover more than intended cases } catch (UniqueConstraintViolationException $exception) { // when DBAL implementation of RDS is used as a persistence } } }; } /** * @param $deliveryExecutionId * @return array * @throws \oat\oatbox\service\exception\InvalidServiceManagerException */ public function getStates($deliveryExecutionId) { $qb = $this->getQueryBuilder() ->select('*') ->from(self::TABLE_NAME) ->where(self::COLUMN_DELIVERY_EXECUTION_ID . ' = :delivery_execution_id') ->setParameter('delivery_execution_id', $deliveryExecutionId); $returnValue = []; foreach ($qb->execute()->fetchAll() as $variable) { $returnValue[$variable[self::COLUMN_TOOL_NAME]] = $variable[self::COLUMN_TOOL_STATE]; } return $returnValue; } /** * @param string $deliveryExecutionId * @return bool whether deleted successfully * @throws \oat\oatbox\service\exception\InvalidServiceManagerException */ public function deleteStates($deliveryExecutionId) { $this->getQueryBuilder() ->delete(self::TABLE_NAME) ->where(self::COLUMN_DELIVERY_EXECUTION_ID . ' = :delivery_execution_id') ->setParameter('delivery_execution_id', $deliveryExecutionId) ->execute(); return true; } }