MigrationService::getConsoleOutput()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
    {
18
        return 'Generated migration file: '.$this->command->naming[$this->key]->getRelativeFilePath();
19
    }
20
21
    public function call()
22
    {
23
        $this->generateFile();
24
        $this->addGeneratedFileToIdeStack();
25
26
        if ($this->command->option('migrate')) {
27
            $this->command->call('migrate');
28
        }
29
    }
30
31
    protected function buildFileContent()
32
    {
33
        $this->replaceClassName();
34
        $this->replaceTableName();
35
        $this->replaceMigrationFields();
36
    }
37
38
    /**
39
     * Replace migration fields in stub file.
40
     *
41
     * @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...
42
     */
43
    protected function replaceMigrationFields(): void
44
    {
45
        $this->fileContent = str_replace('__migration_fields__', $this->generateMigrationFields(), $this->fileContent);
46
    }
47
48
    /**
49
     * Generates the migration fields from schema
50
     *
51
     * @return string
52
     */
53
    protected function generateMigrationFields(): string
54
    {
55
        $migrationFields = '';
56
57
        foreach ($this->command->schema->getMigrationFields() as $migrationField) {
58
            $migrationFields .= '            $table->' . $migrationField['type'] . '(\'' . $migrationField['name'] . '\')';
59
            unset($migrationField['type'], $migrationField['name']);
60
61
            if (!empty($migrationField)) {
62
                foreach ($migrationField as $key => $item) {
63
                    $migrationFields .= '->' . $key . '(' . ((!is_bool($item)) ? '\'' . $item . '\'' : '') . ')';
64
                }
65
            }
66
67
            $migrationFields .= ";" . PHP_EOL;
68
        }
69
70
        return $migrationFields;
71
    }
72
}
73