Config   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 10
eloc 24
c 5
b 1
f 0
dl 0
loc 73
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getSpotifyUsername() 0 10 2
A getClientSecret() 0 10 2
A getClientId() 0 10 2
A getRedirectUri() 0 10 2
A get() 0 7 2
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