Passed
Pull Request — master (#27)
by
unknown
01:47
created

AirtableClient::getBearerTokenStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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
     * Guzzle client object
21
     *
22
     * @var Client|null
23
     */
24
    protected $client;
25
    /**
26
     * @var Config
27
     */
28
    private $config;
29
30
    /**
31
     * Airtable constructor. Create a new Airtable Instance
32
     *
33
     * @param Config $config
34
     */
35 5
    public function __construct(Config $config)
36
    {
37 5
        $this->client = new Client(
38
            [
39 5
                'base_uri' => $config->getBaseUrl() . '/' . $config->getVersion() . '/' . $config->getBaseId() . '/',
40 5
                'handler' => $this->getBearerTokenStack($config->getApiKey())
41
            ]
42
        );
43
44 5
        $this->config = $config;
45 5
    }
46
47 5
    private function getBearerTokenStack(string $apiKey): HandlerStack
48
    {
49 5
        $stack = new HandlerStack(new CurlHandler());
50 5
        $stack->push(Middleware::mapRequest(new BearerTokenMiddleware($apiKey)), BearerTokenMiddleware::class);
51
52 5
        return $stack;
53
    }
54
55
    /**
56
     * @return Client|null
57
     */
58 4
    public function getClient(): Client
59
    {
60 4
        return $this->client;
61
    }
62
63 1
    public function getTable(string $tableName, string $viewName = "Grid view"): Table
64
    {
65 1
        $table = new Table($tableName, $viewName);
66 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

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