1111use Symfony \Component \Console \Input \InputOption ;
1212use Symfony \Component \Console \Output \OutputInterface ;
1313
14- use function date ;
14+ use function dirname ;
1515use function file ;
16+ use function file_exists ;
1617use function is_numeric ;
1718use function json_decode ;
18- use function preg_match ;
1919use function strtolower ;
2020use function strtotime ;
2121
@@ -46,32 +46,42 @@ protected function configure(): void
4646
4747 protected function execute (InputInterface $ input , OutputInterface $ output ): int
4848 {
49- $ start = $ input ->getOption ('start ' );
50- $ end = $ input ->getOption ('end ' );
51- $ limit = $ input ->getOption ('limit ' );
49+ try {
50+ $ startOption = $ input ->getOption ('start ' );
51+ $ endOption = $ input ->getOption ('end ' );
52+ $ limit = $ input ->getOption ('limit ' );
53+
54+ $ startDate = $ startOption ? new \DateTimeImmutable ($ startOption ) : null ;
55+ $ endDate = $ endOption ? new \DateTimeImmutable ($ endOption ) : null ;
56+ } catch (\Exception $ e ) {
57+ $ output ->writeln ('<error>Invalid date format provided.</error> ' );
58+ return Command::FAILURE ;
59+ }
5260
53- if ($ start && ! preg_match ( ' /\d{2}:\d{2}:\d{2}/ ' , $ start ) ) {
54- $ start .= ' 00:00:00 ' ;
61+ if ($ startDate && $ startDate -> format ( ' H:i:s ' ) === ' 00:00:00 ' ) {
62+ $ startDate = $ startDate -> setTime ( 0 , 0 , 0 ) ;
5563 }
5664
57- if ($ end && ! preg_match ( ' /\d{2}:\d{2}:\d{2}/ ' , $ end ) ) {
58- $ end .= ' 23:59:59 ' ;
65+ if ($ endDate && $ endDate -> format ( ' H:i:s ' ) === ' 00:00:00 ' ) {
66+ $ endDate = $ endDate -> setTime ( 23 , 59 , 59 ) ;
5967 }
6068
6169 if ($ limit && is_numeric ($ limit )) {
62- if ($ start && !$ end ) {
63- $ end = date ( ' Y-m-d H:i:s ' , strtotime ("+ {$ limit } days " , strtotime ( $ start )) );
64- } elseif (!$ start && $ end ) {
65- $ start = date ( ' Y-m-d H:i:s ' , strtotime ("- {$ limit } days " , strtotime ( $ end )) );
70+ if ($ startDate && ! $ endDate ) {
71+ $ endDate = $ startDate -> modify ("+ {$ limit } days " );
72+ } elseif (! $ startDate && $ endDate ) {
73+ $ startDate = $ endDate -> modify ("- {$ limit } days " );
6674 }
6775 }
6876
69- if (!$ end ) {
70- $ end = date ( ' Y-m-d H:i:s ' );
77+ if (! $ endDate ) {
78+ $ endDate = new \ DateTime ( );
7179 }
7280
73- $ startTimestamp = $ start ? strtotime ($ start ) : null ;
74- $ endTimestamp = $ end ? strtotime ($ end ) : null ;
81+ if ($ startDate > $ endDate ) {
82+ $ output ->writeln ('<error>The start date cannot be after the end date.</error> ' );
83+ return Command::FAILURE ;
84+ }
7585
7686 $ logPath = dirname (__DIR__ , 3 ) . '/log/queue-log.log ' ;
7787
@@ -80,7 +90,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8090 return Command::FAILURE ;
8191 }
8292
83- $ lines = file ($ logPath , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
93+ $ lines = file ($ logPath , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
94+
95+ $ startTimestamp = $ startDate ?->getTimestamp();
96+ $ endTimestamp = $ endDate ->getTimestamp ();
97+
98+ $ found = false ;
8499
85100 foreach ($ lines as $ line ) {
86101 $ entry = json_decode ($ line , true );
@@ -102,6 +117,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
102117 }
103118
104119 $ output ->writeln ($ line );
120+ $ found = true ;
121+ }
122+
123+ if (! $ found ) {
124+ $ output ->writeln ('<comment>No matching log entries found.</comment> ' );
105125 }
106126
107127 return Command::SUCCESS ;
0 commit comments