1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\AirTable\Jobs; |
4
|
|
|
|
5
|
|
|
use BristolSU\AirTable\AirTable; |
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\Support\Facades\Log; |
11
|
|
|
use Spatie\RateLimitedMiddleware\RateLimited; |
12
|
|
|
|
13
|
|
|
class CreateRecords implements ShouldQueue |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
use Dispatchable, Queueable, InteractsWithQueue; |
16
|
|
|
|
17
|
|
|
private array $data; |
|
|
|
|
18
|
|
|
private string $apiKey; |
|
|
|
|
19
|
|
|
private string $baseId; |
|
|
|
|
20
|
|
|
private string $tableName; |
|
|
|
|
21
|
|
|
private bool $debug = false; |
|
|
|
|
22
|
|
|
|
23
|
5 |
|
public function __construct(array $data, string $apiKey, string $baseId, string $tableName) |
|
|
|
|
24
|
|
|
{ |
25
|
5 |
|
$this->data = $data; |
26
|
5 |
|
$this->apiKey = $apiKey; |
27
|
5 |
|
$this->baseId = $baseId; |
28
|
5 |
|
$this->tableName = $tableName; |
29
|
5 |
|
} |
30
|
|
|
|
31
|
|
|
public function middleware() |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$rateLimitedMiddleware = (new RateLimited()) |
34
|
|
|
->key('airtable') |
35
|
|
|
->allow(1) |
36
|
|
|
->everySeconds(1) |
37
|
|
|
->releaseAfterSeconds(3); |
38
|
|
|
|
39
|
|
|
return [$rateLimitedMiddleware]; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function handle(AirTable $airTable) |
|
|
|
|
43
|
|
|
{ |
44
|
1 |
|
$airTable->setApiKey($this->apiKey); |
45
|
1 |
|
$airTable->setBaseId($this->baseId); |
46
|
1 |
|
$airTable->setTableName($this->tableName); |
47
|
1 |
|
$this->log('Creating Rows'); |
48
|
1 |
|
$airTable->createRows($this->data, true); |
49
|
1 |
|
$this->log('Created Rows'); |
50
|
1 |
|
} |
51
|
|
|
|
52
|
4 |
|
public function withDebug(bool $debug) |
|
|
|
|
53
|
|
|
{ |
54
|
4 |
|
$this->debug = $debug; |
55
|
4 |
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function retryUntil() : \DateTime |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
return now()->addHours(5); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
private function log(string $string) |
|
|
|
|
64
|
|
|
{ |
65
|
1 |
|
if($this->debug) { |
|
|
|
|
66
|
|
|
Log::debug($string); |
67
|
|
|
} |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
} |