Test Setup Failed
Push — master ( 1750e6...e36cb8 )
by Php Easy Api
03:31
created

Generator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 39
c 2
b 1
f 0
dl 0
loc 163
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A createPath() 0 5 3
A replacementVariables() 0 8 1
A createFile() 0 14 4
A replaceFileContent() 0 11 2
A __construct() 0 9 1
A getStubFile() 0 9 2
A setStubPath() 0 3 1
A createMethod() 0 2 1
1
<?php
2
3
namespace Resta\Support;
4
5
use Resta\Exception\FileNotFoundException;
6
7
class Generator
8
{
9
    /**
10
     * @var null|string
11
     */
12
    protected $file;
13
14
    /**
15
     * @var null|object
16
     */
17
    protected $fileSystem;
18
19
    /**
20
     * @var null|string
21
     */
22
    protected $path;
23
24
    /**
25
     * @var null|string
26
     */
27
    protected $name;
28
29
    /**
30
     * @var null|string
31
     */
32
    protected $namespace;
33
34
    /**
35
     * @var string
36
     */
37
    protected $type = 'class';
38
39
    /**
40
     * @var null|string
41
     */
42
    protected $stubPath;
43
44
    /**
45
     * Generator constructor.
46
     * @param null $path
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $path is correct as it would always require null to be passed?
Loading history...
47
     * @param null $fileSystem
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $fileSystem is correct as it would always require null to be passed?
Loading history...
48
     */
49
    public function __construct($path=null,$fileSystem=null)
50
    {
51
        $this->path = $path;
52
53
        $this->fileSystem = $fileSystem;
54
55
        $this->namespace = Utils::getNamespace($this->path);
56
57
        $this->createPath();
58
    }
59
60
    /**
61
     * creates directory for generator
62
     *
63
     * @return mixed|void
64
     */
65
    public function createPath()
66
    {
67
        if(!file_exists($this->path)){
68
            if(!$this->fileSystem->makeDirectory($this->path)){
0 ignored issues
show
Bug introduced by
The method makeDirectory() does not exist on null. ( Ignorable by Annotation )

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

68
            if(!$this->fileSystem->/** @scrutinizer ignore-call */ makeDirectory($this->path)){

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
                throw new \Error($this->path.' makeDirectory fail');
70
            }
71
        }
72
    }
73
74
    /**
75
     * creates file for generator
76
     *
77
     * @param null $name
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $name is correct as it would always require null to be passed?
Loading history...
78
     * @param array $replacement
79
     *
80
     * @return Generator
81
     *
82
     * @throws FileNotFoundException
83
     */
84
    public function createFile($name=null,$replacement=array())
85
    {
86
        if(file_exists($this->path) && !is_null($name)){
0 ignored issues
show
introduced by
The condition is_null($name) is always true.
Loading history...
87
88
            $content = $this->fileSystem->get($this->getStubFile());
89
            $this->file = $this->path.''.DIRECTORY_SEPARATOR.''.ucfirst($name).'.php';
90
91
            if($this->fileSystem->put($this->file,$content)!==FALSE){
92
                $this->name = $name;
93
                $this->replaceFileContent($replacement,$this->file);
94
            }
95
        }
96
97
        return $this;
98
    }
99
100
    /**
101
     * creates method to file for generator
102
     *
103
     * @param array $params
104
     */
105
    public function createMethod($params=array())
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

105
    public function createMethod(/** @scrutinizer ignore-unused */ $params=array())

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
108
    }
109
110
    /**
111
     * get stub file for generator
112
     *
113
     * @return string
114
     */
115
    public function getStubFile()
116
    {
117
        $stubFile = $this->stubPath.''.DIRECTORY_SEPARATOR.''.$this->type.'.stub';
118
119
        if(!file_exists($stubFile)){
120
            throw new \Error($stubFile.' path is not available');
121
        }
122
123
        return $stubFile;
124
    }
125
126
    /**
127
     * replace with replacement variables content of the given file
128
     *
129
     * @param $replacement
130
     * @param $file
131
     * @return void
132
     */
133
    private function replaceFileContent($replacement,$file)
134
    {
135
        $replacementVariables = $this->replacementVariables($replacement);
136
        $content = $this->fileSystem->get($file);
137
138
        foreach ($replacementVariables as $key=>$replacementVariable){
139
            $search = '/__'.$key.'__/';
140
            $replace = $replacementVariable;
141
            $content = preg_replace($search,$replace,$content);
142
        }
143
        $this->fileSystem->replace($file,$content);
144
    }
145
146
    /**
147
     * get replacement variables
148
     *
149
     * @param array $replacement
150
     * @return array
151
     */
152
    private function replacementVariables($replacement=array())
153
    {
154
        $replacement['namespace'] = $this->namespace;
155
        $replacement['class'] = $this->name;
156
157
        return array_map(function($item){
158
            return ucfirst($item);
159
        },$replacement);
160
    }
161
162
    /**
163
     * set stub path
164
     *
165
     * @param $stubPath
166
     */
167
    public function setStubPath($stubPath)
168
    {
169
        $this->stubPath = $stubPath;
170
    }
171
172
}
173