Config::getClientId()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 0
1
<?php declare(strict_types=1);
2
3
namespace SpotifyApiConnect\Domain\Model;
4
5
use SpotifyApiConnect\Message;
6
use RuntimeException;
7
8
final class Config implements ConfigInterface
9
{
10
    /**
11
     * @return string
12
     */
13
    public function getClientId(): string
14
    {
15
        $clientId = (string)$this->get('CLIENT_ID');
16
        if (empty($clientId)) {
17
            throw new RuntimeException(
18
                sprintf(Message::ERROR_GET_ENV_VARIABLE, 'CLIENT_ID')
19
            );
20
        }
21
22
        return $clientId;
23
    }
24
25
    /**
26
     * @return string
27
     */
28
    public function getClientSecret(): string
29
    {
30
        $clientSecret = (string)$this->get('CLIENT_SECRET');
31
        if (empty($clientSecret)) {
32
            throw new RuntimeException(
33
                sprintf(Message::ERROR_GET_ENV_VARIABLE, 'CLIENT_SECRET')
34
            );
35
        }
36
37
        return $clientSecret;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    public function getRedirectUri(): string
44
    {
45
        $redirectUri = (string)$this->get('REDIRECT_URI');
46
        if (empty($redirectUri)) {
47
            throw new RuntimeException(
48
                sprintf(Message::ERROR_GET_ENV_VARIABLE, 'REDIRECT_URI')
49
            );
50
        }
51
52
        return $redirectUri;
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getSpotifyUsername(): string
59
    {
60
        $spotifyUsername = (string)$this->get('SPOTIFY_USERNAME');
61
        if (empty($spotifyUsername)) {
62
            throw new RuntimeException(
63
                sprintf(Message::ERROR_GET_ENV_VARIABLE, 'SPOTIFY_USERNAME')
64
            );
65
        }
66
67
        return $spotifyUsername;
68
    }
69
70
    /**
71
     * @param string $name
72
     * @return mixed
73
     */
74
    private function get(string $name)
75
    {
76
        if (isset($_ENV[$name])) {
77
            return $_ENV[$name];
78
        }
79
80
        return getenv($name);
81
    }
82
}
83