| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Spatie\MailableTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Mail; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class SendTestMail extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $signature = 'mail:send-test {mailableClass} {recipient} {--values=}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $description = 'Send a test email'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     public function handle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $this->guardAgainstInvalidArguments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $mailable = app(MailableFactory::class)->getInstance( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             $this->argument('mailableClass'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             $this->argument('recipient'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             $this->getValues() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         Mail::send($mailable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->comment("Mailable `{$this->argument('mailableClass')}` sent to {$this->argument('recipient')}!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     protected function guardAgainstInvalidArguments() | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $validator = Validator::make( | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |             ['email' => $this->argument('recipient')], | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             ['email' => 'email'] | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         if (! $validator->passes()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             throw new InvalidArgumentException("`{$this->argument('recipient')}` is not a valid e-mail address"); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         if (! $this->validateMailable($this->argument('mailableClass'))) { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             throw new InvalidArgumentException("Mailable `{$this->argument('mailableClass')}` does not exist."); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected function getValues(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (! $this->option('values')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $values = explode(',', $this->option('values')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return collect($values) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ->mapWithKeys(function (string $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 $values = explode(':', $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 if (count($values) != 2) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                     throw new InvalidArgumentException("The given value for the option 'values' `{$this->option('values')}` is not valid."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 return [$values[0] => $values[1]]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     protected function validateMailable($mailableClass) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 70 |  | View Code Duplication |         if (! class_exists($mailableClass)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $mailableClass = sprintf('%s\\%s', config('mailable-test.base_namespace'), $mailableClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             if (! class_exists($mailableClass)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.