Helper::initMagentoRootDir()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 8.125

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 8
cts 16
cp 0.5
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 8
nop 4
crap 8.125
1
<?php
2
/**
3
 *
4
 *
5
 *
6
 *
7
 */
8
9
namespace MagentoHackathon\Composer;
10
11
use MagentoHackathon\Composer\Magento\ProjectConfig;
12
13
class Helper
14
{
15
    /**
16
     * @var \SplFileInfo
17
     */
18
    protected $projectRoot;
19
20
    /**
21
     * @var ProjectConfig
22
     */
23
    protected $magentoProjectConfig;
24
25
    /**
26
     * @param \SplFileInfo $projectRoot
27
     */
28
    public function __construct(\SplFileInfo $projectRoot)
29
    {
30
        if (!file_exists($projectRoot->getPathname().'/composer.json')) {
31
            throw new \InvalidArgumentException('no composer.json found in project root');
32
        }
33
        $this->projectRoot = $projectRoot;
34
        
35
        $reader = new \Eloquent\Composer\Configuration\ConfigurationReader;
36
        $composerJsonObject = $reader->read($this->projectRoot.'/composer.json');
37
        $this->magentoProjectConfig = new ProjectConfig(
38
            (array)$composerJsonObject->extra(),
39
            (array)$composerJsonObject
40
        );
41
    }
42
    
43
    public function getVendorDirectory()
44
    {
45
        /*
46
        $reader = new \Eloquent\Composer\Configuration\ConfigurationReader;
47
        $composerJsonObject = $reader->read($this->projectRoot.'/composer.json');
48
        return $composerJsonObject->vendorName();
49
        */
50
        return new \SplFileInfo($this->projectRoot.'/vendor');
51
    }
52
    
53
    public function getInstalledPackages()
54
    {
55
        
56
        $installedJsonObject = json_decode(file_get_contents(
57
            $this->getVendorDirectory()->getPathname().'/composer/installed.json'
58
        ), true);
59
        return $installedJsonObject;
60
    }
61
62
    /**
63
     * @return ProjectConfig
64
     */
65
    public function getMagentoProjectConfig()
66
    {
67
        return $this->magentoProjectConfig;
68
    }
69
    
70
    public function getPackageByName($name)
71
    {
72
        $result = null;
73
        foreach ($this->getInstalledPackages() as $package) {
74
            if ($package['name'] == $name) {
75
                $result = $package;
76
                break;
77
            }
78
        }
79
        return $result;
80
    }
81
    
82 3
    public static function initMagentoRootDir(
83
        ProjectConfig $projectConfig,
84
        \Composer\IO\IOInterface $io,
85
        \Composer\Util\Filesystem $filesystem,
86
        $vendorDir
87
    ) {
88 3
        if (false === $projectConfig->hasMagentoRootDir()) {
89
            $projectConfig->setMagentoRootDir(
90
                $io->ask(
91
                    sprintf('please define your magento root dir [%s]', ProjectConfig::DEFAULT_MAGENTO_ROOT_DIR),
92
                    ProjectConfig::DEFAULT_MAGENTO_ROOT_DIR
93
                )
94
            );
95
        }
96
97 3
        $magentoRootDirPath = $projectConfig->getMagentoRootDir();
98 3
        $magentoRootDir = new \SplFileInfo($magentoRootDirPath);
99
100 3
        if (!is_dir($magentoRootDirPath)
101
            && $io->askConfirmation(
102 3
                'magento root dir "' . $magentoRootDirPath . '" missing! create now? [Y,n] '
103
            )
104
        ) {
105
            $filesystem->ensureDirectoryExists($magentoRootDir);
106
            $io->write('magento root dir "' . $magentoRootDirPath . '" created');
107
        }
108
109 3
        if (!is_dir($magentoRootDirPath)) {
110
            $dir = self::joinFilePath($vendorDir, $magentoRootDirPath);
0 ignored issues
show
Unused Code introduced by
$dir is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
        }
112 3
    }
113
114
    /**
115
     * join 2 paths
116
     *
117
     * @param        $path1
118
     * @param        $path2
119
     * @param        $delimiter
120
     * @param bool   $prependDelimiter
121
     * @param string $additionalPrefix
122
     *
123
     * @internal param $url1
124
     * @internal param $url2
125
     *
126
     * @return string
127
     */
128
    public static function joinPath($path1, $path2, $delimiter, $prependDelimiter = false, $additionalPrefix = '')
129
    {
130
        $prefix = $additionalPrefix . $prependDelimiter ? $delimiter : '';
131
132
        return $prefix . join(
133
            $delimiter,
134
            array(
135
                explode($path1, $delimiter),
136
                explode($path2, $delimiter)
137
            )
138
        );
139
    }
140
141
    /**
142
     * @param $path1
143
     * @param $path2
144
     *
145
     * @return string
146
     */
147
    public static function joinFilePath($path1, $path2)
148
    {
149
        return self::joinPath($path1, $path2, DIRECTORY_SEPARATOR, true);
150
    }
151
}
152