Console Tool Refactoring
This commit is contained in:
+37
-37
@@ -1,46 +1,46 @@
|
||||
<?php
|
||||
|
||||
$shortopts = "a:f:";
|
||||
$longopts = array(
|
||||
"action:",
|
||||
"file:",
|
||||
);
|
||||
declare(strict_types=1);
|
||||
|
||||
$options = getopt($shortopts, $longopts);
|
||||
use App\Csv\CsvReader;
|
||||
use App\Csv\CsvWriter;
|
||||
use App\CsvCalculationService;
|
||||
use App\Logger\FileLogger;
|
||||
use App\MathOperationFactory;
|
||||
use App\Options\CliOption;
|
||||
use App\Options\Long;
|
||||
use App\Options\Short;
|
||||
|
||||
if(isset($options['a'])) {
|
||||
$action = $options['a'];
|
||||
} elseif(isset($options['action'])) {
|
||||
$action = $options['action'];
|
||||
} else {
|
||||
$action = "xyz";
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
if (count($argv) < 4) {
|
||||
echo 'Usage: php console.php -a <action> -f <file>' . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if(isset($options['f'])) {
|
||||
$file = $options['f'];
|
||||
} elseif(isset($options['file'])) {
|
||||
$file = $options['file'];
|
||||
} else {
|
||||
$file = "notexists.csv";
|
||||
|
||||
$options = getopt(Short::asString(), Long::asArray());
|
||||
|
||||
if (!$options) {
|
||||
echo 'Error getting arguments' . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
if ($action == "plus") {
|
||||
include 'files/ClassOne.php';
|
||||
$classOne = new ClassOne($file);
|
||||
} elseif ($action == "minus") {
|
||||
include 'files/ClassTwo.php';
|
||||
$classTwo = new ClassTwo($file, "minus");
|
||||
$classTwo->start();
|
||||
} elseif ($action == "multiply") {
|
||||
include 'files/Classthree.php';
|
||||
$classThree = new Classthree();
|
||||
$classThree->setFile($file);
|
||||
$classThree->execute();
|
||||
} elseif ($action == "division") {
|
||||
include 'files/classFour.php';
|
||||
$classFouyr = new classFour($file);
|
||||
} else {
|
||||
throw new \Exception("Wrong action is selected");
|
||||
}
|
||||
} catch (\Exception $exception) {}
|
||||
$cliOption = CliOption::fromArray($options);
|
||||
} catch (RuntimeException $e) {
|
||||
echo $e->getMessage();
|
||||
exit(1);
|
||||
}
|
||||
|
||||
try {
|
||||
$csvCalculationService = new CsvCalculationService(
|
||||
new CsvReader(__DIR__ . '/test.csv'),
|
||||
new CsvWriter(__DIR__ . '/result.csv'),
|
||||
MathOperationFactory::getOperationFromCliOption($cliOption),
|
||||
new FileLogger(),
|
||||
);
|
||||
$csvCalculationService->execute();
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user