-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #65 from beromir/feat/delete-duplicate-links
[FEAT] Add command to delete duplicate links of a user
- Loading branch information
Showing
6 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\User; | ||
use App\Services\LinkService; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
class DeleteDuplicateLinksCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'link:delete-duplicates'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Delete the duplicate links of the user'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle(LinkService $linkService): int | ||
{ | ||
$userId = $this->ask('User ID'); | ||
|
||
try { | ||
Validator::make(['id' => $userId], [ | ||
'id' => ['required', 'numeric'], | ||
])->validate(); | ||
} catch (ValidationException $exception) { | ||
$this->error($exception->getMessage()); | ||
return Command::FAILURE; | ||
} | ||
|
||
$user = User::find($userId); | ||
|
||
if (empty($user)) { | ||
$this->error('The user does not exist.'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
$duplicates = $linkService->getDuplicates($user); | ||
|
||
if (count($duplicates) === 0) { | ||
$this->info('No duplicates found.'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
if ($this->confirm(count($duplicates) . ' links found. Do you want to delete them?')) { | ||
$linkService->deleteDuplicates($duplicates); | ||
|
||
$this->info('Duplicate links deleted.'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
$this->error('Please confirm the deletion.'); | ||
|
||
return Command::FAILURE; | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\Link; | ||
use App\Models\User; | ||
|
||
class LinkService | ||
{ | ||
/** | ||
* Get duplicate links of the user. | ||
*/ | ||
public function getDuplicates(User $user): array | ||
{ | ||
$groupedLinks = Link::where('user_id', $user->id)->get(['id', 'link'])->groupBy('link')->toArray();; | ||
|
||
return array_filter($groupedLinks, fn($group) => count($group) > 1); | ||
} | ||
|
||
/** | ||
* Remove duplicate links. | ||
*/ | ||
public function deleteDuplicates(array $links): void | ||
{ | ||
// TODO: Find a cleaner solution. | ||
foreach ($links as $duplicates) { | ||
|
||
if (!is_array($duplicates)) { | ||
continue; | ||
} | ||
|
||
array_pop($duplicates); | ||
|
||
if (empty($duplicates)) { | ||
continue; | ||
} | ||
|
||
foreach ($duplicates as $duplicateLink) { | ||
$link = Link::find($duplicateLink['id']); | ||
|
||
$this->deleteLink($link); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Delete the link if not null. | ||
*/ | ||
public function deleteLink(?Link $link): void | ||
{ | ||
if (is_null($link)) { | ||
return; | ||
} | ||
|
||
$link->delete(); | ||
} | ||
} |