Config::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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