ConfigPath::getUserConfigFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * CRM library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\Crm;
7
8
use XdgBaseDir\Xdg;
9
10
class ConfigPath
11
{
12
    /**
13
     * The config dir
14
     * @var string
15
     */
16
    protected static $homeConfigDir;
17
18
    /**
19
     * Gets the config dir of the current user
20
     * @return string
21
     */
22
    public static function getHomeConfigDir()
23
    {
24
        if (static::$homeConfigDir) {
25
            return static::$homeConfigDir;
26
        }
27
        if (static::isWindows()) {
28
            // @codeCoverageIgnoreStart
29
            if (!getenv('APPDATA')) {
30
                throw new \RuntimeException('The APPDATA environment variable must be set for crm to run correctly');
31
            }
32
            $homeConfigDir = rtrim(strtr(getenv('APPDATA'), '\\', '/'), '/') . '/ComposerRegistryManager';
33
            //codeCoverageIgnoreEnd
34
        } else {
35
            $homeConfigDir = (new Xdg())->getHomeConfigDir() . '/composer-registry-manager';
36
        }
37
        return static::$homeConfigDir = $homeConfigDir;
38
    }
39
40
    /**
41
     * Checks whether the os is windows
42
     * @return bool
43
     */
44
    public static function isWindows()
45
    {
46
        return DIRECTORY_SEPARATOR == '\\';
47
    }
48
49
    /**
50
     * Get default config json file
51
     * @return string
52
     */
53
    public static function getDefaultConfigFile()
54
    {
55
        return __DIR__ . '/../crm.default.json';
56
    }
57
58
    /**
59
     * Get configuration file of the user
60
     * @return string
61
     */
62
    public static function getUserConfigFile()
63
    {
64
        return static::getHomeConfigDir() . '/crm.json';
65
    }
66
}
67