Last active
February 28, 2021 02:54
-
-
Save freekmurze/bfb103e39897003af6ae5b7f1ce7bf18 to your computer and use it in GitHub Desktop.
Refactored Oh Dear! code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Check | |
{ | |
public function needsToRun(): bool | |
{ | |
if (!$this->belongsToTeamOnActiveSubscriptionOrOnGenericTrial()) { | |
return false; | |
} | |
if ($this->disabled()) { | |
return false; | |
} | |
if ($this->alreadyRunningOrScheduled()) { | |
return false; | |
} | |
if ($this->didNotRunBefore()) { | |
return true; | |
} | |
if ($this->checkType()->is(CheckType::UPTIME) && $this->latestRun()->failed()) { | |
return true; | |
} | |
if ($this->previousRunCrashed()) { | |
return true; | |
} | |
return $this->latestRun()->endedMoreThanMinutesAgo($this->checkType()->minutesBetweenRuns()); | |
} | |
protected function checkType(): CheckType | |
{ | |
return new CheckType($this->type); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models\Enums; | |
use MyCLabs\Enum\Enum; | |
class CheckType extends Enum | |
{ | |
const UPTIME = 'uptime'; | |
const BROKEN_LINKS = 'broken_links'; | |
const MIXED_CONTENT = 'mixed_content'; | |
const CERTIFICATE_HEALTH = 'certificate_health'; | |
const CERTIFICATE_TRANSPARENCY = 'certificate_transparency'; | |
public function minutesBetweenRuns(): int | |
{ | |
if ($this->getValue() === static::MIXED_CONTENT) { | |
return 60 * 12; | |
} | |
if ($this->getValue() === static::BROKEN_LINKS) { | |
return 60 * 12; | |
} | |
if ($this->getValue() === static::CERTIFICATE_HEALTH) { | |
return 5; | |
} | |
if ($this->getValue() === static::UPTIME) { | |
return 3; | |
} | |
throw new Exception("Minutes between runs not specified for type `{$this->getValue()}`"); | |
} | |
public function is(string $type): bool | |
{ | |
return $type === $this->getValue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment