Completed
Push — master ( 36e8c5...2454a8 )
by Tom
12:20 queued 07:25
created

ConfigLocator::getUserConfigFilePaths()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 12
nc 4
nop 0
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
        $userConfigFile = null;
52
53
        $personalConfigFilePaths = $this->getUserConfigFilePaths();
54
55
        foreach ($personalConfigFilePaths as $personalConfigFilePath) {
56
            try {
57
                $userConfigFile = ConfigFile::createFromFile($personalConfigFilePath);
58
                $userConfigFile->applyVariables($this->magentoRootFolder);
59
                break;
60
            } catch (InvalidArgumentException $e) {
61
                $userConfigFile = null;
62
            }
63
        }
64
65
        return $userConfigFile;
66
    }
67
68
    /**
69
     * Obtain the project-config-file, it is placed in the magento app/etc dir, e.g. app/etc/n98-magerun2.yaml
70
     *
71
     * @return ConfigFile|null
72
     */
73
    public function getProjectConfigFile()
74
    {
75
        if (!strlen($this->magentoRootFolder)) {
76
            return null;
77
        }
78
79
        $projectConfigFilePath = $this->magentoRootFolder . '/app/etc/' . $this->customConfigFilename;
80
81
        try {
82
            $projectConfigFile = ConfigFile::createFromFile($projectConfigFilePath);
83
            $projectConfigFile->applyVariables($this->magentoRootFolder);
84
        } catch (InvalidArgumentException $e) {
85
            $projectConfigFile = null;
86
        }
87
88
        return $projectConfigFile;
89
    }
90
91
    /**
92
     * Obtain the (optional) stop-file-config-file, it is placed in the folder of the stop-file, always
93
     * prefixed with a dot: stop-file-folder/.n98-magerun2.yaml
94
     *
95
     * @param string $magerunStopFileFolder
96
     * @return ConfigFile|null
97
     */
98
    public function getStopFileConfigFile($magerunStopFileFolder)
99
    {
100
        if (empty($magerunStopFileFolder)) {
101
            return null;
102
        }
103
104
        $stopFileConfigFilePath = $magerunStopFileFolder . '/.' . $this->customConfigFilename;
105
106
        if (!file_exists($stopFileConfigFilePath)) {
107
            return null;
108
        }
109
110
        try {
111
            $stopFileConfigFile = ConfigFile::createFromFile($stopFileConfigFilePath);
112
            $stopFileConfigFile->applyVariables($this->magentoRootFolder);
113
        } catch (InvalidArgumentException $e) {
114
            $stopFileConfigFile = null;
115
        }
116
117
        return $stopFileConfigFile;
118
    }
119
120
    /**
121
     * @return array
122
     */
123
    private function getUserConfigFilePaths()
124
    {
125
        $homeDirectory = OperatingSystem::getHomeDir();
126
        if (!$homeDirectory) {
127
            throw new RuntimeException('Unable to get home-directory to obtain user-config-file.');
128
        }
129
130
        if (!is_dir($homeDirectory)) {
131
            throw new RuntimeException(sprintf("Home-directory '%s' is not a directory.", $homeDirectory));
132
        }
133
134
        $basename = $this->customConfigFilename;
135
136
        $paths = array();
137
        if (OperatingSystem::isWindows()) {
138
            $paths[] = $homeDirectory . '/' . $basename;
139
        }
140
        $paths[] = $homeDirectory . '/.' . $basename;
141
142
        return $paths;
143
    }
144
}
145