Completed
Pull Request — develop (#942)
by Luke
06:05
created

MagentoHelper::_search()   C

Complexity

Conditions 10
Paths 46

Size

Total Lines 57
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 34
nc 46
nop 1
dl 0
loc 57
rs 6.7123
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace N98\Util\Console\Helper;
4
5
use N98\Magento\Application;
6
use Symfony\Component\Console\Helper\Helper as AbstractHelper;
7
use Symfony\Component\Console\Input\ArgvInput;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\ConsoleOutput;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Finder\Finder;
12
13
/**
14
 * Class MagentoHelper
15
 *
16
 * @package N98\Util\Console\Helper
17
 */
18
class MagentoHelper extends AbstractHelper
19
{
20
    /**
21
     * @var string
22
     */
23
    protected $_magentoRootFolder = null;
24
25
    /**
26
     * @var int
27
     */
28
    protected $_magentoMajorVersion = \N98\Magento\Application::MAGENTO_MAJOR_VERSION_1;
29
30
    /**
31
     * @var bool
32
     */
33
    protected $_magentoEnterprise = false;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $_magerunStopFileFound = false;
39
40
    /**
41
     * @var string
42
     */
43
    protected $_magerunStopFileFolder = null;
44
45
    /**
46
     * @var InputInterface
47
     */
48
    protected $input;
49
50
    /**
51
     * @var OutputInterface
52
     */
53
    protected $output;
54
55
    /**
56
     * @var string
57
     */
58
    protected $_customConfigFilename = 'n98-magerun.yaml';
59
60
    /**
61
     * Returns the canonical name of this helper.
62
     *
63
     * @return string The canonical name
64
     *
65
     * @api
66
     */
67
    public function getName()
68
    {
69
        return 'magento';
70
    }
71
72
    /**
73
     * @param InputInterface $input
74
     * @param OutputInterface $output
75
     */
76
    public function __construct(InputInterface $input = null, OutputInterface $output = null)
77
    {
78
        if (null === $input) {
79
            $input = new ArgvInput();
80
        }
81
82
        if (null === $output) {
83
            $output = new ConsoleOutput();
84
        }
85
86
        $this->input = $input;
87
        $this->output = $output;
88
    }
89
90
    /**
91
     * Start Magento detection
92
     *
93
     * @param string $folder
94
     * @param array $subFolders [optional] sub-folders to check
95
     * @return bool
96
     */
97
    public function detect($folder, array $subFolders = array())
98
    {
99
        $folders = $this->splitPathFolders($folder);
100
        $folders = $this->checkMagerunFile($folders);
101
        $folders = $this->checkModman($folders);
102
        $folders = array_merge($folders, $subFolders);
103
104
        foreach (array_reverse($folders) as $searchFolder) {
105
            if (!is_dir($searchFolder) || !is_readable($searchFolder)) {
106
                continue;
107
            }
108
109
            $found = $this->_search($searchFolder);
110
            if ($found) {
111
                return true;
112
            }
113
        }
114
115
        return false;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getRootFolder()
122
    {
123
        return $this->_magentoRootFolder;
124
    }
125
126
    public function getEdition()
127
    {
128
        return $this->_magentoMajorVersion;
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function isEnterpriseEdition()
135
    {
136
        return $this->_magentoEnterprise;
137
    }
138
139
    /**
140
     * @return int
141
     */
142
    public function getMajorVersion()
143
    {
144
        return $this->_magentoMajorVersion;
145
    }
146
147
    /**
148
     * @return boolean
149
     */
150
    public function isMagerunStopFileFound()
151
    {
152
        return $this->_magerunStopFileFound;
153
    }
154
155
    /**
156
     * @return string
157
     */
158
    public function getMagerunStopFileFolder()
159
    {
160
        return $this->_magerunStopFileFolder;
161
    }
162
163
    /**
164
     * @param string $folder
165
     *
166
     * @return array
167
     */
168
    protected function splitPathFolders($folder)
169
    {
170
        $folders = array();
171
172
        $folderParts = explode(DIRECTORY_SEPARATOR, $folder);
173
        foreach ($folderParts as $key => $part) {
174
            $explodedFolder = implode(DIRECTORY_SEPARATOR, array_slice($folderParts, 0, $key + 1));
175
            if ($explodedFolder !== '') {
176
                $folders[] = $explodedFolder;
177
            }
178
        }
179
180
        return $folders;
181
    }
182
183
    /**
184
     * Check for modman file and .basedir
185
     *
186
     * @param array $folders
187
     *
188
     * @return array
189
     */
190
    protected function checkModman(array $folders)
191
    {
192
        foreach (array_reverse($folders) as $searchFolder) {
193 View Code Duplication
            if (!is_readable($searchFolder)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
194
                if (OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity()) {
195
                    $this->output->writeln(
196
                        '<debug>Folder <info>' . $searchFolder . '</info> is not readable. Skip.</debug>'
197
                    );
198
                }
199
                continue;
200
            }
201
202
            $finder = Finder::create();
203
            $finder
204
                ->files()
205
                ->ignoreUnreadableDirs(true)
206
                ->depth(0)
207
                ->followLinks()
208
                ->ignoreDotFiles(false)
209
                ->name('.basedir')
210
                ->in($searchFolder);
211
212
            $count = $finder->count();
213
            if ($count > 0) {
214
                $baseFolderContent = trim(file_get_contents($searchFolder . DIRECTORY_SEPARATOR . '.basedir'));
215
                if (OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity()) {
216
                    $this->output->writeln(
217
                        '<debug>Found modman .basedir file with content <info>' . $baseFolderContent . '</info></debug>'
218
                    );
219
                }
220
221
                if (!empty($baseFolderContent)) {
222
                    array_push(
223
                        $folders, $searchFolder . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $baseFolderContent
224
                    );
225
                }
226
            }
227
        }
228
229
        return $folders;
230
    }
231
232
    /**
233
     * Check for magerun stop-file
234
     *
235
     * @param array $folders
236
     *
237
     * @return array
238
     */
239
    protected function checkMagerunFile(array $folders)
240
    {
241
        foreach (array_reverse($folders) as $searchFolder) {
242 View Code Duplication
            if (!is_readable($searchFolder)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
243
                if (OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity()) {
244
                    $this->output->writeln(
245
                        sprintf('<debug>Folder <info>%s</info> is not readable. Skip.</debug>', $searchFolder)
246
                    );
247
                }
248
                continue;
249
            }
250
            $stopFile = '.' . pathinfo($this->_customConfigFilename, PATHINFO_FILENAME);
251
            $finder = Finder::create();
252
            $finder
253
                ->files()
254
                ->ignoreUnreadableDirs(true)
255
                ->depth(0)
256
                ->followLinks()
257
                ->ignoreDotFiles(false)
258
                ->name($stopFile)
259
                ->in($searchFolder);
260
261
            $count = $finder->count();
262
            if ($count > 0) {
263
                $this->_magerunStopFileFound = true;
264
                $this->_magerunStopFileFolder = $searchFolder;
265
                $magerunFilePath = $searchFolder . DIRECTORY_SEPARATOR . $stopFile;
266
                $magerunFileContent = trim(file_get_contents($magerunFilePath));
267
                if (OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity()) {
268
                    $message = sprintf(
269
                        '<debug>Found stopfile \'%s\' file with content <info>%s</info></debug>', $stopFile,
270
                        $magerunFileContent
271
                    );
272
                    $this->output->writeln($message);
273
                }
274
275
                array_push($folders, $searchFolder . DIRECTORY_SEPARATOR . $magerunFileContent);
276
            }
277
        }
278
279
        return $folders;
280
    }
281
282
    /**
283
     * @param string $searchFolder
284
     *
285
     * @return bool
286
     */
287
    protected function _search($searchFolder)
288
    {
289
        if (OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity()) {
290
            $this->output->writeln('<debug>Search for Magento in folder <info>' . $searchFolder . '</info></debug>');
291
        }
292
293
        if (!is_dir($searchFolder . '/app')) {
294
            return false;
295
        }
296
297
        $finder = Finder::create();
298
        $finder
299
            ->ignoreUnreadableDirs(true)
300
            ->depth(0)
301
            ->followLinks()
302
            ->name('Mage.php')
303
            ->name('bootstrap.php')
304
            ->name('autoload.php')
305
            ->in($searchFolder . '/app');
306
307
        if ($finder->count() > 0) {
308
            $files = iterator_to_array($finder, false);
309
            /* @var $file \SplFileInfo */
310
311
            $hasMageFile = false;
312
            foreach ($files as $file) {
313
                if ($file->getFilename() === 'Mage.php') {
314
                    $hasMageFile = true;
315
                }
316
            }
317
318
            $this->_magentoRootFolder = $searchFolder;
319
320
            // Magento 2 does not have a god class and thus if this file is not there it is version 2
321
            if ($hasMageFile === false) {
322
                $this->_magentoMajorVersion = Application::MAGENTO_MAJOR_VERSION_2;
323
                return true; // the rest of this does not matter since we are simply exiting with a notice
324
            }
325
326
            if (is_callable(array('\Mage', 'getEdition'))) {
327
                $this->_magentoEnterprise = (\Mage::getEdition() == 'Enterprise');
328
            } else {
329
                $this->_magentoEnterprise = is_dir($this->_magentoRootFolder . '/app/code/core/Enterprise') ||
330
                    is_dir($this->_magentoRootFolder . '/app/design/frontend/enterprise/default/layout');
331
            }
332
333
            if (OutputInterface::VERBOSITY_DEBUG <= $this->output->getVerbosity()) {
334
                $this->output->writeln(
335
                    '<debug>Found Magento in folder <info>' . $this->_magentoRootFolder . '</info></debug>'
336
                );
337
            }
338
339
            return true;
340
        }
341
342
        return false;
343
    }
344
}
345