| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Jobs\Geneanum; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Models\Geneanum; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Bus\Queueable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Contracts\Queue\ShouldQueue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Foundation\Bus\Dispatchable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Queue\InteractsWithQueue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Queue\SerializesModels; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Support\Facades\Http; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  | abstract class Sync implements ShouldQueue | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     protected const MODEL = Geneanum::class; | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     protected const DATABASE = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     protected const MAX_RETRY = 3; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     protected const AREA = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     protected const URL = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public static $is_testing = true; //change true if want test sync | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     protected $current_page; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     protected $retry; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * Create a new job instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function __construct(int $page = 1, int $retry = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->current_page = $page; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |         $this->retry = $retry; | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * Execute the job. | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function handle(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $url = sprintf(static::URL, time() - random_int(1, 30), $this->current_page); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $response = Http::get($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if ($response->failed()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             if (self::$is_testing) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $response->throw(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             $this->retry++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             if ($this->retry > static::MAX_RETRY) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             dispatch(new static($this->current_page, $this->retry)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 ->delay(random_int(10, 60)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $result = $response->json(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (! self::$is_testing) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $has_more_result = $result['total'] > $result['page']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             if ($has_more_result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 $this->current_page++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 dispatch(new static($this->current_page)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     ->delay(now()->addSeconds(random_int(10, 60))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $rows = $result['rows']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         foreach ($rows as $row) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $remote_id = $row['id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             static::MODEL::updateOrCreate( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     'remote_id' => $remote_id, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     'area' => static::AREA, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                     'db_name' => static::DATABASE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 [ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |                     'data' => $this->getFields($row), | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |                 ] | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |             ); | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |      * get the fields to store. | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |      * | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |      * @return mixed | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |     abstract protected function getFields(array $row): array; | 
            
                                                        
            
                                    
            
            
                | 99 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  |