Skip to content

Commit b14bdda

Browse files
committed
added file reader interface
1 parent 2109387 commit b14bdda

File tree

5 files changed

+55
-34
lines changed

5 files changed

+55
-34
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"src/Gendiff.php",
1919
"src/CommandInterface.php",
2020
"src/Command.php",
21-
"src/OutputInterface.php"
21+
"src/OutputInterface.php",
22+
"src/FileReaderInterface.php",
23+
"src/FileReader.php"
2224
]
2325
},
2426
"authors": [

src/Command.php

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,28 @@
44

55
use App\CommandInterface;
66
use App\OutputInterface;
7+
use App\FileReaderInterface;
8+
use App\FileReader;
79

810
class ViewFilesCommand implements CommandInterface
911
{
10-
protected $output;
11-
private const MAX_FILE_SIZE = 4096;
12+
protected FileReaderInterface $fileReader;
1213

13-
public function __construct(OutputInterface $output)
14+
public function __construct(FileReaderInterface $fileReader)
1415
{
15-
$this->output = $output;
16-
}
17-
18-
private function parseFile(string $filename): array
19-
{
20-
$handle = fopen($filename, "r");
21-
$result = json_decode(fread($handle, self::MAX_FILE_SIZE));
22-
fclose($handle);
23-
$type = gettype($result);
24-
if ($type === 'object') {
25-
$keys = (get_object_vars($result));
26-
} elseif ($type === 'array') {
27-
$keys = array_keys($result);
28-
} else {
29-
$keys = null;
30-
}
31-
32-
return $keys;
16+
$this->fileReader = $fileReader;
3317
}
3418

3519
public function execute(object $cliData)
3620
{
3721
if (isset($cliData['FILE1']) && isset($cliData['FILE2'])) {
38-
if (file_exists($cliData['FILE1']) && file_exists($cliData['FILE2'])) {
39-
$file1Content = $this->parseFile($cliData['FILE1']);
40-
$file2Content = $this->parseFile($cliData['FILE2']);
22+
$file1Content = $this->fileReader->readFile($cliData['FILE1']);
23+
$file2Content = $this->fileReader->readFile($cliData['FILE2']);
4124

42-
print_r("File1 content:\n");
43-
print_r($file1Content);
44-
print_r("File2 content:\n");
45-
print_r($file2Content);
46-
} else {
47-
print_r("File {$cliData['FILE1']} or {$cliData['FILE2']} not exists\n");
48-
}
25+
print_r("File1 content:\n");
26+
print_r($file1Content);
27+
print_r("File2 content:\n");
28+
print_r($file2Content);
4929
}
5030
}
5131
}

src/FileReader.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\FileReaderInterface;
6+
7+
class FileReader implements FileReaderInterface
8+
{
9+
private const MAX_FILE_SIZE = 4096;
10+
11+
public function readFile(string $filename): array | null
12+
{
13+
if (file_exists($filename)) {
14+
$handle = fopen($filename, "r");
15+
$result = json_decode(fread($handle, self::MAX_FILE_SIZE));
16+
fclose($handle);
17+
$type = gettype($result);
18+
if ($type === 'object') {
19+
return (get_object_vars($result));
20+
} elseif ($type === 'array') {
21+
return array_keys($result);
22+
} else {
23+
return null;
24+
}
25+
} else {
26+
return null;
27+
}
28+
}
29+
}

src/FileReaderInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App;
4+
5+
interface FileReaderInterface
6+
{
7+
public function readFile(string $filename): array | null;
8+
}

src/Gendiff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Docopt;
66
use App\OutputInterface;
77
use App\Command;
8+
use App\FileReader;
89

910
function runGendiff(): void
1011
{
@@ -33,8 +34,9 @@ public function parseCommandData(string $docopt): object
3334
return Docopt::handle($docopt, array('version' => '1.0.6'));
3435
}
3536
};
36-
$cliData = $output->parseCommandData($docopt);
3737

38-
$command = new ViewFilesCommand($output);
38+
$fileReader = new FileReader();
39+
$cliData = $output->parseCommandData($docopt);
40+
$command = new ViewFilesCommand($fileReader);
3941
$command->execute($cliData);
4042
}

0 commit comments

Comments
 (0)