RootDiscovery   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 190
ccs 40
cts 40
cp 1
c 0
b 0
f 0
wmc 31
lcom 1
cbo 1
rs 9.8

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A getRootDirectory() 0 8 2
A getConfigReader() 0 7 1
A getInstallationType() 0 12 3
A getConfigurationFilePath() 0 9 3
A discoverRootDirectory() 0 16 4
A checkCurrentDirectory() 0 10 3
A checkParentDirectories() 0 13 3
A checkChildDirectories() 0 20 4
A isDirectoryRoot() 0 4 2
A isMagentoOneDirectoryRoot() 0 4 1
A isMagentoTwoDirectoryRoot() 0 4 1
A getLocalXmlPath() 0 4 1
A getEnvPhpPath() 0 4 1
1
<?php
2
3
namespace Meanbee\LibMageConf;
4
5
use Meanbee\LibMageConf\ConfigReader\Factory as ConfigReaderFactory;
6
use RecursiveDirectoryIterator;
7
use RecursiveIteratorIterator;
8
use SplFileInfo;
9
10
/**
11
 * Discover the root directory of a Magento installation given a starting directory.
12
 *
13
 * @package Meanbee\LibMageConf
14
 */
15
class RootDiscovery
16
{
17
    protected $baseDirectory;
18
    protected $rootDirectory;
19
    protected $configReaderFactory;
20
21
    /**
22 5
     * @param string $baseDirectory The directory from which to start the search
23
     * @param ConfigReaderFactory $configReaderFactory
24 5
     */
25 5
    public function __construct($baseDirectory, ConfigReaderFactory $configReaderFactory = null)
26
    {
27
        if ($configReaderFactory === null) {
28
            $configReaderFactory = new ConfigReaderFactory();
29
        }
30
31
        $this->baseDirectory = $baseDirectory;
32 5
        $this->configReaderFactory = $configReaderFactory;
33
    }
34 5
35 5
    /**
36
     * The root directory of the first Magento installation found in the base directory.
37
     *
38 5
     * @return null|string
39
     */
40
    public function getRootDirectory()
41
    {
42
        if ($this->rootDirectory === null) {
43
            $this->rootDirectory = $this->discoverRootDirectory();
44 5
        }
45
46 5
        return $this->rootDirectory;
47 1
    }
48
49
    /**
50 4
     * @return ConfigReader
51 1
     */
52
    public function getConfigReader()
53
    {
54 3
        return $this->configReaderFactory->create(
55 2
            $this->getConfigurationFilePath(),
56
            $this->getInstallationType()
57
        );
58 1
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getInstallationType()
64 5
    {
65
        if ($this->isMagentoOneDirectoryRoot($this->getRootDirectory())) {
66 5
            return MagentoType::MAGENTO_1;
67 5
        }
68 1
69
        if ($this->isMagentoTwoDirectoryRoot($this->getRootDirectory())) {
70
            return MagentoType::MAGENTO_2;
71
        }
72 4
73
        return MagentoType::UNKNOWN;
74
    }
75
76
    /**
77
     * @return string
78 4
     */
79
    protected function getConfigurationFilePath()
80 4
    {
81 4
        switch ($this->getInstallationType()) {
82 4
            case MagentoType::MAGENTO_1:
83 1
                return $this->getLocalXmlPath($this->getRootDirectory());
84
            case MagentoType::MAGENTO_2:
85
                return $this->getEnvPhpPath($this->getRootDirectory());
86 3
        }
87
    }
88
89 3
    /**
90
     * @return null|string
91
     */
92
    protected function discoverRootDirectory()
93
    {
94
        if ($dir = $this->checkCurrentDirectory()) {
95 3
            return $dir;
96
        }
97 3
98 3
        if ($dir = $this->checkParentDirectories()) {
99 3
            return $dir;
100
        }
101
102 3
        if ($dir = $this->checkChildDirectories()) {
103
            return $dir;
104 3
        }
105
106 3
        return null;
107 3
    }
108 3
109
    /**
110
     * @return null|string
111
     */
112
    protected function checkCurrentDirectory()
113 1
    {
114
        if (is_dir($this->baseDirectory)) {
115
            if ($this->isDirectoryRoot($this->baseDirectory)) {
116
                return $this->baseDirectory;
117
            }
118
        }
119
120 5
        return null;
121
    }
122 5
123
    /**
124 5
     * @return null|string
125
     */
126
    protected function checkParentDirectories()
127
    {
128
        $dir = new SplFileInfo($this->baseDirectory);
129
        while ($testDir = $dir->getPath()) {
130
            if ($this->isDirectoryRoot($testDir)) {
131
                return $testDir;
132
            }
133
134
            $dir = new SplFileInfo($testDir);
135
        }
136
137
        return null;
138
    }
139
140
    /**
141
     * @return null|string
142
     */
143
    protected function checkChildDirectories()
144
    {
145
        $iterator = new RecursiveIteratorIterator(
146
            new RecursiveDirectoryIterator($this->baseDirectory),
147
            RecursiveIteratorIterator::SELF_FIRST
148
        );
149
150
        foreach($iterator as $fileInfo) {
151
            /** @var SplFileInfo $fileInfo */
152
            $filename = $fileInfo->getPath() . DIRECTORY_SEPARATOR . $fileInfo->getFilename();
153
154
            if ($fileInfo->isDir()) {
155
                if ($this->isDirectoryRoot($filename)) {
156
                    return $filename;
157
                }
158
            }
159
        }
160
161
        return null;
162
    }
163
164
    /**
165
     * @param $directory
166
     * @return bool
167
     */
168
    protected function isDirectoryRoot($directory)
169
    {
170
        return $this->isMagentoOneDirectoryRoot($directory) || $this->isMagentoTwoDirectoryRoot($directory);
171
    }
172
173
    /**
174
     * @param $directory
175
     * @return bool
176
     */
177
    protected function isMagentoOneDirectoryRoot($directory)
178
    {
179
        return file_exists($this->getLocalXmlPath($directory));
180
    }
181
182
    protected function isMagentoTwoDirectoryRoot($directory)
183
    {
184
        return file_exists($this->getEnvPhpPath($directory));
185
    }
186
187
    /**
188
     * @param $directory
189
     * @return string
190
     */
191
    protected function getLocalXmlPath($directory)
192
    {
193
        return sprintf("%s/app/etc/local.xml", $directory);
194
    }
195
196
    /**
197
     * @param $directory
198
     * @return string
199
     */
200
    protected function getEnvPhpPath($directory)
201
    {
202
        return sprintf("%s/app/etc/env.php", $directory);
203
    }
204
}