AirtableManager   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 3
dl 0
loc 172
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A table() 0 6 2
A configuration() 0 15 3
A getDefaultTable() 0 4 1
A setDefaultTable() 0 4 1
A getTables() 0 4 1
A set() 0 6 1
A get() 0 4 1
A resolve() 0 10 2
A createAirtable() 0 9 1
A getConfig() 0 4 1
A __call() 0 4 1
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

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.

Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $config of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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