|
4 | 4 |
|
5 | 5 | class DisplayCommand implements CommandInterface |
6 | 6 | { |
7 | | - // Property to store displaying mode for debug |
8 | 7 | private string $mode; |
| 8 | + private CommandInterface $formatCommand; |
| 9 | + private const string MODE_EXCEPTION_MESSAGE = "internal error: unknown mode for display\n"; |
9 | 10 | public const AVAILABLE_MODES = [ |
10 | | - "differents", |
11 | | - "content" |
| 11 | + "differents" => "differents", |
| 12 | + "content" => "content" |
12 | 13 | ]; |
13 | 14 |
|
14 | | - public function __construct(string $mode = self::AVAILABLE_MODES[0]) |
| 15 | + public function __construct(string $mode = self::AVAILABLE_MODES["differents"]) |
15 | 16 | { |
16 | 17 | $this->mode = $mode; |
17 | 18 | } |
18 | 19 |
|
| 20 | + public function setFormatter(CommandInterface $formatter) |
| 21 | + { |
| 22 | + $this->formatCommand = $formatter; |
| 23 | + |
| 24 | + return $this; |
| 25 | + } |
| 26 | + |
| 27 | + public function getFilesContent(): string |
| 28 | + { |
| 29 | + return $this->formatCommand->getContentString(); |
| 30 | + } |
| 31 | + |
| 32 | + public function getFilesDiffs(): string |
| 33 | + { |
| 34 | + return $this->formatCommand->getDiffsString(); |
| 35 | + } |
| 36 | + |
19 | 37 | public function execute(CommandInterface $command = null): CommandInterface |
20 | 38 | { |
21 | | - if (!is_null($command)) { |
22 | | - switch ($this->mode) { |
23 | | - case self::AVAILABLE_MODES[0]: |
24 | | - print_r($command->getFilesDiffs()); |
25 | | - break; |
26 | | - case self::AVAILABLE_MODES[1]: |
27 | | - print_r($command->getFilesContent()); |
28 | | - break; |
29 | | - default: |
30 | | - throw new DifferException("internal error: unknown mode for display\n"); |
31 | | - } |
| 39 | + $this->formatCommand = $command; |
| 40 | + switch ($this->mode) { |
| 41 | + case self::AVAILABLE_MODES["differents"]: |
| 42 | + print_r($this->getFilesDiffs()); |
| 43 | + break; |
| 44 | + case self::AVAILABLE_MODES["content"]: |
| 45 | + print_r($this->getFilesContent()); |
| 46 | + break; |
| 47 | + default: |
| 48 | + throw new DifferException(self::MODE_EXCEPTION_MESSAGE); |
32 | 49 | } |
33 | 50 |
|
34 | 51 | return $this; |
35 | 52 | } |
36 | 53 |
|
37 | 54 | public function setMode(string $mode): CommandInterface |
38 | 55 | { |
39 | | - $this->mode = $mode; |
| 56 | + if (in_array($mode, array_keys(self::AVAILABLE_MODES))) { |
| 57 | + $this->mode = $mode; |
40 | 58 |
|
41 | | - return $this; |
| 59 | + return $this; |
| 60 | + } else { |
| 61 | + throw new DifferException(self::MODE_EXCEPTION_MESSAGE); |
| 62 | + } |
42 | 63 | } |
43 | 64 | } |
0 commit comments