Completed
Push — master ( 863025...a6e3aa )
by Vincenzo
02:37
created

GeneratorHelperCommand::getFullFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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)) return 0;
18
19
        $result = file_put_contents(
20
            $fullFileName
21
            ,
22
            $this->getHead() . $this->getStub()
23
        );
24
        return $result !== false ? 0 : 1;
25
    }
26
27
    protected abstract function getFilePath();
28
29
    protected function getHead()
30
    {
31
        return "<?php" . PHP_EOL;
32
    }
33
34
    protected function getFileName()
35
    {
36
        return $this->getArg(0);
37
    }
38
39
    protected function getFileExtension()
40
    {
41
        return ".php";
42
    }
43
44
    protected function getStub()
45
    {
46
        return "";
47
    }
48
49
    private function getFullFileName()
50
    {
51
        return $this->getFilePath() . $this->getFileName() . $this->getFileExtension();
52
    }
53
54
    private function checkFile($fullFileName)
55
    {
56
        if (file_exists($fullFileName)) {
57
            echo "File already exists, want to override? [y/n]: ";
58
            $resp = $this->readInput();
59
            if ($resp !== 'y') {
60
                return true;
61
            }
62
        }
63
64
        return false;
65
    }
66
67
}