Console Tool Refactoring

This commit is contained in:
2023-07-26 18:21:51 +02:00
parent b5836f4ae9
commit 10d7b09c5f
36 changed files with 1605 additions and 427 deletions
+37 -37
View File
@@ -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();
}