|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Console\Commands; |
| 4 | + |
| 5 | +use App\Mail\FeedDeleted; |
| 6 | +use App\Models\Feed; |
| 7 | +use Illuminate\Console\Command; |
| 8 | +use Illuminate\Support\Facades\Mail; |
| 9 | + |
| 10 | +class DeleteFeedAndNotify extends Command |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The name and signature of the console command. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $signature = 'app:delete-feed-and-notify'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The console command description. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $description = 'Delete a feed and notify the owner with a custom reason'; |
| 25 | + |
| 26 | + /** |
| 27 | + * Execute the console command. |
| 28 | + */ |
| 29 | + public function handle(): void |
| 30 | + { |
| 31 | + // Step 1: Prompt for feed identifier (ID or URL) |
| 32 | + $identifier = $this->ask('Enter feed ID or URL'); |
| 33 | + |
| 34 | + // Step 2: Find feed by ID or URL |
| 35 | + $feed = $this->findFeed($identifier); |
| 36 | + |
| 37 | + if (! $feed) { |
| 38 | + $this->error('Feed not found.'); |
| 39 | + |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Step 3: Display feed information for confirmation |
| 44 | + $this->newLine(); |
| 45 | + $this->line('Feed found:'); |
| 46 | + $this->table( |
| 47 | + ['ID', 'URL', 'Email', 'Status', 'Confirmed'], |
| 48 | + [[$feed->id, $feed->url, $feed->email, $feed->status, $feed->confirmed ? 'Yes' : 'No']] |
| 49 | + ); |
| 50 | + |
| 51 | + // Step 4: Prompt for deletion reason |
| 52 | + $reason = $this->ask('Enter deletion reason (will be sent to feed owner)'); |
| 53 | + |
| 54 | + if (empty($reason)) { |
| 55 | + $this->error('Deletion reason is required.'); |
| 56 | + |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // Step 5: Final confirmation |
| 61 | + $this->newLine(); |
| 62 | + if (! $this->confirm('Are you sure you want to delete this feed and notify the owner?', false)) { |
| 63 | + $this->info('Deletion cancelled.'); |
| 64 | + |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + // Step 6: Send notification email |
| 69 | + $this->line('Sending notification email...'); |
| 70 | + try { |
| 71 | + Mail::send(new FeedDeleted($feed, $reason)); |
| 72 | + $this->info('Notification sent to '.$feed->email); |
| 73 | + } catch (\Exception $e) { |
| 74 | + $this->error('Failed to send notification: '.$e->getMessage()); |
| 75 | + if (! $this->confirm('Continue with deletion anyway?', false)) { |
| 76 | + $this->info('Deletion cancelled.'); |
| 77 | + |
| 78 | + return; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Step 7: Delete the feed (cascade will handle checks and connection_failures) |
| 83 | + $feedUrl = $feed->url; |
| 84 | + $feedEmail = $feed->email; |
| 85 | + |
| 86 | + $this->line('Deleting feed...'); |
| 87 | + $feed->delete(); |
| 88 | + |
| 89 | + $this->newLine(); |
| 90 | + $this->info('Feed deleted successfully.'); |
| 91 | + $this->line('URL: '.$feedUrl); |
| 92 | + $this->line('Owner: '.$feedEmail); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Find feed by ID (UUID) or URL |
| 97 | + */ |
| 98 | + private function findFeed(string $identifier): ?Feed |
| 99 | + { |
| 100 | + // Try to find by ID first (UUID format) |
| 101 | + if (strlen($identifier) === 36 && str_contains($identifier, '-')) { |
| 102 | + $feed = Feed::find($identifier); |
| 103 | + if ($feed) { |
| 104 | + return $feed; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Try to find by URL |
| 109 | + return Feed::where('url', $identifier)->first(); |
| 110 | + } |
| 111 | +} |
0 commit comments