Test Failed
Push — master ( 802c16...59dfb3 )
by Adam
44s queued 12s
created

AirtableClient::getBearerTokenStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 1
crap 2
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