1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tapp\Airtable; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Tapp\Airtable\Api\AirtableApiClient; |
8
|
|
|
|
9
|
|
|
class AirtableManager |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The application instance. |
13
|
|
|
* |
14
|
|
|
* @var \Illuminate\Foundation\Application |
15
|
|
|
*/ |
16
|
|
|
protected $app; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The active connection instances. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $tables = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new Airtable manager instance. |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Foundation\Application $app |
29
|
|
|
* @return void |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function __construct($app) |
32
|
|
|
{ |
33
|
|
|
$this->app = $app; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get a airtable table instance. |
38
|
|
|
* |
39
|
|
|
* @param string $name |
40
|
|
|
* @return \Airtable\Table |
41
|
|
|
*/ |
42
|
|
|
public function table($name = null) |
43
|
|
|
{ |
44
|
|
|
$name = $name ?: $this->getDefaultTable(); |
45
|
|
|
|
46
|
|
|
return $this->tables[$name] = $this->get($name); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the configuration for a table. |
51
|
|
|
* |
52
|
|
|
* @param string $name |
53
|
|
|
* @return array |
54
|
|
|
* |
55
|
|
|
* @throws \InvalidArgumentException |
56
|
|
|
*/ |
57
|
|
|
protected function configuration($name) |
58
|
|
|
{ |
59
|
|
|
$name = $name ?: $this->getDefaultTable(); |
60
|
|
|
|
61
|
|
|
// To get the aritable table configuration, we will just pull each of the |
62
|
|
|
// connection configurations and get the configurations for the given name. |
63
|
|
|
// If the configuration doesn't exist, we'll throw an exception and bail. |
64
|
|
|
$tables = $this->app['config']['airtable.tables']; |
65
|
|
|
|
66
|
|
|
if (is_null($config = Arr::get($tables, $name))) { |
67
|
|
|
throw new InvalidArgumentException("Table [{$name}] not configured."); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $config; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the default connection name. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getDefaultTable() |
79
|
|
|
{ |
80
|
|
|
return $this->app['config']['airtable.default']; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the default connection name. |
85
|
|
|
* |
86
|
|
|
* @param string $name |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function setDefaultTable($name) |
90
|
|
|
{ |
91
|
|
|
$this->app['config']['airtable.default'] = $name; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return all of the created connections. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
public function getTables() |
100
|
|
|
{ |
101
|
|
|
return $this->tables; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Set the given table instance. |
106
|
|
|
* |
107
|
|
|
* @param string $name |
108
|
|
|
* @param mixed $table |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function set($name, $table) |
112
|
|
|
{ |
113
|
|
|
$this->tables[$name] = $table; |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Attempt to get the table from the local cache. |
120
|
|
|
* |
121
|
|
|
* @param string $name |
122
|
|
|
* @return \Tapp\Airtable |
123
|
|
|
*/ |
124
|
|
|
protected function get($name) |
125
|
|
|
{ |
126
|
|
|
return $this->tables[$name] ?? $this->resolve($name); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Resolve the given table. |
131
|
|
|
* |
132
|
|
|
* @param string $name |
133
|
|
|
* @return Airtable |
134
|
|
|
* |
135
|
|
|
* @throws \InvalidArgumentException |
136
|
|
|
*/ |
137
|
|
|
protected function resolve($name) |
138
|
|
|
{ |
139
|
|
|
$config = $this->getConfig($name); |
140
|
|
|
|
141
|
|
|
if ($config) { |
|
|
|
|
142
|
|
|
return $this->createAirtable($config); |
143
|
|
|
} else { |
144
|
|
|
throw new InvalidArgumentException("Table [{$name}] is not configured."); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function createAirtable($config) |
149
|
|
|
{ |
150
|
|
|
$base = $this->app['config']['airtable.base']; |
151
|
|
|
$access_token = $this->app['config']['airtable.key']; |
152
|
|
|
|
153
|
|
|
$client = new AirtableApiClient($base, $config, $access_token); |
154
|
|
|
|
155
|
|
|
return new Airtable($client, $config); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get the table configuration. |
160
|
|
|
* |
161
|
|
|
* @param string $name |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
protected function getConfig($name) |
165
|
|
|
{ |
166
|
|
|
return $this->app['config']["airtable.tables.{$name}"]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Dynamically pass methods to the default connection. |
171
|
|
|
* |
172
|
|
|
* @param string $method |
173
|
|
|
* @param array $parameters |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
|
|
public function __call($method, $parameters) |
177
|
|
|
{ |
178
|
|
|
return $this->table()->$method(...$parameters); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.