*/ declare(strict_types=1); namespace oat\taoQtiTest\models\runner\synchronisation\synchronisationService; use common_exception_InconsistentData; use oat\oatbox\service\ConfigurableService; use oat\taoQtiTest\models\runner\synchronisation\TestRunnerAction; use ResolverException; class TestRunnerActionResolver extends ConfigurableService { /** * @param array $data * @param array $availableActions * @return TestRunnerAction * @throws common_exception_InconsistentData * @throws ResolverException */ public function resolve(array $data, array $availableActions): TestRunnerAction { $this->checkData($data); $actionName = $data['action']; $actionClass = (string) ($availableActions[$actionName] ?? ''); $this->checkClass($actionClass, $actionName); return $this->propagate(new $actionClass($actionName, $data['timestamp'], $data['parameters'])); } /** * @param $data * @throws common_exception_InconsistentData */ private function checkData($data): void { if (!isset( $data['action'], $data['timestamp'], $data['parameters']) ) { throw new common_exception_InconsistentData( 'Action parameters have to contain "action", "timestamp" and "parameters" fields.' ); } if (!is_array($data['parameters'])) { throw new common_exception_InconsistentData( 'Action parameters have to contain "parameters" field as an array.' ); } } /** * @param $actionClass * @param string $actionName * @throws ResolverException */ private function checkClass(string $actionClass, string $actionName): void { if (!is_a($actionClass, TestRunnerAction::class, true)) { throw new ResolverException('Action name "' . $actionName . '" could not be resolved.'); } } }