Completed
Push — develop ( 3c1b70...55bb1b )
by Tom
03:03
created

MagentoDetector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 8
dl 0
loc 72
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A detect() 0 29 5
A getDirectRootDirectory() 0 4 1
A resolveRootDirOption() 0 16 3
1
<?php
2
3
namespace N98\Magento\Application;
4
5
use N98\Util\Console\Helper\MagentoHelper;
6
use N98\Util\OperatingSystem;
7
use Symfony\Component\Console\Helper\HelperSet;
8
use Symfony\Component\Console\Input\ArgvInput;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\ConsoleOutput;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * Class MagentoDetector
15
 * @package N98\Magento\Application
16
 */
17
class MagentoDetector
18
{
19
    /**
20
     * @param \Symfony\Component\Console\Input\InputInterface|null $input
21
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output
22
     * @param \N98\Magento\Application\Config $config
23
     * @param \Symfony\Component\Console\Helper\HelperSet $helperSet
24
     * @param string $magentoRootDirectory
0 ignored issues
show
Documentation introduced by
Should the type for parameter $magentoRootDirectory not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
25
     * @return \N98\Magento\Application\DetectionResult
26
     */
27
    public function detect(
28
        InputInterface $input,
29
        OutputInterface $output,
30
        Config $config,
31
        HelperSet $helperSet,
32
        $magentoRootDirectory = null
33
    ) {
34
        $input = $input ?: new ArgvInput();
35
        $output = $output ?: new ConsoleOutput();
36
37
        $folder = OperatingSystem::getCwd();
38
        $subFolders = [];
39
40
        $directRootDirectory = $this->getDirectRootDirectory($input);
41
42
        if (is_string($directRootDirectory)) {
43
            $folder = $this->resolveRootDirOption($directRootDirectory);
44
        } elseif ($magentoRootDirectory !== null) {
45
            $subFolders = [$magentoRootDirectory];
46
        } else {
47
            $subFolders = $config->getDetectSubFolders();
48
        }
49
50
        $helperSet->set(new MagentoHelper($input, $output), 'magento');
51
        /* @var $magentoHelper MagentoHelper */
52
        $magentoHelper = $helperSet->get('magento');
53
54
        return new DetectionResult($magentoHelper, $folder, $subFolders); // @TODO must be refactored
55
    }
56
57
    /**
58
     * @param InputInterface $input
59
     * @return string
60
     */
61
    protected function getDirectRootDirectory(InputInterface $input)
62
    {
63
        return $input->getParameterOption('--root-dir');
64
    }
65
66
    /**
67
     * Set root dir (chdir()) of magento directory
68
     *
69
     * @param string $path to Magento directory
70
     * @return string
71
     */
72
    private function resolveRootDirOption($path)
73
    {
74
        $path = trim($path);
75
76
        if (strpos($path, '~') === 0) {
77
            $path = OperatingSystem::getHomeDir() . substr($path, 1);
78
        }
79
80
        $path = realpath($path);
81
82
        if (is_dir($path)) {
83
            chdir($path);
84
        }
85
86
        return $path;
87
    }
88
}
89