1616namespace Drift \Bus \Async ;
1717
1818use Drift \Bus \Bus \CommandBus ;
19+ use Drift \Bus \Console \ConsumerLineMessage ;
1920use Drift \Bus \Exception \InvalidCommandException ;
2021use Drift \Bus \Exception \MissingHandlerException ;
22+ use Drift \Console \OutputPrinter ;
23+ use Drift \Console \TimeFormatter ;
2124use React \Promise \FulfilledPromise ;
2225use React \Promise \PromiseInterface ;
23- use Symfony \Component \Console \Output \OutputInterface ;
2426
2527/**
2628 * Interface AsyncAdapter.
@@ -64,43 +66,51 @@ abstract public function enqueue($command): PromiseInterface;
6466 /**
6567 * Consume.
6668 *
67- * @param CommandBus $bus
68- * @param int $limit
69- * @param OutputInterface $output
69+ * @param CommandBus $bus
70+ * @param int $limit
71+ * @param OutputPrinter $outputPrinter
7072 *
7173 * @throws InvalidCommandException
7274 */
7375 abstract public function consume (
7476 CommandBus $ bus ,
7577 int $ limit ,
76- OutputInterface $ output
78+ OutputPrinter $ outputPrinter
7779 );
7880
7981 /**
8082 * Execute command.
8183 *
82- * @param CommandBus $bus
83- * @param object $command
84- * @param OutputInterface $output
85- * @param callable $ok
86- * @param callable $ko
87- * @param callable $finish
84+ * @param CommandBus $bus
85+ * @param object $command
86+ * @param OutputPrinter $outputPrinter
87+ * @param callable $ok
88+ * @param callable $ko
89+ * @param callable $finish
8890 *
8991 * @return PromiseInterface
9092 */
9193 protected function executeCommand (
9294 CommandBus $ bus ,
9395 $ command ,
94- OutputInterface $ output ,
96+ OutputPrinter $ outputPrinter ,
9597
9698 callable $ ok ,
9799 callable $ ko ,
98100 callable $ finish
99101 ): PromiseInterface {
102+ $ from = microtime (true );
103+
100104 return $ bus
101105 ->execute ($ command )
102- ->then (function () use ($ output , $ command , $ ok , $ finish ) {
103- $ this ->printCommandMessage ($ command , $ output , 'consumed ' );
106+ ->then (function () use ($ from , $ outputPrinter , $ command , $ ok , $ finish ) {
107+ $ to = microtime (true );
108+
109+ (new ConsumerLineMessage (
110+ $ command ,
111+ TimeFormatter::formatTime ($ to - $ from ),
112+ ConsumerLineMessage::CONSUMED
113+ ))->print ($ outputPrinter );
104114
105115 return (new FulfilledPromise ())
106116 ->then (function () use ($ ok ) {
@@ -119,11 +129,17 @@ protected function executeCommand(
119129
120130 return false ;
121131 });
122- }, function (\Exception $ exception ) use ($ output , $ command , $ ok , $ ko ) {
132+ }, function (\Exception $ exception ) use ($ from , $ outputPrinter , $ command , $ ok , $ ko ) {
133+ $ to = microtime (true );
123134 $ ignorable = $ exception instanceof MissingHandlerException;
124- $ ignorable
125- ? $ this ->printCommandMessage ($ command , $ output , 'ignored ' )
126- : $ this ->printCommandMessage ($ command , $ output , 'failed ' );
135+
136+ (new ConsumerLineMessage (
137+ $ command ,
138+ TimeFormatter::formatTime ($ to - $ from ),
139+ $ ignorable
140+ ? ConsumerLineMessage::IGNORED
141+ : ConsumerLineMessage::REJECTED
142+ ))->print ($ outputPrinter );
127143
128144 return (
129145 $ ignorable
@@ -171,22 +187,4 @@ public function canConsumeAnotherOne(): bool
171187
172188 return true ;
173189 }
174-
175- /**
176- * Print command consumed.
177- *
178- * @param object $command
179- * @param OutputInterface $output
180- * @param string $status
181- */
182- private function printCommandMessage (
183- $ command ,
184- OutputInterface $ output ,
185- string $ status
186- ) {
187- $ commandNamespace = get_class ($ command );
188- $ commandParts = explode ('\\' , $ commandNamespace );
189- $ commandClass = end ($ commandParts );
190- $ output ->writeln (sprintf ('Command <%s> %s ' , $ commandClass , $ status ));
191- }
192190}
0 commit comments