FileCreate   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A copyFile() 0 3 1
A modifyClassName() 0 7 2
A createFile() 0 15 4
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