Completed
Pull Request — master (#79)
by Julien
06:09 queued 54s
created

UnignoreComposerLock::fire()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4286
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
/**
3
 * test
4
 *
5
 * @author Julien Tant - Craftyx <[email protected]>
6
 */
7
8
namespace Modules\Core\Console\Installers\Scripts;
9
10
use Illuminate\Console\Command;
11
use Modules\Core\Console\Installers\SetupScript;
12
13
class UnignoreComposerLock implements SetupScript
14
{
15
16
    /**
17
     * Fire the install script
18
     *
19
     * @param  Command $command
20
     * @return mixed
21
     */
22
    public function fire(Command $command)
23
    {
24
        $gitignorePath = base_path('.gitignore');
25
        $composerLock = 'composer.lock';
26
27
        if (!$this->gitignoreContainsComposerLock($gitignorePath, $composerLock)) {
28
            return;
29
        }
30
31
        $removeComposerLock = $command->confirm('Do you want to remove composer.lock from .gitignore ?', true);
32
        if ($removeComposerLock) {
33
            $out = $this->getGitignoreLinesButComposerLock($gitignorePath, $composerLock);
34
            $this->writeNewGitignore($gitignorePath, $out);
35
        }
36
    }
37
38
    /**
39
     * @param $gitignorePath
40
     * @return bool
41
     */
42
    private function gitignoreContainsComposerLock($gitignorePath, $composerLock)
43
    {
44
        return file_exists($gitignorePath) && strpos(file_get_contents($gitignorePath), $composerLock) !== false;
45
    }
46
47
    /**
48
     * @param $gitignorePath
49
     * @param $composerLock
50
     * @return array
51
     */
52
    private function getGitignoreLinesButComposerLock($gitignorePath, $composerLock)
53
    {
54
        $data = file($gitignorePath);
55
        $out = [];
56
        foreach ($data as $line) {
57
            if (trim($line) != $composerLock) {
58
                $out[] = $line;
59
            }
60
        }
61
62
        return $out;
63
    }
64
65
    /**
66
     * @param $gitignorePath
67
     * @param $out
68
     */
69
    private function writeNewGitignore($gitignorePath, $out)
70
    {
71
        $fp = fopen($gitignorePath, "w+");
72
        flock($fp, LOCK_EX);
73
        foreach ($out as $line) {
74
            fwrite($fp, $line);
75
        }
76
        flock($fp, LOCK_UN);
77
        fclose($fp);
78
    }
79
}