AirtableClient   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 4
b 0
f 0
dl 0
loc 50
ccs 17
cts 17
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getClient() 0 3 1
A getBearerTokenStack() 0 6 1
A getTable() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Beachcasts\Airtable;
6
7
use Assert\Assert;
8
use Beachcasts\Airtable\Middleware\BearerTokenMiddleware;
9
use GuzzleHttp\Client;
10
use GuzzleHttp\Handler\CurlHandler;
11
use GuzzleHttp\HandlerStack;
12
use GuzzleHttp\Middleware;
13
14
/**
15
 * Class AirtableClient
16
 * @package Beachcasts\Airtable
17
 */
18
class AirtableClient
19
{
20
    /**
21
     * Guzzle client object
22
     *
23
     * @var Client|null
24
     */
25
    protected $client;
26
27
    /**
28
     * Airtable constructor. Create a new Airtable Instance
29
     *
30
     * @param Config $config
31
     * @param string $baseId
32
     */
33 4
    public function __construct(Config $config, string $baseId)
34
    {
35 4
        Assert::that($baseId)
36 4
            ->notBlank('AirTable requires a non-empty $baseId');
37
38 4
        $this->client = new Client(
39
            [
40 4
                'base_uri' => $config->getBaseUrl() . '/' . $config->getVersion() . '/' . $baseId . '/',
41 4
                'handler' => $this->getBearerTokenStack($config->getApiKey())
42
            ]
43
        );
44 4
    }
45
46 4
    private function getBearerTokenStack(string $apiKey): HandlerStack
47
    {
48 4
        $stack = new HandlerStack(new CurlHandler());
49 4
        $stack->push(Middleware::mapRequest(new BearerTokenMiddleware($apiKey)), BearerTokenMiddleware::class);
50
51 4
        return $stack;
52
    }
53
54
    /**
55
     * @return Client|null
56
     */
57 2
    public function getClient(): Client
58
    {
59 2
        return $this->client;
60
    }
61
62 1
    public function getTable(string $tableName): Table
63
    {
64 1
        $table = new Table($tableName);
65 1
        $table->setClient($this->client);
0 ignored issues
show
Bug introduced by
It seems like $this->client can also be of type null; however, parameter $client of Beachcasts\Airtable\Table::setClient() does only seem to accept GuzzleHttp\Client, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        $table->setClient(/** @scrutinizer ignore-type */ $this->client);
Loading history...
66
67 1
        return $table;
68
    }
69
}
70