Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
{
private ObjectManager $em;

public function __construct(ManagerRegistry $managerRegistry, private readonly DateTimeFormatter $dateTimeFormatter, string $managerName)
public function __construct(ManagerRegistry $managerRegistry,
private readonly DateTimeFormatter|null $dateTimeFormatter,
string $managerName)
{
$this->em = $managerRegistry->getManager($managerName);
parent::__construct();
Expand All @@ -38,6 +40,17 @@
->setHelp('This class is for listing all active commands.');
}

// https://github.com/Dukecity/CommandSchedulerBundle/issues/99
private function formatDiff(\DateTime $dateTime): string
{
if($this->dateTimeFormatter)
{
return $this->dateTimeFormatter->formatDiff($dateTime);
}

return $dateTime->format('Y-m-d H:i');

Check warning on line 51 in Command/ListCommand.php

View check run for this annotation

Codecov / codecov/patch

Command/ListCommand.php#L51

Added line #L51 was not covered by tests
}

/**
* @throws \Exception
*/
Expand All @@ -64,11 +77,11 @@
};

if($nextRunDate = $command->getNextRunDate())
{$nextRunDateText = $this->dateTimeFormatter->formatDiff($nextRunDate);}
{$nextRunDateText = $this->formatDiff($nextRunDate);}
else {$nextRunDateText = "";}

if($lastRunDate = $command->getLastExecution())
{$lastRunDateText = $this->dateTimeFormatter->formatDiff($lastRunDate);}
{$lastRunDateText = $this->formatDiff($lastRunDate);}
else {$lastRunDateText = "";}

$table->addRow([
Expand Down
17 changes: 14 additions & 3 deletions Command/MonitorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public function __construct(
private EventDispatcherInterface $eventDispatcher,
ManagerRegistry $managerRegistry,
private readonly DateTimeFormatter $dateTimeFormatter,
private readonly DateTimeFormatter|null $dateTimeFormatter,
string $managerName,
private int | bool $lockTimeout,
private array $receiver,
Expand Down Expand Up @@ -103,6 +103,17 @@
return Command::SUCCESS;
}

// https://github.com/Dukecity/CommandSchedulerBundle/issues/99
private function formatDiff(\DateTime $dateTime): string
{
if($this->dateTimeFormatter)
{
return $this->dateTimeFormatter->formatDiff($dateTime);
}

return '';

Check warning on line 114 in Command/MonitorCommand.php

View check run for this annotation

Codecov / codecov/patch

Command/MonitorCommand.php#L114

Added line #L114 was not covered by tests
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not returning a date string in this case ?

$datetime->format('Y-m-d')

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be duplicate the info in this format twice or not?

}

/**
* Print a table of locked Commands to console.
*
Expand Down Expand Up @@ -133,7 +144,7 @@
if($lastRunDate)
{
$lastRunDateText = $lastRunDate->format('Y-m-d H:i').' ('
.$this->dateTimeFormatter->formatDiff($command->getLastExecution()).')';
.$this->formatDiff($command->getLastExecution()).')';
}
else {
$lastRunDateText = '';
Expand All @@ -143,7 +154,7 @@
if($nextRunDate)
{
$nextRunDateText = $nextRunDate->format('Y-m-d H:i').' ('
.$this->dateTimeFormatter->formatDiff($command->getLastExecution()).')';
.$this->formatDiff($command->getLastExecution()).')';
}
else {
$nextRunDateText = '';
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
[
service('event_dispatcher'),
service('doctrine'),
service('time.datetime_formatter'),
service('time.datetime_formatter')->nullOnInvalid(),
'%dukecity_command_scheduler.doctrine_manager%',
'%dukecity_command_scheduler.lock_timeout%',
'%dukecity_command_scheduler.monitor_mail%',
Expand All @@ -115,7 +115,7 @@
->args(
[
service('doctrine'),
service('time.datetime_formatter'),
service('time.datetime_formatter')->nullOnInvalid(),
'%dukecity_command_scheduler.doctrine_manager%',
]
)
Expand Down