Skip to content

Commit 2109387

Browse files
committed
using DIP principle
1 parent ee2fca9 commit 2109387

File tree

4 files changed

+13
-42
lines changed

4 files changed

+13
-42
lines changed

src/Command.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@
55
use App\CommandInterface;
66
use App\OutputInterface;
77

8-
class Command implements CommandInterface
8+
class ViewFilesCommand implements CommandInterface
99
{
1010
protected $output;
11-
protected $docopt;
12-
protected $cliData;
1311
private const MAX_FILE_SIZE = 4096;
1412

15-
public function __construct(OutputInterface $output, string $doc)
13+
public function __construct(OutputInterface $output)
1614
{
1715
$this->output = $output;
18-
$this->docopt = $doc;
19-
$this->cliData = $this->output->parseCommandData($this->docopt);
2016
}
2117

2218
private function parseFile(string $filename): array
@@ -36,22 +32,20 @@ private function parseFile(string $filename): array
3632
return $keys;
3733
}
3834

39-
public function execute()
35+
public function execute(object $cliData)
4036
{
41-
if (isset($this->cliData['FILE1']) & isset($this->cliData['FILE2'])) {
42-
if (file_exists($this->cliData['FILE1']) & file_exists($this->cliData['FILE2'])) {
43-
$file1Content = $this->parseFile($this->cliData['FILE1']);
44-
$file2Content = $this->parseFile($this->cliData['FILE2']);
37+
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']);
4541

4642
print_r("File1 content:\n");
4743
print_r($file1Content);
4844
print_r("File2 content:\n");
4945
print_r($file2Content);
5046
} else {
51-
print_r("File {$this->cliData['FILE2']} not exists\n");
47+
print_r("File {$cliData['FILE1']} or {$cliData['FILE2']} not exists\n");
5248
}
53-
} else {
54-
print_r("File {$this->cliData['FILE1']} not exists\n");
5549
}
5650
}
5751
}

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();
7+
public function execute(object $cliData);
88
}

src/Gendiff.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
use Docopt;
66
use App\OutputInterface;
7-
use App\CommandInterface;
8-
use App\Invoker;
97
use App\Command;
108

119
function runGendiff(): void
1210
{
1311

14-
$doc = <<<'DOCOPT'
12+
$docopt = <<<'DOCOPT'
1513
gendiff -h
1614
1715
Generate diff
@@ -35,9 +33,8 @@ public function parseCommandData(string $docopt): object
3533
return Docopt::handle($docopt, array('version' => '1.0.6'));
3634
}
3735
};
36+
$cliData = $output->parseCommandData($docopt);
3837

39-
$command = new Command($output, $doc);
40-
$invoker = new Invoker();
41-
$invoker->setCommand($command);
42-
$invoker->run();
38+
$command = new ViewFilesCommand($output);
39+
$command->execute($cliData);
4340
}

src/Invoker.php

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

0 commit comments

Comments
 (0)