Config   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 7
eloc 24
c 4
b 0
f 2
dl 0
loc 68
ccs 28
cts 28
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromValues() 0 5 1
A getBaseUrl() 0 3 1
A getApiKey() 0 3 1
A getVersion() 0 3 1
A validateValues() 0 13 1
A fromEnvironment() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Beachcasts\Airtable;
6
7
use Assert\Assert;
8
9
class Config
10
{
11
    /**
12
     * @var string
13
     */
14
    private $baseUrl;
15
16
    /**
17
     * @var string
18
     */
19
    private $version;
20
21
    /**
22
     * @var string
23
     */
24
    private $apiKey;
25
26 6
    protected function __construct(string $baseUrl, string $version, string $apiKey)
27
    {
28 6
        $this->baseUrl = $baseUrl;
29 6
        $this->version = $version;
30 6
        $this->apiKey = $apiKey;
31 6
    }
32
33 2
    public static function fromEnvironment(): self
34
    {
35 2
        $baseUrl = getenv('AIRTABLE_BASE_URL');
36 2
        $version = getenv('AIRTABLE_API_VERSION');
37 2
        $apiKey = getenv('AIRTABLE_API_KEY');
38
39 2
        return self::fromValues($baseUrl, $version, $apiKey);
40
    }
41
42 10
    public static function fromValues(string $baseUrl, string $version, string $apiKey): self
43
    {
44 10
        self::validateValues($baseUrl, $version, $apiKey);
45
46 6
        return new self($baseUrl, $version, $apiKey);
47
    }
48
49 10
    protected static function validateValues(string $baseUrl, string $version, string $apiKey): bool
50
    {
51 10
        Assert::that($baseUrl)
52 10
            ->notBlank('baseUrl was blank/empty')
53 9
            ->url('baseUrl was not a valid url');
54
55 8
        Assert::that($version)
56 8
            ->regex('/v\d+/', 'Version did not match expected pattern');
57
58 7
        Assert::that($apiKey)
59 7
            ->notBlank('apiKey was blank/empty');
60
61 6
        return true;
62
    }
63
64 6
    public function getBaseUrl(): string
65
    {
66 6
        return $this->baseUrl;
67
    }
68
69 6
    public function getVersion(): string
70
    {
71 6
        return $this->version;
72
    }
73
74 6
    public function getApiKey(): string
75
    {
76 6
        return $this->apiKey;
77
    }
78
}
79