Completed
Push — master ( c58d07...c19540 )
by Nicolas
04:33
created

Updater::copyScriptsToMainComposerJson()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Nwidart\Modules\Process;
4
5
use Nwidart\Modules\Module;
6
7
class Updater extends Runner
8
{
9
    /**
10
     * Update the dependencies for the specified module by given the module name.
11
     *
12
     * @param string $module
13
     */
14
    public function update($module)
15
    {
16
        $module = $this->module->findOrFail($module);
17
18
        chdir(base_path());
19
20
        $this->installRequires($module);
21
        $this->installDevRequires($module);
22
        $this->copyScriptsToMainComposerJson($module);
23
    }
24
25
    /**
26
     * @param Module $module
27
     */
28 View Code Duplication
    private function installRequires(Module $module)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
29
    {
30
        $packages = $module->getComposerAttr('require', []);
31
32
        $package = '';
33
        foreach ($packages as $name => $version) {
34
            $package .= "\"{$name}:{$version}\" ";
35
        }
36
        $this->run("composer require {$package}");
37
    }
38
39
    /**
40
     * @param Module $module
41
     */
42 View Code Duplication
    private function installDevRequires(Module $module)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
43
    {
44
        $devPackages = $module->getComposerAttr('require-dev', []);
45
46
        $package = '';
47
        foreach ($devPackages as $name => $version) {
48
            $package .= "\"{$name}:{$version}\" ";
49
        }
50
        $this->run("composer require --dev {$package}");
51
    }
52
53
    /**
54
     * @param Module $module
55
     */
56
    private function copyScriptsToMainComposerJson(Module $module)
57
    {
58
        $scripts = $module->getComposerAttr('scripts', []);
59
60
        $composer = json_decode(file_get_contents(base_path('composer.json')), true);
61
62
        foreach ($scripts as $key => $script) {
63
            if (array_key_exists($key, $composer['scripts'])) {
64
                $composer['scripts'][$key] = array_unique(array_merge($composer['scripts'][$key], $script));
65
                continue;
66
            }
67
            $composer['scripts'] = array_merge($composer['scripts'], [$key => $script]);
68
        }
69
70
        file_put_contents(base_path('composer.json'), json_encode($composer, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
71
    }
72
}
73