81 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| declare(strict_types=1);
 | |
| 
 | |
| namespace App\Console\Commands;
 | |
| 
 | |
| use Illuminate\Console\Command;
 | |
| use App\Models\CV;
 | |
| 
 | |
| class UpdateCV extends Command
 | |
| {
 | |
|     protected $signature = 'cv:update
 | |
|                             {id : ID CV element}
 | |
|                             {--company= : Company name}
 | |
|                             {--phone= : Phone number}
 | |
|                             {--begin-location : Add begin}
 | |
|                             {--add-location=* : Add locations}
 | |
|                             {--remove-location=* : Remove lcoations}
 | |
|                             {--mission= : Set new text value}
 | |
|                             {--rodo= : Set new text value}
 | |
|                             {--position= : Set position value}';
 | |
| 
 | |
|     protected $description = 'Update CV element';
 | |
| 
 | |
|     public function handle(): int
 | |
|     {
 | |
|         if (! ($id = $this->argument('id')) || $id <= 0) {
 | |
|             $this->error('Incorrect id');
 | |
|             return Command::FAILURE;
 | |
|         }
 | |
| 
 | |
|         $cv = CV::find($id);
 | |
|         
 | |
|         if ($company = $this->option('company')) {
 | |
|             $cv->recipient = $company;
 | |
|         }
 | |
|         if ($phone = $this->option('phone')) {
 | |
|             $cv->phone_number = $phone;
 | |
|         }
 | |
|         if (count($addLocations = $this->option('remove-location')) > 0) {
 | |
|             $locations = $cv->locations;
 | |
|             $locations = array_diff($locations, $addLocations);
 | |
|             $cv->locations = $locations;
 | |
|         }
 | |
|         if (count($addLocations = $this->option('add-location')) > 0) {
 | |
|             $locations = $cv->locations;
 | |
| 
 | |
|             $clearLocations = [];
 | |
|             foreach ($addLocations as $location) {
 | |
|                 if (in_array($location, $locations)) {
 | |
|                     $this->warn('"'. $location .'" exists! This value was not added.');
 | |
|                     $clearLocations[] = $location;
 | |
|                 }
 | |
|             }
 | |
|             $addLocations = array_diff($addLocations, $clearLocations);
 | |
| 
 | |
|             if ($this->option('begin-location'))
 | |
|                 $locations = array_merge($addLocations, $locations);
 | |
|             else
 | |
|                 $locations = array_merge($locations, $addLocations);
 | |
|             $cv->locations = $locations;
 | |
|         }
 | |
| 
 | |
|         if ($mission = $this->option('mission')) {
 | |
|             $cv->mission = $mission === 'null' ? null : $mission;
 | |
|         }
 | |
|         if ($rodo = $this->option('rodo')) {
 | |
|             $cv->rodo = $rodo === 'null' ? null : $rodo;
 | |
|         }
 | |
|         if ($position = $this->option('position')) {
 | |
|             $cv->position = $position === 'null' ? null : $position;
 | |
|         }
 | |
| 
 | |
|         $cv->save();
 | |
| 
 | |
|         $this->info('Updated!');
 | |
| 
 | |
|         return Command::SUCCESS;
 | |
|     }
 | |
| }
 |