Console Tool Refactoring
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Csv\CsvReader;
|
||||
use App\Csv\CsvWriter;
|
||||
use App\Logger\LoggerInterface;
|
||||
use App\Operations\Exceptions\InvalidResultException;
|
||||
use App\Operations\MathOperationInterface;
|
||||
|
||||
class CsvCalculationService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CsvReader $csvReader,
|
||||
private readonly CsvWriter $csvWriter,
|
||||
private readonly MathOperationInterface $operation,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
public function execute(): void
|
||||
{
|
||||
$this->logger->logMessage(sprintf('Start %s operation.%s', $this->operation->getDescription(), PHP_EOL));
|
||||
foreach ($this->csvReader->readData() as $data) {
|
||||
try {
|
||||
$result = $this->operation->calculate($data->number1, $data->number2);
|
||||
$this->csvWriter->addLine(
|
||||
[$data->number1, $data->number2, $result]
|
||||
);
|
||||
} catch (InvalidResultException $e) {
|
||||
$this->logger->logMessage(
|
||||
sprintf(
|
||||
'Numbers %d and %d are wrong. %s%s',
|
||||
$data->number1,
|
||||
$data->number2,
|
||||
$e->getMessage(),
|
||||
PHP_EOL
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$this->logger->logMessage(sprintf('Finished %s operation.%s', $this->operation->getDescription(), PHP_EOL));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user