Completed
Push — master ( aca347...733456 )
by Nicolas
08:33
created

Stub   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 62.16%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 182
ccs 23
cts 37
cp 0.6216
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 4 1
A createFromPath() 0 8 1
A setPath() 0 6 1
A getPath() 0 4 1
A setBasePath() 0 4 1
A getBasePath() 0 4 1
A getContents() 0 10 2
A render() 0 4 1
A saveTo() 0 4 1
A replace() 0 6 1
A getReplaces() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace Nwidart\Modules\Support;
4
5
class Stub
6
{
7
    /**
8
     * The stub path.
9
     *
10
     * @var string
11
     */
12
    protected $path;
13
14
    /**
15
     * The base path of stub file.
16
     *
17
     * @var null|string
18
     */
19
    protected static $basePath = null;
20
21
    /**
22
     * The replacements array.
23
     *
24
     * @var array
25
     */
26
    protected $replaces = [];
27
28
    /**
29
     * The contructor.
30
     *
31
     * @param string $path
32
     * @param array  $replaces
33
     */
34 31
    public function __construct($path, array $replaces = [])
35
    {
36 31
        $this->path = $path;
37 31
        $this->replaces = $replaces;
38 31
    }
39
40
    /**
41
     * Create new self instance.
42
     *
43
     * @param string $path
44
     * @param array  $replaces
45
     *
46
     * @return self
47
     */
48 2
    public static function create($path, array $replaces = [])
49
    {
50 2
        return new static($path, $replaces);
51
    }
52
53
    /**
54
     * Create new self instance from full path.
55
     *
56
     * @param string $path
57
     * @param array  $replaces
58
     *
59
     * @return self
60
     */
61
    public static function createFromPath($path, array $replaces = [])
62
    {
63
        $stub = static::create($path, $replaces);
64
65
        $stub->setBasePath('');
66
67
        return $stub;
68
    }
69
70
    /**
71
     * Set stub path.
72
     *
73
     * @param string $path
74
     *
75
     * @return self
76
     */
77
    public function setPath($path)
78
    {
79
        $this->path = $path;
80
81
        return $this;
82
    }
83
84
    /**
85
     * Get stub path.
86
     *
87
     * @return string
88
     */
89 31
    public function getPath()
90
    {
91 31
        return static::getBasePath() . $this->path;
92
    }
93
94
    /**
95
     * Set base path.
96
     *
97
     * @param string $path
98
     */
99 73
    public static function setBasePath($path)
100
    {
101 73
        static::$basePath = $path;
102 73
    }
103
104
    /**
105
     * Get base path.
106
     *
107
     * @return string|null
108
     */
109 31
    public static function getBasePath()
110
    {
111 31
        return static::$basePath;
112
    }
113
114
    /**
115
     * Get stub contents.
116
     *
117
     * @return mixed|string
118
     */
119 31
    public function getContents()
120
    {
121 31
        $contents = file_get_contents($this->getPath());
122
123 31
        foreach ($this->replaces as $search => $replace) {
124 31
            $contents = str_replace('$' . strtoupper($search) . '$', $replace, $contents);
125 31
        }
126
127 31
        return $contents;
128
    }
129
130
    /**
131
     * Get stub contents.
132
     *
133
     * @return string
134
     */
135 31
    public function render()
136
    {
137 31
        return $this->getContents();
138
    }
139
140
    /**
141
     * Save stub to specific path.
142
     *
143
     * @param string $path
144
     * @param string $filename
145
     *
146
     * @return bool
147
     */
148
    public function saveTo($path, $filename)
149
    {
150
        return file_put_contents($path . '/' . $filename, $this->getContents());
151
    }
152
153
    /**
154
     * Set replacements array.
155
     *
156
     * @param array $replaces
157
     *
158
     * @return $this
159
     */
160
    public function replace(array $replaces = [])
161
    {
162
        $this->replaces = $replaces;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Get replacements.
169
     *
170
     * @return array
171
     */
172
    public function getReplaces()
173
    {
174
        return $this->replaces;
175
    }
176
177
    /**
178
     * Handle magic method __toString.
179
     *
180
     * @return string
181
     */
182 2
    public function __toString()
183
    {
184 2
        return $this->render();
185
    }
186
}
187