Completed
Push — master ( 3523cb...ac48e3 )
by Tom
9s
created

ConfigLocator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
/*
3
 * this file is part of magerun
4
 *
5
 * @author Tom Klingenberg <https://github.com/ktomk>
6
 */
7
8
namespace N98\Magento\Application;
9
10
use InvalidArgumentException;
11
use N98\Util\OperatingSystem;
12
use RuntimeException;
13
14
/**
15
 * Class ConfigLocator
16
 *
17
 * Has all the information encoded to retrieve the various config files
18
 *
19
 * @package N98\Magento\Application
20
 */
21
class ConfigLocator
22
{
23
    /**
24
     * @var string
25
     */
26
    private $customConfigFilename;
27
28
    /**
29
     * @var string
30
     */
31
    private $magentoRootFolder;
32
33
    /**
34
     * ConfigLocator constructor.
35
     * @param string $configFilename
36
     * @param string $magentoRootFolder
37
     */
38
    public function __construct($configFilename, $magentoRootFolder)
39
    {
40
        $this->customConfigFilename = $configFilename;
41
        $this->magentoRootFolder = $magentoRootFolder;
42
    }
43
44
    /**
45
     * Obtain the user-config-file, it is placed in the homedir, e.g. ~/.n98-magerun2.yaml
46
     *
47
     * @return ConfigFile|null
48
     */
49
    public function getUserConfigFile()
50
    {
51
        $personalConfigFilePath = $this->getUserConfigFilePath();
52
53
        try {
54
            $userConfigFile = ConfigFile::createFromFile($personalConfigFilePath);
55
            $userConfigFile->applyVariables($this->magentoRootFolder);
56
        } catch (InvalidArgumentException $e) {
57
            $userConfigFile = null;
58
        }
59
60
        return $userConfigFile;
61
    }
62
63
    /**
64
     * Obtain the project-config-file, it is placed in the magento app/etc dir, e.g. app/etc/n98-magerun2.yaml
65
     *
66
     * @return ConfigFile|null
67
     */
68
    public function getProjectConfigFile()
69
    {
70
        if (!strlen($this->magentoRootFolder)) {
71
            return null;
72
        }
73
74
        $projectConfigFilePath = $this->magentoRootFolder . '/app/etc/' . $this->customConfigFilename;
75
76
        try {
77
            $projectConfigFile = ConfigFile::createFromFile($projectConfigFilePath);
78
            $projectConfigFile->applyVariables($this->magentoRootFolder);
79
        } catch (InvalidArgumentException $e) {
80
            $projectConfigFile = null;
81
        }
82
83
        return $projectConfigFile;
84
    }
85
86
    /**
87
     * Obtain the (optional) stop-file-config-file, it is placed in the folder of the stop-file, always
88
     * prefixed with a dot: stop-file-folder/.n98-magerun2.yaml
89
     *
90
     * @param string $magerunStopFileFolder
91
     * @return ConfigFile|null
92
     */
93
    public function getStopFileConfigFile($magerunStopFileFolder)
94
    {
95
        if (empty($magerunStopFileFolder)) {
96
            return null;
97
        }
98
99
        $stopFileConfigFilePath = $magerunStopFileFolder . '/.' . $this->customConfigFilename;
100
101
        if (!file_exists($stopFileConfigFilePath)) {
102
            return null;
103
        }
104
105
        try {
106
            $stopFileConfigFile = ConfigFile::createFromFile($stopFileConfigFilePath);
107
            $stopFileConfigFile->applyVariables($this->magentoRootFolder);
108
        } catch (InvalidArgumentException $e) {
109
            $stopFileConfigFile = null;
110
        }
111
112
        return $stopFileConfigFile;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    private function getUserConfigFilePath()
119
    {
120
        $homeDirectory = OperatingSystem::getHomeDir();
121
        if (!$homeDirectory) {
122
            throw new RuntimeException('Unable to get home-directory to obtain user-config-file.');
123
        }
124
125
        if (!is_dir($homeDirectory)) {
126
            throw new RuntimeException(sprintf("Home-directory '%s' is not a directory.", $homeDirectory));
127
        }
128
129
        $basename = $this->customConfigFilename;
130
        if (!OperatingSystem::isWindows()) {
131
            $basename = ".$basename";
132
        }
133
134
        return $homeDirectory . DIRECTORY_SEPARATOR . $basename;
135
    }
136
}
137