@@ -8,30 +8,36 @@ class ConsoleApp
88{
99 private CommandInterface $ nextCommand ;
1010 private CommandFactoryInterface $ commandFactory ;
11+ private array $ flowSteps ;
12+ private CommandInterface $ initCommand ;
1113
1214 public function __construct (
1315 CommandFactoryInterface $ commandFactory
1416 ) {
1517 $ this ->commandFactory = $ commandFactory ;
18+
19+ $ parseCommand = $ this ->commandFactory ->createCommand ("parse " );
20+ $ this ->initCommand = $ parseCommand ->execute ();
21+ $ this ->flowSteps = [
22+ "difference " ,
23+ strtolower ($ parseCommand ->getFormat ()),
24+ "show "
25+ ];
1626 }
1727
1828 public function run (): void
1929 {
20- $ parseCommand = $ this ->commandFactory -> createCommand ( " parse " ) ;
21- $ this -> nextCommand = $ parseCommand
22- -> execute ();
23-
24- $ differenceCommand = $ this -> commandFactory ->createCommand (" difference " );
25- $ this -> nextCommand = $ differenceCommand
26- -> execute ( $ this -> nextCommand );
27-
28- $ formatCommand = $ this -> commandFactory -> createCommand (
29- strtolower ( $ parseCommand -> getFormat ())
30+ $ commandFactory = $ this ->commandFactory ;
31+ array_reduce (
32+ $ this -> flowSteps ,
33+ function ( $ nextCommand , $ item ) use ( $ commandFactory ) {
34+ $ currentCommand = $ commandFactory ->createCommand ($ item );
35+ $ nextCommand = $ currentCommand -> execute ( $ nextCommand );
36+
37+ return $ nextCommand ;
38+ },
39+ $ this -> initCommand
3040 );
31- $ this ->nextCommand = $ formatCommand ->execute ($ this ->nextCommand );
32-
33- $ displayCommand = $ this ->commandFactory ->createCommand ("show " );
34-
35- $ displayCommand ->execute ($ this ->nextCommand );
41+
3642 }
3743}
0 commit comments