From 2bc74c75e12bd103a15d3e4f2b854468b35d5a96 Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:43:02 +0200 Subject: [PATCH] Remove duplicated code --- app/Jobs/CleanupSshKeysJob.php | 9 +++++---- app/Models/PrivateKey.php | 22 +++++----------------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/app/Jobs/CleanupSshKeysJob.php b/app/Jobs/CleanupSshKeysJob.php index 05e3a8837..485bf99eb 100644 --- a/app/Jobs/CleanupSshKeysJob.php +++ b/app/Jobs/CleanupSshKeysJob.php @@ -19,8 +19,9 @@ class CleanupSshKeysJob implements ShouldQueue $oneWeekAgo = Carbon::now()->subWeek(); PrivateKey::where('created_at', '<', $oneWeekAgo) - ->whereDoesntHave('gitSources') - ->whereDoesntHave('servers') - ->delete(); + ->get() + ->each(function ($privateKey) { + $privateKey->safeDelete(); + }); } -} \ No newline at end of file +} diff --git a/app/Models/PrivateKey.php b/app/Models/PrivateKey.php index fef464823..05310e413 100644 --- a/app/Models/PrivateKey.php +++ b/app/Models/PrivateKey.php @@ -143,11 +143,7 @@ class PrivateKey extends BaseModel } 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}"; Storage::disk('ssh-keys')->delete($filename); } @@ -184,14 +180,6 @@ class PrivateKey extends BaseModel 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() { return $this->servers()->exists() @@ -202,11 +190,11 @@ class PrivateKey extends BaseModel public function safeDelete() { - if ($this->isInUse()) { - throw new \Exception('This private key is in use and cannot be deleted.'); + if (!$this->isInUse()) { + $this->delete(); + return true; } - - $this->delete(); + return false; } public static function generateFingerprint($privateKey)