/** * @param $algorithmType * @param null $constructParameters * @return null * @throws \Exception * * Create concrete class instance of crypto algorithm implementation */ public static function createAlgorithm($algorithmType, $constructParameters = null) { if (0 === strlen((string) $algorithmType)) { throw new \Exception('Algorithm type is empty!'); } try { self::$algorithmMap = Config\Parameters::getParameter('algorithmMap'); } catch (Exceptions\ParameterNotExistException $e) { //Catch some not good exception (I've decided that it is fatal Exception) //Try to log it Logger::log($e->getMessage(), $algorithmType); return null; } /** * @var \Library\Interfaces\Algorithm; */ if (isset(self::$algorithmMap[$algorithmType])) { $class = self::$algorithmMap[$algorithmType]['class']; if (!class_exists($class)) { throw new \Exception('Class not exist!'); } if (!is_null(self::$algorithmMap[$algorithmType]['construct_parameters'])) { $parametersCount = count(self::$algorithmMap[$algorithmType]['construct_parameters']['parameters']); switch ($parametersCount) { case 1: if (!is_null($constructParameters) && is_array($constructParameters) && 1 !== count($constructParameters)) { throw new \Exception('Bad input constructor parameter'); } break; default: throw new \Exception('Multiple input constructor parameters doesn\'t implement yet!'); break; } } return new $class($constructParameters); } else { throw new \Exception('Bad input parameter!'); } }
//Get user input function read_stdin() { $fr = fopen("php://stdin", "r"); $input = fgets($fr, 128); $input = rtrim($input); fclose($fr); return $input; } //Get some config params try { $algorithms = Parameters::getParameter('algorithms'); $algorithmMap = Parameters::getParameter('algorithmMap'); $methods = Parameters::getParameter('methods'); } catch (ParameterNotExistException $e) { Logger::log($e->getMessage(), 'Methods not exist!'); return; } $i = 0; $k = 0; $constructParameters = null; //A-a-a-and, prepare view for user echo "==============================================================\n"; echo "Choose algorithm you want to test (Press number to choose it):\n"; foreach ($algorithms as $algorithm) { echo "\t" . ++$i . ") " . $algorithm . "\n"; } echo "\t" . ($i + 1) . ") Or run tests!\n"; $chosenAlgorithm = read_stdin(); if ($i + 1 == $chosenAlgorithm) { Util::runTests();