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

Config   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 31
c 1
b 0
f 1
dl 0
loc 83
ccs 35
cts 35
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fromValues() 0 5 1
A getBaseUrl() 0 3 1
A getApiKey() 0 3 1
A getBaseId() 0 3 1
A getVersion() 0 3 1
A fromEnvironment() 0 10 1
A validateValues() 0 17 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
     * @var string
17
     */
18
    private $version;
19
    /**
20
     * @var string
21
     */
22
    private $baseId;
23
    /**
24
     * @var string
25
     */
26
    private $apiKey;
27
28 7
    protected function __construct(string $baseUrl, string $version, string $apiKey, string $baseId)
29
    {
30 7
        $this->baseUrl = $baseUrl;
31 7
        $this->version = $version;
32 7
        $this->baseId = $baseId;
33 7
        $this->apiKey = $apiKey;
34 7
    }
35
36 5
    public static function fromEnvironment(): Config
37
    {
38 5
        $baseUrl = getenv('BASE_URL');
39 5
        $version = getenv('VERSION');
40 5
        $apiKey = getenv('API_KEY');
41 5
        $baseId = getenv('BASE_ID');
42
43 5
        self::validateValues($baseUrl, $version, $apiKey, $baseId);
44
45 4
        return new self($baseUrl, $version, $apiKey, $baseId);
46
    }
47
48 8
    public static function fromValues(string $baseUrl, string $version, string $apiKey, string $baseId): Config
49
    {
50 8
        self::validateValues($baseUrl, $version, $apiKey, $baseId);
51
52 3
        return new self($baseUrl, $version, $apiKey, $baseId);
53
    }
54
55 12
    protected static function validateValues(string $baseUrl, string $version, string $apiKey, string $baseId): bool
56
    {
57 12
        Assert::that($baseUrl)
58 12
            ->notBlank('baseUrl was blank/empty')
59 11
            ->url('baseUrl was not a valid url');
60
61 10
        Assert::that($version)
62 10
            ->regex('/v\d+/', 'Version did not match expected pattern');
63
64 9
        Assert::that($apiKey)
65 9
            ->notBlank('apiKey was blank/empty');
66
67 8
        Assert::that($baseId)
68 8
            ->notBlank('baseId was blank/empty');
69
70
        // validate
71 7
        return true;
72
    }
73
74 6
    public function getBaseUrl(): string
75
    {
76 6
        return $this->baseUrl;
77
    }
78
79 6
    public function getVersion(): string
80
    {
81 6
        return $this->version;
82
    }
83
84 6
    public function getBaseId(): string
85
    {
86 6
        return $this->baseId;
87
    }
88
89 6
    public function getApiKey(): string
90
    {
91 6
        return $this->apiKey;
92
    }
93
}
94