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

UnignoreComposerLock   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fire() 0 15 3
A gitignoreContainsComposerLock() 0 4 2
A getGitignoreLinesButComposerLock() 0 12 3
A writeNewGitignore() 0 10 2
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
}