Skip to content

Commit 464caab

Browse files
committed
files processing and console output are separated
1 parent b14bdda commit 464caab

File tree

9 files changed

+129
-49
lines changed

9 files changed

+129
-49
lines changed

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
"vendor/docopt/docopt/src/docopt.php",
1818
"src/Gendiff.php",
1919
"src/CommandInterface.php",
20-
"src/Command.php",
20+
"src/FilesDiffCommand.php",
21+
"src/DisplayCommand.php",
2122
"src/OutputInterface.php",
23+
"src/Output.php",
2224
"src/FileReaderInterface.php",
2325
"src/FileReader.php"
2426
]

src/Command.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/CommandInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
interface CommandInterface
66
{
7-
public function execute(object $cliData);
7+
public function execute(object $data): object | null;
88
}

src/DisplayCommand.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\CommandInterface;
6+
7+
class DisplayCommand implements CommandInterface
8+
{
9+
private array $filesDiffContent;
10+
11+
public function __construct()
12+
{
13+
$this->filesDiffContent = [];
14+
}
15+
16+
private function constructContent($accum, $item)
17+
{
18+
return $accum .= "\n " . $item;
19+
}
20+
21+
public function execute(object $data): object
22+
{
23+
24+
$this->filesDiffContent[] = "file1.json content:\n";
25+
$this->filesDiffContent[] = array_reduce(
26+
get_object_vars($data)['file1'],
27+
[$this, 'constructContent'],
28+
"{") . "\r}\n";
29+
30+
$this->filesDiffContent[] = "file2.json content:\n";
31+
$this->filesDiffContent[] = array_reduce(
32+
get_object_vars($data)['file2'],
33+
[$this, 'constructContent'],
34+
"{") . "\n}\n";
35+
36+
return $this;
37+
}
38+
39+
public function showDiffsToConsole()
40+
{
41+
echo implode("", $this->filesDiffContent);
42+
}
43+
}

src/FileReader.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class FileReader implements FileReaderInterface
88
{
99
private const MAX_FILE_SIZE = 4096;
1010

11+
public function __construct()
12+
{
13+
}
14+
1115
public function readFile(string $filename): array | null
1216
{
1317
if (file_exists($filename)) {
@@ -18,7 +22,7 @@ public function readFile(string $filename): array | null
1822
if ($type === 'object') {
1923
return (get_object_vars($result));
2024
} elseif ($type === 'array') {
21-
return array_keys($result);
25+
return $result;
2226
} else {
2327
return null;
2428
}

src/FilesDiffCommand.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\CommandInterface;
6+
use App\FileReaderInterface;
7+
use App\FileReader;
8+
9+
class FilesDiffCommand implements CommandInterface
10+
{
11+
private FileReaderInterface $fileReader;
12+
private array $file1Content;
13+
private array $file2Content;
14+
private array $filesDiffContent;
15+
16+
public function __construct()
17+
{
18+
$this->file1Content = [];
19+
$this->file2Content = [];
20+
}
21+
22+
public function setFileReader(FileReaderInterface $fileReader): object
23+
{
24+
$this->fileReader = $fileReader;
25+
26+
return $this;
27+
}
28+
29+
public function execute(object $cliData): object
30+
{
31+
if (isset($cliData['FILE1']) && isset($cliData['FILE2'])) {
32+
$this->file1Content = $this->fileReader->readFile($cliData['FILE1']);
33+
$this->file2Content = $this->fileReader->readFile($cliData['FILE2']);
34+
}
35+
36+
return
37+
(object)[
38+
'file1' => $this->file1Content,
39+
'file2' => $this->file2Content
40+
];
41+
}
42+
}

src/Gendiff.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace App;
44

5-
use Docopt;
65
use App\OutputInterface;
7-
use App\Command;
6+
use App\Output;
7+
use App\FilesDiffCommand;
8+
use App\DisplayCommand;
89
use App\FileReader;
910

1011
function runGendiff(): void
@@ -27,16 +28,14 @@ function runGendiff(): void
2728

2829
DOCOPT;
2930

30-
$output = new class implements OutputInterface
31-
{
32-
public function parseCommandData(string $docopt): object
33-
{
34-
return Docopt::handle($docopt, array('version' => '1.0.6'));
35-
}
36-
};
37-
38-
$fileReader = new FileReader();
39-
$cliData = $output->parseCommandData($docopt);
40-
$command = new ViewFilesCommand($fileReader);
41-
$command->execute($cliData);
31+
$output = new Output($docopt);
32+
$cliData = $output->parseCommandData();
33+
34+
$filesDiffCommand = new FilesDiffCommand();
35+
$filesContent = $filesDiffCommand->setFileReader(new FileReader())
36+
->execute($cliData);
37+
38+
$displayCommand = new DisplayCommand();
39+
$displayCommand->execute((object)$filesContent)
40+
->showDiffsToConsole();
4241
}

src/Output.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\OutputInterface;
6+
use Docopt;
7+
8+
class Output implements OutputInterface
9+
{
10+
private string $docopt;
11+
12+
public function __construct(string $docopt)
13+
{
14+
$this->docopt = $docopt;
15+
}
16+
17+
public function parseCommandData(): object
18+
{
19+
return (new Docopt)->handle($this->docopt, array('version' => '1.0.6'));
20+
}
21+
}

src/OutputInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
interface OutputInterface
66
{
7-
public function parseCommandData(string $docopt): object;
7+
public function parseCommandData(): object;
88
}

0 commit comments

Comments
 (0)