Skip to content

Commit 3e576ab

Browse files
committed
update files reading executor
1 parent 121c1a0 commit 3e576ab

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

src/CommandFactory.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,29 @@
55
class CommandFactory implements CommandFactoryInterface
66
{
77
private string $docopt;
8+
private const COMMAND_LIST = ["parse", "difference", "show"];
89

910
public function __construct(string $docopt = '')
1011
{
1112
$this->docopt = $docopt;
1213
}
1314

14-
public function getCommand(string $commandType): CommandInterface | CommandLineParserInterface | null
15+
public function getCommandTypeList(): array
1516
{
16-
if (!strcmp($commandType, "parse")) {
17-
return new CommandLineParser($this->docopt);
18-
} elseif (!strcmp($commandType, "difference")) {
19-
return new FilesDiffCommand();
20-
} elseif (!strcmp($commandType, "show")) {
21-
return new DisplayCommand();
17+
return CommandFactory::COMMAND_LIST;
18+
}
19+
20+
public function getCommand(string $commandType): CommandInterface | CommandLineParserInterface
21+
{
22+
switch ($commandType) {
23+
case self::COMMAND_LIST[0]:
24+
return new CommandLineParser($this->docopt);
25+
case self::COMMAND_LIST[1]:
26+
return new FilesDiffCommand();
27+
case self::COMMAND_LIST[2]:
28+
return new DisplayCommand();
29+
default:
30+
throw new \Exception("Unknown command");
2231
}
23-
return null;
2432
}
2533
}

src/CommandFactoryInterface.php

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

55
interface CommandFactoryInterface
66
{
7-
public function getCommand(string $commandType): CommandInterface | CommandLineParserInterface | null;
7+
public function getCommandTypeList(): array;
8+
public function getCommand(string $commandType): CommandInterface | CommandLineParserInterface;
89
}

src/ConsoleApp.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
class ConsoleApp
66
{
77
private string $docopt;
8+
private array $commandTypeList;
9+
private $currentCommand;
810
private CommandFactoryInterface $commandFactory;
911
private CommandLineParserInterface $commandLineParser;
1012
private CommandInterface $filesDiffCommand;
@@ -32,10 +34,30 @@ public function __construct()
3234
DOCOPT;
3335

3436
$this->commandFactory = new CommandFactory($this->docopt);
37+
$this->commandTypeList = $this->commandFactory->getCommandTypeList();
3538
}
3639

3740
public function run(): void
3841
{
42+
foreach ($this->commandTypeList as $commandType) {
43+
$this->currentCommand = $this->commandFactory->getCommand($commandType);
44+
switch ($commandType) {
45+
case "parse":
46+
$this->cliData = $this->currentCommand->execute();
47+
break;
48+
case "difference":
49+
$this->filesContent = $this->currentCommand->setFileReader(new FileReader())
50+
->execute($this->cliData);
51+
break;
52+
case "show":
53+
$this->currentCommand->execute($this->filesContent)
54+
->showDiffsToConsole();
55+
break;
56+
default:
57+
throw new \Exception('unknown command type');
58+
}
59+
}
60+
/*
3961
if ($this->commandLineParser = $this->commandFactory->getCommand("parse")) {
4062
$this->cliData = $this->commandLineParser->execute();
4163
} else {
@@ -50,10 +72,11 @@ public function run(): void
5072
}
5173
5274
if ($this->displayCommand = $this->commandFactory->getCommand("show")) {
53-
$this->displayCommand->execute(/*(object)*/$this->filesContent)
75+
$this->displayCommand->execute($this->filesContent)
5476
->showDiffsToConsole();
5577
} else {
5678
throw new \Exception("can't create display command");
5779
}
80+
*/
5881
}
5982
}

src/DisplayCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ public function execute(object $data): object
2525

2626
$this->filesContent[] = "file1.json content:\n";
2727
$this->filesContent[] = array_reduce(
28-
get_object_vars($data)['file1'],
28+
$data->file1,
2929
[$this, 'constructContent'],
3030
"{"
3131
) . "\r}\n";
3232

3333
$this->filesContent[] = "file2.json content:\n";
3434
$this->filesContent[] = array_reduce(
35-
get_object_vars($data)['file2'],
35+
$data->file2,
3636
[$this, 'constructContent'],
3737
"{"
3838
) . "\n}\n";
3939

40-
$file1Array = get_object_vars($data)['file1'];
40+
$file1Array = $data->file1;
4141
$file1Keys = array_keys($file1Array);
42-
$file2Array = get_object_vars($data)['file2'];
42+
$file2Array = $data->file2;
4343
$this->filesDiffContent = array_map(
4444
function ($file1Key) use ($file1Array, $file2Array) {
4545
if (array_key_exists($file1Key, $file2Array)) {

src/FilesDiffCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function setFileReader(FileReaderInterface $fileReader): object
2626

2727
public function execute(object $cliData): object
2828
{
29-
if (isset($cliData['FILE1']) && isset($cliData['FILE2'])) {
30-
$this->file1Content = $this->fileReader->readFile($cliData['FILE1']);
31-
$this->file2Content = $this->fileReader->readFile($cliData['FILE2']);
29+
if (isset($cliData->args['FILE1']) && isset($cliData->args['FILE2'])) {
30+
$this->file1Content = $this->fileReader->readFile($cliData->args['FILE1']);
31+
$this->file2Content = $this->fileReader->readFile($cliData->args['FILE2']);
3232
}
3333

3434
return

0 commit comments

Comments
 (0)