You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
1.8 KiB
76 lines
1.8 KiB
<?php
|
|
|
|
namespace App\Jobs;
|
|
|
|
use App\Models\User;
|
|
use App\Pipes\StoreChunk\AgeFilterPipe;
|
|
use App\Pipes\StoreChunk\CreditCardFilter;
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Contracts\Queue\ShouldBeUnique;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use Illuminate\Pipeline\Pipeline;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class StoreChunk implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
public Collection $chunk;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(
|
|
array $chunk
|
|
)
|
|
{
|
|
$this->chunk = collect($chunk);
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$chunk = (new Pipeline(app()))
|
|
->through([
|
|
AgeFilterPipe::class,
|
|
CreditCardFilter::class,
|
|
])
|
|
->send($this->changeDateOfBirthToCarbon())
|
|
->thenReturn();
|
|
|
|
User::insert(
|
|
$this->prepare($chunk)->toArray()
|
|
);
|
|
}
|
|
|
|
protected function changeDateOfBirthToCarbon()
|
|
{
|
|
return $this->chunk->map(function ($item) {
|
|
if (!strpos($item['date_of_birth'], '/')) {
|
|
$item['date_of_birth'] = Carbon::create($item['date_of_birth']);
|
|
} else {
|
|
$item['date_of_birth'] = Carbon::createFromFormat('d/m/Y', $item['date_of_birth']);
|
|
}
|
|
|
|
return $item;
|
|
});
|
|
}
|
|
|
|
protected function prepare($chunk)
|
|
{
|
|
return $chunk->map(function ($item) {
|
|
$item['credit_card'] = json_encode($item['credit_card']);
|
|
return $item;
|
|
});
|
|
}
|
|
}
|
|
|