1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\ControlDB\Export\Handler\Airtable; |
4
|
|
|
|
5
|
|
|
use BristolSU\ControlDB\Export\FormattedItem; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Illuminate\Bus\Queueable; |
8
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
9
|
|
|
use Illuminate\Foundation\Events\Dispatchable; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
|
12
|
|
|
class CreateRows implements ShouldQueue |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
use Dispatchable, Queueable; |
15
|
|
|
|
16
|
|
|
private array $data; |
|
|
|
|
17
|
|
|
private string $apiKey; |
|
|
|
|
18
|
|
|
private string $baseId; |
|
|
|
|
19
|
|
|
private string $tableName; |
|
|
|
|
20
|
|
|
/** |
|
|
|
|
21
|
|
|
* @var Collection |
22
|
|
|
*/ |
23
|
|
|
private Collection $creatingItems; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* CreateRows constructor. |
27
|
|
|
* @param array $data |
|
|
|
|
28
|
|
|
* @param string $apiKey |
|
|
|
|
29
|
|
|
* @param string $baseId |
|
|
|
|
30
|
|
|
* @param string $tableName |
|
|
|
|
31
|
|
|
* @param Collection|FormattedItem[] $creatingItems |
|
|
|
|
32
|
|
|
*/ |
33
|
3 |
|
public function __construct(array $data, string $apiKey, string $baseId, string $tableName) |
34
|
|
|
{ |
35
|
3 |
|
$this->data = $data; |
36
|
3 |
|
$this->apiKey = $apiKey; |
37
|
3 |
|
$this->baseId = $baseId; |
38
|
3 |
|
$this->tableName = $tableName; |
39
|
3 |
|
} |
40
|
|
|
|
41
|
2 |
|
public function handle(Client $client) |
|
|
|
|
42
|
|
|
{ |
43
|
2 |
|
$response = $client->post( |
44
|
2 |
|
sprintf('https://api.airtable.com/v0/%s/%s', $this->baseId, $this->tableName), |
45
|
|
|
[ |
46
|
2 |
|
'json' => $this->data, |
47
|
|
|
'headers' => [ |
48
|
2 |
|
'Authorization' => 'Bearer ' . $this->apiKey |
49
|
|
|
] |
50
|
|
|
] |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** @var IdRetriever $idRetriver */ |
|
|
|
|
54
|
2 |
|
$idRetriver = app(IdRetriever::class, [ |
|
|
|
|
55
|
2 |
|
'baseId' => $this->baseId, |
56
|
2 |
|
'tableName' => $this->tableName |
57
|
|
|
]); |
|
|
|
|
58
|
|
|
|
59
|
2 |
|
$records = json_decode($response->getBody()->getContents(), true); |
60
|
2 |
|
foreach($records['records'] as $record) { |
|
|
|
|
61
|
2 |
|
$idRetriver->pushId($record['id']); |
62
|
|
|
} |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
} |