Issues (465)

app/Jobs/SyncWithGeneanum.php (1 issue)

Severity
1
<?php
2
3
namespace App\Jobs;
4
5
use App\Models\Geneanum;
6
use Illuminate\Bus\Queueable;
7
use Illuminate\Contracts\Queue\ShouldBeUnique;
8
use Illuminate\Contracts\Queue\ShouldQueue;
9
use Illuminate\Foundation\Bus\Dispatchable;
10
use Illuminate\Queue\InteractsWithQueue;
11
use Illuminate\Queue\SerializesModels;
12
use Illuminate\Support\Facades\Http;
13
use Spatie\Multitenancy\Jobs\NotTenantAware;
14
15
class SyncWithGeneanum implements ShouldQueue, NotTenantAware
16
{
17
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by App\Jobs\SyncWithGeneanum: $id, $relations, $class, $keyBy
Loading history...
18
19
    private const URL = 'http://static.geneanum.com/libs/grid/malte_bapteme.php?annee_limite=75&_search=false&nd=%s&rows=100&page=%s&sidx=Nom_Baptise&sord=asc';
20
    private const MAX_RETRY = 3;
21
22
    private $current_page;
23
    private $retry;
24
25
    /**
26
     * Create a new job instance.
27
     *
28
     * @return void
29
     */
30
    public function __construct(int $page = 1, int $retry = 0)
31
    {
32
        $this->current_page = $page;
33
        $this->retry        = $retry;
34
    }
35
36
    /**
37
     * Execute the job.
38
     *
39
     * @return void
40
     */
41
    public function handle()
42
    {
43
        $url = sprintf(self::URL, time() - mt_rand(1, 30), $this->current_page);
44
45
        $response = Http::get($url);
46
47
        if ($response->failed()) {
48
            if (++$this->retry > self::MAX_RETRY) {
49
                return;
50
            }
51
52
            dispatch(new SyncWithGeneanum($this->current_page, $this->retry));
53
        }
54
55
        $result = $response->json();
56
57
        $has_more_result = $result['total'] > $result['page'];
58
        if ($has_more_result) {
59
            dispatch(new SyncWithGeneanum(++$this->current_page))
60
                ->delay(now()->addSeconds(mt_rand(10, 60)));
61
        }
62
63
        $rows = $result['rows'];
64
65
        foreach ($rows as $row) {
66
            $remote_id = $row['id'];
67
            [
68
                $date,
69
                $name,
70
                $first_name,
71
                $sex,
72
                $father_first_name,
73
                $father_is_dead,
74
                $mother_name,
75
                $mother_first_name,
76
                $mother_is_dead,
77
                $observation1,
78
                $observation2,
79
                $observation3,
80
                $observation4,
81
                $officer,
82
                $parish,
83
                $source,
84
                $update,
85
            ] = $row['cell'];
86
87
            Geneanum::updateOrCreate(['remote_id' => $remote_id],
88
                [
89
                    'date'              => $date,
90
                    'name'              => $name,
91
                    'first_name'        => $first_name,
92
                    'sex'               => $sex,
93
                    'father_first_name' => $father_first_name,
94
                    'father_is_dead'    => $father_is_dead,
95
                    'mother_name'       => $mother_name,
96
                    'mother_first_name' => $mother_first_name,
97
                    'mother_is_dead'    => $mother_is_dead,
98
                    'observation1'      => $observation1,
99
                    'observation2'      => $observation2,
100
                    'observation3'      => $observation3,
101
                    'observation4'      => $observation4,
102
                    'officer'           => $officer,
103
                    'parish'            => $parish,
104
                    'source'            => $source,
105
                    'update'            => $update,
106
                ]);
107
        }
108
109
    }
110
}
111