Skip to content

Commit

Permalink
Prevent error when user timezone is null
Browse files Browse the repository at this point in the history
Use server timezone when the use haven't a timezone

Signed-off-by: Vitor Mattos <vitor@php.rio>
  • Loading branch information
vitormattos committed Dec 5, 2023
1 parent bee83f9 commit 22ab6cc
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Repository/LockdownRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public function updateLockWeek(?Approval $approval, ApprovalRepository $approval
$endDate = $this->getOldestNotSubmittedDate($allWeeks, $user);
$endDate = $this->getOldestSubmittedApprovalFromTeam($user, $approvalRepository, $endDate);

$graceDate = $this->getGraceDate(clone $endDate, new DateTimeZone($user->getPreferenceValue('timezone')));
$userTimezone = $user->getPreferenceValue('timezone');
if (empty($userTimezone)) {
$userTimezone = $_SERVER['TIMEZONE'];
}
$graceDate = $this->getGraceDate(clone $endDate, new DateTimeZone($userTimezone));
$endDate->modify('midnight')->modify('+1 day')->modify('-1 second');
$this->updateLockPreference($user, $endDate, $graceDate);
}
Expand All @@ -66,7 +70,11 @@ private function updatePreference(User $user, string $preferenceName, string $va
*/
private function getOldestNotSubmittedDate(array $allWeeks, User $user): DateTime
{
$timezone = new DateTimeZone($user->getPreferenceValue('timezone'));
$userTimezone = $user->getPreferenceValue('timezone');
if (empty($userTimezone)) {
$userTimezone = $_SERVER['TIMEZONE'];
}
$timezone = new DateTimeZone($userTimezone);

foreach ($allWeeks as $week) {
if ($week['status'] === 'not_submitted') {
Expand All @@ -87,9 +95,13 @@ private function getOldestNotSubmittedDate(array $allWeeks, User $user): DateTim

private function updateLockPreference(User $user, $endDate, $graceDate): void
{
$userTimezone = $user->getPreferenceValue('timezone');
if (empty($userTimezone)) {
$userTimezone = $_SERVER['TIMEZONE'];
}
$this->updatePreference($user, self::LOCKDOWN_PERIOD_START, '0000-01-01 00:00:01');
$this->updatePreference($user, self::LOCKDOWN_PERIOD_END, $endDate->format('Y-m-d H:i:s'));
$this->updatePreference($user, self::LOCKDOWN_PERIOD_TIMEZONE, $user->getPreferenceValue('timezone'));
$this->updatePreference($user, self::LOCKDOWN_PERIOD_TIMEZONE, $userTimezone);
$this->updatePreference($user, self::LOCKDOWN_GRACE_PERIOD, $graceDate->format('Y-m-d H:i:s'));
}

Expand Down Expand Up @@ -123,7 +135,11 @@ private function updateTeamleadersLock(User $user, ApprovalRepository $approvalR
$endDate = $this->getOldestNotSubmittedDate($weeks, $teamLeader);
$endDate = $this->getOldestSubmittedApprovalFromTeam($teamLeader, $approvalRepository, $endDate);

$graceDate = $this->getGraceDate(clone $endDate, new DateTimeZone($user->getPreferenceValue('timezone')));
$userTimezone = $user->getPreferenceValue('timezone');
if (empty($userTimezone)) {
$userTimezone = $_SERVER['TIMEZONE'];
}
$graceDate = $this->getGraceDate(clone $endDate, new DateTimeZone($userTimezone));
$endDate->modify('midnight')->modify('+1 day')->modify('-1 second');
$this->updateLockPreference($teamLeader, $endDate, $graceDate);
}
Expand Down

0 comments on commit 22ab6cc

Please sign in to comment.