Skip to content

Commit

Permalink
Reemplazada la función everyFirstDatOfMonthAt($hour) por everyDay($da…
Browse files Browse the repository at this point in the history
…y, $hour) que si pones $day = 1, es lo mesmo que la anterior, pero te da opción a usar otros días.
  • Loading branch information
NeoRazorX committed Jun 17, 2023
1 parent 8cdd602 commit 6eb25f0
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Core/Model/CronJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ public function every(string $period): bool
return strtotime($this->date) <= strtotime('-' . $period);
}

public function everyDayAt(int $hour, bool $strict = false): bool
public function everyDay(int $day, int $hour, bool $strict = false): bool
{
return $this->everyDayAux('today', $hour, $strict);
$date = date('Y-m-' . $day);
return $this->everyDayAux($date, $hour, $strict);
}

public function everyFirstDayOfMonthAt(int $hour, bool $strict): bool
public function everyDayAt(int $hour, bool $strict = false): bool
{
return $this->everyDayAux('first day of this month', $hour, $strict);
return $this->everyDayAux('today', $hour, $strict);
}

public function everyFridayAt(int $hour, bool $strict): bool
Expand Down Expand Up @@ -171,15 +172,14 @@ private function everyDayAux(string $day, int $hour, bool $strict): bool
return true;
}

// devolvemos true si la última ejecución es anterior a hoy a la hora indicada
$last = strtotime($this->date);
$start = strtotime($day . ' +' . $hour . ' hours');

// si strict es true, solamente devolvemos true si es la hora exacta
$end = $strict ?
strtotime($day . ' +' . $hour . ' hours +59 minutes') :
strtotime($day . ' +23 hours +59 minutes');

// devolvemos true si la última ejecución es anterior a hoy a la hora indicada
$last = strtotime($this->date);
$start = strtotime($day . ' +' . $hour . ' hours');
$this->start = microtime(true);
return $last <= $start && $this->start >= $start && $this->start <= $end;
}
Expand Down
22 changes: 22 additions & 0 deletions Test/Core/Model/CronJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ public function testEveryFunction(): void
$this->assertTrue($job->delete());
}

public function testEveryDayFunction(): void
{
$currentDay = date('d') < 28 ? (int)date('d') : 10;

$job = new CronJob();
$job->jobname = 'TestName';
$job->pluginname = 'TestPlugin';

// como nunca se ha ejecutado, si decimos de ejecutar hoy, se ejecutará
$this->assertTrue($job->everyDay($currentDay, 1));
$this->assertTrue($job->save());

// como ya se ha ejecutado, si decimos de ejecutar hoy, no se ejecutará
$this->assertFalse($job->everyDayAt($currentDay, 1));

// como ya se ha ejecutado, si decimos de ejecutar mañana, no se ejecutará
$this->assertFalse($job->everyDayAt($currentDay + 1, 1));

// eliminamos
$this->assertTrue($job->delete());
}

public function testEveryDayAtFunction(): void
{
$job = new CronJob();
Expand Down

0 comments on commit 6eb25f0

Please sign in to comment.