Passed
Pull Request — master (#36)
by Cristian
01:57
created

MigrationService::generateMigrationFields()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 9.3222
c 0
b 0
f 0
cc 5
nc 3
nop 0
crap 30
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Services;
4
5
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6
use Webfactor\Laravel\Generators\Contracts\ServiceAbstract;
7
use Webfactor\Laravel\Generators\Contracts\ServiceInterface;
8
use Webfactor\Laravel\Generators\Traits\CanGenerateFile;
9
10
class MigrationService extends ServiceAbstract implements ServiceInterface
11
{
12
    use CanGenerateFile;
13
14
    protected $key = 'migration';
15
16
    public function getConsoleOutput() {
17
        return 'Generated migration file: '.$this->command->naming[$this->key]->getRelativeFilePath();
18
    }
19
20
    public function call()
21
    {
22
        $this->generateFile();
23
        $this->addGeneratedFileToIdeStack();
24
25
        if ($this->command->option('migrate')) {
26
            $this->command->call('migrate');
27
        }
28
    }
29
30
    protected function buildFileContent()
31
    {
32
        $this->replaceClassName();
33
        $this->replaceTableName();
34
        $this->replaceMigrationFields();
35
    }
36
37
    /**
38
     * Replace migration fields in stub file.
39
     *
40
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
41
     */
42
    protected function replaceMigrationFields(): void
43
    {
44
        $this->fileContent = str_replace('__migration_fields__', $this->generateMigrationFields(), $this->fileContent);
45
    }
46
47
    /**
48
     * Generates the migration fields from schema
49
     *
50
     * @return string
51
     */
52
    protected function generateMigrationFields(): string
53
    {
54
        $migrationFields = '';
55
56
        foreach ($this->command->schema->getMigrationFields() as $migrationField) {
57
            $migrationFields .= '$table->' . $migrationField['type'] . '(\'' . $migrationField['name'] . '\')';
58
            unset($migrationField['type'], $migrationField['name']);
59
60
            if (!empty($migrationField)) {
61
                foreach ($migrationField as $key => $item) {
62
                    $migrationFields .= '->' . $key . '(' . ((!is_bool($item)) ? '\'' . $item . '\'' : '') . ')';
63
                }
64
            }
65
66
            $migrationFields .= ";" . PHP_EOL;
67
        }
68
69
        return $migrationFields;
70
    }
71
}
72