Passed
Push — master ( d88b3b...8c1d75 )
by Prateek
09:11 queued 10s
created

BaseGenerator::deleteFiles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Prateekkarki\Laragen\Generators;
4
use Prateekkarki\Laragen\Models\Module;
5
use Prateekkarki\Laragen\Models\FileSystem;
6
7
class BaseGenerator
8
{	
9
    protected $module;
10
    protected $fileSystem;
11
12
    public function __construct(Module $module)
13
    {
14
        $this->setModule($module);
15
        $this->fileSystem = new FileSystem();
16
    }
17
18
    public function getModule()
19
    {
20
        return $this->module;
21
    }
22
23
    public function setModule(Module $module)
24
    {
25
        $this->module = $module;
26
    }
27
28
    public function getStub($type)
29
    {
30
        return $this->sanitize(file_get_contents(realpath(__DIR__.DIRECTORY_SEPARATOR."..".DIRECTORY_SEPARATOR."resources".DIRECTORY_SEPARATOR."stubs").DIRECTORY_SEPARATOR.$type.".stub"));
31
    }
32
33
    public function sanitize($string)
34
    {
35
        return str_replace("\r", '', $string);
36
    }
37
38
    public function getPath($path)
39
    {
40
        $dir = base_path($path);
41
42
        if (!is_dir($dir)) {
43
            $this->fileSystem->mkdir($dir, 0755);
44
        }
45
46
        return $dir;
47
    }
48
    
49
    function deleteFiles($target) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
50
        if(is_dir($target)){
51
            $files = glob( $target . '*', GLOB_MARK );
52
53
            foreach( $files as $file ){
54
                deleteFiles( $file );      
0 ignored issues
show
Bug introduced by
The function deleteFiles was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

54
                /** @scrutinizer ignore-call */ 
55
                deleteFiles( $file );      
Loading history...
55
            }
56
57
            rmdir( $target );
58
        } elseif(is_file($target)) {
59
            unlink( $target );  
60
        }
61
    }
62
63
    public function moduleToModelName($moduleName)
64
    {
65
        return ucfirst(Str::camel(Str::singular($moduleName)));
0 ignored issues
show
Bug introduced by
The type Prateekkarki\Laragen\Generators\Str was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
    }
67
68
    public function initializeFile($fullFilePath, $stub, $initializeWithText = false) {
69
        if (file_exists($fullFilePath)) {
70
            $this->fileSystem->remove($fullFilePath);
71
        }
72
        $seederTemplate = ($initializeWithText === false) ? $this->buildTemplate($stub) : $initializeWithText;
73
74
        $cleanFilePath = $this->getCleanPath($fullFilePath);
75
        $this->fileSystem->dumpFile($cleanFilePath, $seederTemplate);
76
        return $fullFilePath;
77
    }
78
79
    public function getCleanPath($file) {
80
        return realpath(dirname($file)).DIRECTORY_SEPARATOR.basename($file);
81
    }
82
83
    public function initializeFiles($fileMaps = []) {
84
        foreach ($fileMaps as $file => $stub) {
85
            $this->initializeFile($file, $stub);
86
        }
87
    }
88
89
    public function buildTemplate($stub, $replacements = [])
90
    {
91
        return str_replace(array_keys($replacements), array_values($replacements), $this->getStub($stub));
92
    }
93
94
    public function updateFile($file, $replacements)
95
    {
96
        return str_replace(array_keys($replacements), array_values($replacements), file_get_contents($file));
97
    }
98
99
    public function insertIntoFile($file_path, $insert_marker, $text, $after = true) {
100
        $contents = str_replace("\r", '', file_get_contents($file_path));
101
        $new_contents = ($after) ? str_replace($insert_marker, $insert_marker.$text, $contents) : str_replace($insert_marker, $text.$insert_marker, $contents); 
102
        $this->fileSystem->dumpFile($file_path, $new_contents);
103
    }
104
105
106
    public function getTabs($number)
107
    {
108
        $schema = "";
109
        for ($i = 0; $i < $number; $i++) { 
110
            $schema .= "    ";
111
        }
112
        return $schema;
113
    }
114
}
115