Completed
Push — master ( d38b33...d045d3 )
by Vincenzo
02:36
created

GeneratorHelperCommand::checkDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
4
namespace App\Lib\Slime\Console;
5
6
7
use App\Lib\Slime\Console\Traits\StdInReader;
8
9
abstract class GeneratorHelperCommand extends SlimeCommand
10
{
11
    use StdInReader;
12
13
    public function run()
14
    {
15
        $fullFileName = $this->getFullFileName();
16
17
        if ($this->checkFile($fullFileName)) {
18
            return 0;
19
        }
20
21
        $result = file_put_contents(
22
            $fullFileName
23
            ,
24
            $this->getHead() . $this->getStub()
25
        );
26
        return $result !== false ? 0 : 1;
27
    }
28
29
    protected abstract function getFilePath();
30
31
    protected function getHead()
32
    {
33
        return "<?php" . PHP_EOL;
34
    }
35
36
    protected function getFileName()
37
    {
38
        return $this->getArg(0);
39
    }
40
41
    protected function getFileExtension()
42
    {
43
        return ".php";
44
    }
45
46
    protected function getStub()
47
    {
48
        return "";
49
    }
50
51
    private function getFullFileName()
52
    {
53
        $filePath = $this->getFilePath();
54
        if (!$this->checkDir($filePath)) {
55
            mkdir($filePath);
56
        }
57
58
        return $filePath . $this->getFileName() . $this->getFileExtension();
59
    }
60
61
    protected function checkDir($dirName)
62
    {
63
        return is_dir($dirName);
64
    }
65
66
    protected function checkFile($fullFileName)
67
    {
68
        if (file_exists($fullFileName)) {
69
            echo "File already exists, want to override? [y/n]: ";
70
            $resp = $this->readInput();
71
            if ($resp !== 'y') {
72
                return true;
73
            }
74
        }
75
76
        return false;
77
    }
78
79
}