Guest User

Untitled

a guest
May 27th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.68 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Imports;
  4.  
  5. use App\Models\Buffer;
  6. use Maatwebsite\Excel\Concerns\ToModel;
  7. use App\Models\Source;
  8. use Maatwebsite\Excel\Concerns\WithChunkReading;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Maatwebsite\Excel\Concerns\SkipsFailures;
  11. use Maatwebsite\Excel\Concerns\SkipsOnFailure;
  12. use Maatwebsite\Excel\Concerns\WithValidation;
  13. use Maatwebsite\Excel\Concerns\WithStartRow;
  14.  
  15. class BufferImport implements ToModel, SkipsOnFailure,  WithValidation, WithStartRow, WithChunkReading, ShouldQueue
  16. {
  17.     use SkipsFailures;
  18.  
  19.     protected $sourceId;
  20.  
  21.     protected $startRow;
  22.  
  23.     protected $dataColumn;
  24.  
  25.     protected $priceColumn;
  26.  
  27.     public function __construct(Source $source)
  28.     {
  29.         $this->sourceId = $source->id;
  30.         $this->startRow = $source->start_row;
  31.         $this->dataColumn = $source->data_column - 1;
  32.         $this->priceColumn = $source->price_column - 1;
  33.     }
  34.  
  35.     /**
  36.      * @param array $row
  37.      *
  38.      * @return \Illuminate\Database\Eloquent\Model|null
  39.      */
  40.     public function model(array $row)
  41.     {
  42.         return new Buffer([
  43.             'source_id' => $this->sourceId,
  44.             'source_string' => $row[$this->dataColumn],
  45.             'distinct_string' => $row[$this->dataColumn],
  46.             'cost' => (float) $row[$this->priceColumn],
  47.         ]);
  48.     }
  49.  
  50.  
  51.  
  52.     public function startRow(): int
  53.     {
  54.         return $this->startRow;
  55.     }
  56.    
  57.  
  58.     public function rules(): array
  59.     {
  60.         return [
  61.             $this->dataColumn => 'required',
  62.             $this->priceColumn => 'required',
  63.         ];
  64.     }
  65.  
  66.    
  67.     public function chunkSize(): int
  68.     {
  69.         return 500;
  70.     }
  71.    
  72. }
Add Comment
Please, Sign In to add comment