Test Failed
Pull Request — master (#23)
by
unknown
01:42
created

AirtableClient   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A getClient() 0 3 1
A getTable() 0 6 1
A getBearerTokenStack() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Beachcasts\Airtable;
6
7
use Beachcasts\Airtable\Middleware\BearerTokenMiddleware;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\Handler\CurlHandler;
10
use GuzzleHttp\HandlerStack;
11
use GuzzleHttp\Middleware;
12
13
/**
14
 * Class AirtableClient
15
 * @package Beachcasts\Airtable
16
 */
17
class AirtableClient
18
{
19
    /**
20
     * Base identifier
21
     *
22
     * @var null|string $baseId
23
     */
24
    protected $baseId;
25
26
    /**
27
     * Guzzle client object
28
     *
29
     * @var Client|null
30
     */
31
    protected $client;
32
33
    /**
34 2
     * Airtable constructor. Create a new Airtable Instance
35
     *
36 2
     * @param string $apiKey
37 2
     * @param string $baseId
38
     */
39
    public function __construct(string $apiKey, string $baseId)
40 2
    {
41 2
        $this->client = new Client(
42
            [
43
                'base_uri' => getenv('BASE_URL') . '/' . getenv('VERSION') . '/' . $baseId . '/',
44
                'handler' => $this->getBearerTokenStack($apiKey)
45
            ]
46 2
        );
47
48 2
        $this->baseId = $baseId;
49
    }
50
51
    private function getBearerTokenStack(string $apiKey): HandlerStack
52
    {
53
        $stack = new HandlerStack(new CurlHandler());
54
        $stack->push(Middleware::mapRequest(new BearerTokenMiddleware($apiKey)), BearerTokenMiddleware::class);
55
56
        return $stack;
57
    }
58
59
    /**
60
     * @return Client|null
61
     */
62
    public function getClient(): Client
63
    {
64
        return $this->client;
65
    }
66
67
    public function getTable(string $tableName, string $viewName = "Grid view"): Table
68
    {
69
        $table = new Table($tableName, $viewName);
70
        $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

70
        $table->setClient(/** @scrutinizer ignore-type */ $this->client);
Loading history...
71
72
        return $table;
73
    }
74
}
75