Remove duplicated code

This commit is contained in:
peaklabs-dev
2024-09-17 14:43:02 +02:00
parent 4ac2758d70
commit 2bc74c75e1
2 changed files with 10 additions and 21 deletions

View File

@@ -19,8 +19,9 @@ class CleanupSshKeysJob implements ShouldQueue
$oneWeekAgo = Carbon::now()->subWeek(); $oneWeekAgo = Carbon::now()->subWeek();
PrivateKey::where('created_at', '<', $oneWeekAgo) PrivateKey::where('created_at', '<', $oneWeekAgo)
->whereDoesntHave('gitSources') ->get()
->whereDoesntHave('servers') ->each(function ($privateKey) {
->delete(); $privateKey->safeDelete();
});
} }
} }

View File

@@ -144,10 +144,6 @@ class PrivateKey extends BaseModel
public static function deleteFromStorage(self $privateKey) public static function deleteFromStorage(self $privateKey)
{ {
if ($privateKey->isInUse()) {
throw new \Exception('Cannot delete a private key that is in use.');
}
$filename = "ssh@{$privateKey->uuid}"; $filename = "ssh@{$privateKey->uuid}";
Storage::disk('ssh-keys')->delete($filename); Storage::disk('ssh-keys')->delete($filename);
} }
@@ -184,14 +180,6 @@ class PrivateKey extends BaseModel
return $this->hasMany(GitlabApp::class); return $this->hasMany(GitlabApp::class);
} }
public function isEmpty()
{
return $this->servers()->count() === 0
&& $this->applications()->count() === 0
&& $this->githubApps()->count() === 0
&& $this->gitlabApps()->count() === 0;
}
public function isInUse() public function isInUse()
{ {
return $this->servers()->exists() return $this->servers()->exists()
@@ -202,11 +190,11 @@ class PrivateKey extends BaseModel
public function safeDelete() public function safeDelete()
{ {
if ($this->isInUse()) { if (!$this->isInUse()) {
throw new \Exception('This private key is in use and cannot be deleted.');
}
$this->delete(); $this->delete();
return true;
}
return false;
} }
public static function generateFingerprint($privateKey) public static function generateFingerprint($privateKey)