Passed
Push — master ( 1f7f34...948e88 )
by Thomas
04:12 queued 02:03
created

MigrationService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 4
dl 0
loc 58
ccs 0
cts 33
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A call() 0 9 2
A buildFileContent() 0 6 1
A replaceMigrationFields() 0 4 1
A generateMigrationFields() 0 19 5
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 call()
17
    {
18
        $this->generateFile();
19
        $this->addGeneratedFileToIdeStack();
20
21
        if ($this->command->option('migrate')) {
22
            $this->command->call('migrate');
23
        }
24
    }
25
26
    protected function buildFileContent()
27
    {
28
        $this->replaceClassName();
29
        $this->replaceTableName();
30
        $this->replaceMigrationFields();
31
    }
32
33
    /**
34
     * Replace migration fields in stub file.
35
     *
36
     * @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...
37
     */
38
    protected function replaceMigrationFields(): void
39
    {
40
        $this->fileContent = str_replace('__migration_fields__', $this->generateMigrationFields(), $this->fileContent);
41
    }
42
43
    /**
44
     * Generates the migration fields from schema
45
     *
46
     * @return string
47
     */
48
    protected function generateMigrationFields(): string
49
    {
50
        $migrationFields = '';
51
52
        foreach ($this->command->schema->getMigrationFields() as $migrationField) {
53
            $migrationFields .= '$table->' . $migrationField['type'] . '(\'' . $migrationField['name'] . '\')';
54
            unset($migrationField['type'], $migrationField['name']);
55
56
            if (!empty($migrationField)) {
57
                foreach ($migrationField as $key => $item) {
58
                    $migrationFields .= '->' . $key . '(' . ((!is_bool($item)) ? '\'' . $item . '\'' : '') . ')';
59
                }
60
            }
61
62
            $migrationFields .= ";" . PHP_EOL;
63
        }
64
65
        return $migrationFields;
66
    }
67
}
68