|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BinBytes\ModelMediaBackup\Commands; |
| 4 | + |
| 5 | +use BinBytes\ModelMediaBackup\Interfaces\ShouldBackupFiles; |
| 6 | +use BinBytes\ModelMediaBackup\Mail\MediaBackupTaken; |
| 7 | +use Illuminate\Console\Command; |
| 8 | + |
| 9 | +class ModelMediaBackup extends Command |
| 10 | +{ |
| 11 | + /** |
| 12 | + * The name and signature of the console command. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + protected $signature = 'model:media:backup'; |
| 17 | + |
| 18 | + /** |
| 19 | + * The console command description. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + protected $description = 'Take backup of resource by model'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Create a new command instance. |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function __construct() |
| 31 | + { |
| 32 | + parent::__construct(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Execute the console command. |
| 37 | + * |
| 38 | + * @return mixed |
| 39 | + */ |
| 40 | + public function handle() |
| 41 | + { |
| 42 | + $backupDisk = config('modelmediabackup.BACKUP_DISK'); |
| 43 | + |
| 44 | + if(!$backupDisk) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + $storage = \Storage::disk($backupDisk); |
| 49 | + $chunkSize = config('modelmediabackup.ChunkSize'); |
| 50 | + |
| 51 | + foreach ($models = config('modelmediabackup.Models') as $model) { |
| 52 | + if(in_array(ShouldBackupFiles::class, class_implements($model)) === false) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + $recordsBackup = []; |
| 57 | + |
| 58 | + $model::backupRecords() |
| 59 | + ->chunk($chunkSize, function ($records) use($storage, &$recordsBackup) { |
| 60 | + $records->each(function ($record) use($storage, &$recordsBackup) { |
| 61 | + if ($backupFiles = $record->backupFiles()) { |
| 62 | + foreach (is_array($backupFiles) ? $backupFiles : [$backupFiles] as $backupFile) { |
| 63 | + if($this->takeBackup($storage, $backupFile)) { |
| 64 | + $recordsBackup[] = $record->getKey(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + if($recordsBackup) { |
| 72 | + $this->sendNotifications($recordsBackup); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Copy file to destination/backup disk |
| 79 | + * |
| 80 | + * @param $storage |
| 81 | + * @param $file |
| 82 | + * |
| 83 | + * @return bool |
| 84 | + */ |
| 85 | + protected function takeBackup($storage, $file) |
| 86 | + { |
| 87 | + if(\Storage::exists($file) && $storage->exists($file) == false) { |
| 88 | + $stream = \Storage::getDriver()->readStream($file); |
| 89 | + |
| 90 | + return $storage->put($file, $stream); |
| 91 | + } |
| 92 | + |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Send notifications after backup |
| 98 | + * |
| 99 | + * @param $recordsBackup |
| 100 | + */ |
| 101 | + protected function sendNotifications(array $recordsBackup) |
| 102 | + { |
| 103 | + if(!config('modelmediabackup.EnableNotification')) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if($mailTo = config('modelmediabackup.Notification.MailTo')) { |
| 108 | + $this->sendNotificationMail($mailTo, $recordsBackup); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Send notification mail |
| 114 | + */ |
| 115 | + protected function sendNotificationMail($mailTo, array $recordsBackup) |
| 116 | + { |
| 117 | + \Mail::to($mailTo) |
| 118 | + ->send(new MediaBackupTaken( |
| 119 | + $recordsBackup |
| 120 | + )); |
| 121 | + } |
| 122 | +} |
0 commit comments