Skip to content

Commit 41a8ba3

Browse files
Alexsey-VRAlexsey-VR
authored andcommitted
added command factory
1 parent c8f711f commit 41a8ba3

File tree

7 files changed

+72
-24
lines changed

7 files changed

+72
-24
lines changed

composer.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,7 @@
1515
},
1616
"files": [
1717
"vendor/docopt/docopt/src/docopt.php",
18-
"src/Gendiff.php",
19-
"src/CommandInterface.php",
20-
"src/FilesDiffCommand.php",
21-
"src/DisplayCommand.php",
22-
"src/OutputInterface.php",
23-
"src/Output.php",
24-
"src/FileReaderInterface.php",
25-
"src/FileReader.php"
18+
"src/Gendiff.php"
2619
]
2720
},
2821
"authors": [

src/CommandFactory.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Differ;
4+
5+
class CommandFactory implements CommandFactoryInterface
6+
{
7+
private string $docopt;
8+
9+
public function __construct(string $docopt = '')
10+
{
11+
$this->docopt = $docopt;
12+
}
13+
14+
public function getCommand(string $commandType): CommandInterface | OutputInterface | null
15+
{
16+
if (!strcmp($commandType, "parse")) {
17+
return new Output($this->docopt);
18+
} elseif (!strcmp($commandType, "difference")) {
19+
return new FilesDiffCommand();
20+
} elseif (!strcmp($commandType, "show")) {
21+
return new DisplayCommand();
22+
}
23+
return null;
24+
}
25+
}

src/CommandFactoryInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Differ;
4+
5+
interface CommandFactoryInterface
6+
{
7+
public function getCommand(string $commandType): CommandInterface | OutputInterface | null;
8+
}

src/DisplayCommand.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public function __construct()
1111
{
1212
$this->filesContent = [];
1313
$this->filesDiffContent = [];
14+
15+
return $this;
1416
}
1517

1618
private function constructContent($accum, $item)
@@ -25,20 +27,21 @@ public function execute(object $data): object
2527
$this->filesContent[] = array_reduce(
2628
get_object_vars($data)['file1'],
2729
[$this, 'constructContent'],
28-
"{") . "\r}\n";
30+
"{"
31+
) . "\r}\n";
2932

3033
$this->filesContent[] = "file2.json content:\n";
3134
$this->filesContent[] = array_reduce(
3235
get_object_vars($data)['file2'],
3336
[$this, 'constructContent'],
34-
"{") . "\n}\n";
37+
"{"
38+
) . "\n}\n";
3539

3640
$file1Array = get_object_vars($data)['file1'];
3741
$file1Keys = array_keys($file1Array);
3842
$file2Array = get_object_vars($data)['file2'];
3943
$this->filesDiffContent = array_map(
40-
function($file1Key) use ($file1Array, $file2Array)
41-
{
44+
function ($file1Key) use ($file1Array, $file2Array) {
4245
if (array_key_exists($file1Key, $file2Array)) {
4346
if (!strcmp($file1Array[$file1Key], $file2Array[$file1Key])) {
4447
return " " . $file1Array[$file1Key] . "\n";
@@ -49,7 +52,8 @@ function($file1Key) use ($file1Array, $file2Array)
4952
} else {
5053
return " - " . $file1Array[$file1Key] . "\n";
5154
}
52-
}, $file1Keys
55+
},
56+
$file1Keys
5357
);
5458
return $this;
5559
}

src/FilesDiffCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public function __construct()
1313
{
1414
$this->file1Content = [];
1515
$this->file2Content = [];
16+
17+
return $this;
1618
}
1719

1820
public function setFileReader(FileReaderInterface $fileReader): object

src/Gendiff.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,30 @@ function runGendiff(): void
2222

2323
DOCOPT;
2424

25-
$output = new Output($docopt);
26-
$cliData = $output->parseCommandData();
27-
28-
$filesDiffCommand = new FilesDiffCommand();
29-
$filesContent = $filesDiffCommand->setFileReader(new FileReader())
30-
->execute($cliData);
31-
32-
$displayCommand = new DisplayCommand();
33-
$displayCommand->execute((object)$filesContent)
34-
->showDiffsToConsole();
25+
$factory = new CommandFactory($docopt);
26+
27+
$outputParser = $factory->getCommand("parse");
28+
if (is_null($outputParser)) {
29+
print_r("error: can't create command to parse command data\n");
30+
} else {
31+
$cliData = $outputParser->parseCommandData();
32+
}
33+
34+
$filesDiffCommand = $factory->getCommand("difference");
35+
if (is_null($filesDiffCommand)) {
36+
print_r("error: can't create command to get files difference\n");
37+
exit;
38+
} else {
39+
$filesContent = $filesDiffCommand->setFileReader(new FileReader())
40+
->execute($cliData);
41+
}
42+
43+
$displayCommand = $factory->getCommand("show");
44+
if (is_null($displayCommand)) {
45+
print_r("error: can't create command to show differences to console\n");
46+
exit;
47+
} else {
48+
$displayCommand->execute((object)$filesContent)
49+
->showDiffsToConsole();
50+
}
3551
}

src/Output.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public function __construct(string $docopt)
1515

1616
public function parseCommandData(): object
1717
{
18-
return (new Docopt)->handle($this->docopt, array('version' => '1.0.6'));
18+
return (new Docopt())->handle($this->docopt, array('version' => '1.0.6'));
1919
}
2020
}

0 commit comments

Comments
 (0)