1 | <?php |
||
2 | |||
3 | namespace GDCInfo\Models; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | use GDCInfo\Exceptions\GDCInfoException; |
||
7 | use GDCInfo\Facades\GDCInfoManager; |
||
8 | use Illuminate\Database\Eloquent\Model; |
||
9 | |||
10 | class GDCInfo extends Model |
||
11 | { |
||
12 | protected $primaryKey = 'gdc'; |
||
13 | public $incrementing = false; |
||
14 | |||
15 | protected $guarded = []; |
||
16 | |||
17 | protected $casts = [ |
||
18 | 'first_registered_on' => 'date', |
||
19 | 'current_period_from' => 'date', |
||
20 | 'current_period_until' => 'date', |
||
21 | 'last_fetched_at' => 'datetime', |
||
22 | 'data' => 'array', |
||
23 | ]; |
||
24 | |||
25 | 2 | public function getTable(): string |
|
26 | { |
||
27 | 2 | return config('gdc-info.tables.gdc_info'); |
|
28 | } |
||
29 | |||
30 | |||
31 | public function infoFromData(): \GDCInfo\GDCInfo |
||
32 | { |
||
33 | return \GDCInfo\GDCInfo::fromArray($this->data); |
||
34 | } |
||
35 | |||
36 | 2 | public static function findOrFetch(int $gdc): ?static |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
37 | { |
||
38 | 2 | return static::find($gdc) ?? static::fetch($gdc); |
|
39 | } |
||
40 | |||
41 | 2 | public static function fetch(int $gdc): ?static |
|
42 | { |
||
43 | try { |
||
44 | /** @var \GDCInfo\GDCInfo $gdcInfo */ |
||
45 | 2 | $gdcInfo = GDCInfoManager::infoByGdcNumber($gdc); |
|
46 | 1 | } catch (GDCInfoException $e) { |
|
47 | // nothing. |
||
48 | 1 | return null; |
|
49 | } |
||
50 | |||
51 | 1 | $model = static::query()->find($gdcInfo->gdc()); |
|
52 | 1 | if (!$model) { |
|
53 | 1 | $model = new static([ |
|
54 | 1 | (new static)->getKeyName() => $gdcInfo->gdc(), |
|
55 | 1 | ]); |
|
56 | } |
||
57 | |||
58 | 1 | $model->first_name = $gdcInfo->firstName(); |
|
59 | 1 | $model->last_name = $gdcInfo->lastName(); |
|
60 | 1 | $model->status = $gdcInfo->status(); |
|
61 | 1 | $model->registrant_type = $gdcInfo->registrantType(); |
|
62 | 1 | $model->qualifications = $gdcInfo->qualifications(); |
|
63 | 1 | $model->first_registered_on = $gdcInfo->firstRegisteredOn(); |
|
64 | 1 | $model->current_period_from = $gdcInfo->currentPeriodFrom(); |
|
65 | 1 | $model->current_period_until = $gdcInfo->currentPeriodUntil(); |
|
66 | 1 | $model->data = $gdcInfo->toArray(); |
|
67 | 1 | $model->last_fetched_at = Carbon::now(); |
|
68 | 1 | $model->save(); |
|
69 | |||
70 | 1 | return $model; |
|
71 | } |
||
72 | } |
||
73 |