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

Config::fromEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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