MakeFile::replaceContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
namespace InnoFlash\LaraStart\Console\Commands\Helpers;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Filesystem\FileNotFoundException;
7
use Illuminate\Filesystem\Filesystem;
8
use Illuminate\Support\Str;
9
use InnoFlash\LaraStart\Helper;
10
11
abstract class MakeFile extends Command
12
{
13
    abstract public function getStub();
14
15
    abstract public function getFilename();
16
17
    abstract public function getPath();
18
19
    protected $filesystem;
20
21
    public function __construct(Filesystem $filesystem)
22
    {
23
        parent::__construct();
24
        $this->filesystem = $filesystem;
25
    }
26
27
    /**
28
     * Execute the console command.
29
     *
30
     * @return mixed
31
     */
32
    public function handle()
33
    {
34
        $this->makeFile();
35
    }
36
37
    protected function makeFile()
38
    {
39
        $this->makeDir();
40
        if (! $this->filesystem->isFile($this->getPath().'/'.$this->getFilename())) {
41
            $this->warn(Helper::getFileName($this->argument('name')).' created');
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type null and string[]; however, parameter $fullname of InnoFlash\LaraStart\Helper::getFileName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            $this->warn(Helper::getFileName(/** @scrutinizer ignore-type */ $this->argument('name')).' created');
Loading history...
42
43
            return $this->filesystem->put($this->getPath().'/'.$this->getFilename(), $this->getReplaceContent());
44
        } else {
45
            $this->warn(Helper::getFileName($this->argument('name')).' already exist');
46
        }
47
    }
48
49
    /**
50
     * @return bool
51
     */
52
    protected function makeDir()
53
    {
54
        if (! $this->filesystem->isDirectory($this->getPath())) {
55
            return $this->filesystem->makeDirectory($this->getPath(), 0755, true);
56
        }
57
    }
58
59
    protected function getContent()
60
    {
61
        try {
62
            return $this->filesystem->get($this->getStub());
63
        } catch (FileNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
64
        }
65
    }
66
67
    protected function getReplaceContent()
68
    {
69
        $content = $this->getContent();
70
        $content = str_replace(
71
            $this->stringsToReplace(),
72
            $this->replaceContent(),
73
            $content
74
        );
75
76
        return $content;
77
    }
78
79
    protected function stringsToReplace()
80
    {
81
        return [
82
            '$servicePackage',
83
            '$namespaceModelName',
84
            '$filename',
85
            'modelObject',
86
            'model_object',
87
            'ModelName',
88
        ];
89
    }
90
91
    protected function replaceContent()
92
    {
93
        return [
94
            Helper::getDirName($this->argument('name'), true),
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type null and string[]; however, parameter $fullname of InnoFlash\LaraStart\Helper::getDirName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
            Helper::getDirName(/** @scrutinizer ignore-type */ $this->argument('name'), true),
Loading history...
95
            Helper::getModelNamespace($this->option('model')),
96
            Helper::getFileName($this->argument('name')),
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') can also be of type null and string[]; however, parameter $fullname of InnoFlash\LaraStart\Helper::getFileName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
            Helper::getFileName(/** @scrutinizer ignore-type */ $this->argument('name')),
Loading history...
97
            Str::camel(Helper::getFileName($this->option('model'))),
98
            \str_replace('-', '_', Str::kebab(Helper::getFileName($this->option('model')))),
99
            Helper::getFileName($this->option('model')),
100
        ];
101
    }
102
}
103