fix missing final keyword, keep file open for csv writer

main
Mathias Rothenhaeusler 2023-08-03 21:02:27 +02:00
parent 5dce67ed1e
commit 630845e0bd
6 changed files with 42 additions and 15 deletions

1
.gitignore vendored
View File

@ -4,5 +4,4 @@
log.txt log.txt
result.csv result.csv
.run .run
*.lock
.php-cs-fixer.cache .php-cs-fixer.cache

View File

@ -54,14 +54,8 @@ _numbers are 3 and 0 are wrong, is not allowed_ <br/>
## Task ## Task
You need to refactor code and write it on proper way. Just do your best: update/delete/add code as you wish. You need to refactor code and write it on proper way. Just do your best: update/delete/add code as you wish.
After finishing - please push your code in your github/bitbucket account, and send me link back.
### Requirements ### Requirements
* After refactoring code shoud work * After refactoring code shoud work
* Code should work on PHP8.0+ * Code should work on PHP8.0+
* As file source example please use test.csv * As file source example please use test.csv
### Result
Please put result of your work in your Github or Bitbucket account, and send link back.

20
composer.lock generated 100644
View File

@ -0,0 +1,20 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "63e7cd6af94fff1be77b03d112d0b91c",
"packages": [],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": ">=8.1"
},
"platform-dev": [],
"plugin-api-version": "2.3.0"
}

View File

@ -6,23 +6,37 @@ namespace App\Csv;
use RuntimeException; use RuntimeException;
class CsvWriter final class CsvWriter
{ {
private string $separator = ';'; private string $separator = ';';
/**
* @var false|resource
*/
private $fileHandle;
public function __construct(private readonly string $filePath) public function __construct(private readonly string $filePath)
{ {
file_put_contents($this->filePath, ''); file_put_contents($this->filePath, '');
} }
/**
* @param array<int,mixed> $data
*/
public function addLine(array $data): void public function addLine(array $data): void
{ {
$handle = fopen($this->filePath, 'ab'); if (!$this->fileHandle) {
if (!$handle) { $this->fileHandle = fopen($this->filePath, 'ab');
throw new RuntimeException('Could not open file.'); if (!$this->fileHandle) {
throw new RuntimeException('Could not open file.');
}
} }
fputcsv($handle, $data, $this->separator); fputcsv($this->fileHandle, $data, $this->separator);
fclose($handle); }
public function __destruct()
{
fclose($this->fileHandle);
} }
} }

View File

@ -10,7 +10,7 @@ use App\Logger\LoggerInterface;
use App\Operations\Exceptions\InvalidResultException; use App\Operations\Exceptions\InvalidResultException;
use App\Operations\MathOperationInterface; use App\Operations\MathOperationInterface;
class CsvCalculationService private class CsvCalculationService
{ {
public function __construct( public function __construct(
private readonly CsvReader $csvReader, private readonly CsvReader $csvReader,

View File

@ -12,7 +12,7 @@ use App\Operations\Subtract;
use App\Operations\Multiply; use App\Operations\Multiply;
use App\Options\CliOption; use App\Options\CliOption;
class MathOperationFactory final class MathOperationFactory
{ {
public static function getOperationFromCliOption(CliOption $cliOption): MathOperationInterface public static function getOperationFromCliOption(CliOption $cliOption): MathOperationInterface
{ {