FileCreate::modifyClassName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Luke\Migration\Config;
4
5
class FileCreate
6
{
7
    private $arrayFilterKeys = ['create_', '_table', '_table_'];
8
9
    public function createFile(string $fileName)
10
    {
11
        if (!file_exists(CONFIG_PATH)) {
12
            mkdir(CONFIG_PATH, 0777, true);
13
        }
14
15
        if (file_exists(CONFIG_PATH . "/" . $fileName . ".php")) {
16
            throw new \Exception("Arquivo ja existe");
17
        }
18
        $file = fopen(CONFIG_PATH . "/"  . $fileName . ".php", 'w+');
19
        if ($file) {
0 ignored issues
show
introduced by
$file is of type resource, thus it always evaluated to false.
Loading history...
20
            $text = $this->copyFile();
21
            $lines = $this->modifyClassName($text, $fileName);
22
            fwrite($file, $lines);
23
            fclose($file);
24
        }
25
    }
26
27
    private function copyFile(): string
28
    {
29
        return file_get_contents(__DIR__ . "/../stub/migration.stub");
30
    }
31
32
    private function modifyClassName(string $text, string $table): string
33
    {
34
        foreach ($this->arrayFilterKeys as $key) {
35
            $table = str_replace($key, '', $table);
36
        }
37
38
        return str_replace('{{ $table }}', $table, $text);
39
    }
40
}
41