Failed Conditions
Branch feature/streamline-console-com... (d385c8)
by Bas
11:19
created

MigrationCreator::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 5
dl 0
loc 24
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Migrations;
6
7
use Illuminate\Contracts\Filesystem\FileNotFoundException;
8
use Illuminate\Database\Migrations\MigrationCreator as IlluminateMigrationCreator;
9
10
class MigrationCreator extends IlluminateMigrationCreator
11
{
12
    /**
13
     * Get the path to the stubs.
14
     *
15
     * @return string
16
     */
17
    public function stubPath()
18
    {
19
        return __DIR__ . '/stubs';
20
    }
21
22
    /**
23
     * Get the migration stub file.
24
     *
25
     * @param string|null $table
26
     * @param bool $create
27
     * @param bool $edge
28
     * @return string
29
     *
30
     * @throws FileNotFoundException
31
     *
32
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
33
     */
34
    protected function getStub($table, $create, $edge = false)
35
    {
36
        if (is_null($table)) {
37
            $stub = $this->files->exists($customPath = $this->customStubPath . '/migration.stub')
38
                ? $customPath
39
                : $this->stubPath() . '/migration.stub';
40
            return $this->files->get($stub);
41
        } elseif ($edge) {
42
            $stub = $this->files->exists($customPath = $this->customStubPath . '/migration.create-edge.stub')
43
                ? $customPath
44
                : $this->stubPath() . '/migration.create-edge.stub';
45
46
47
            return $this->files->get($stub);
48
        } elseif ($create) {
49
            $stub = $this->files->exists($customPath = $this->customStubPath . '/migration.create.stub')
50
                ? $customPath
51
                : $this->stubPath() . '/migration.create.stub';
52
53
            return $this->files->get($stub);
54
        }
55
56
57
        $stub = $this->files->exists($customPath = $this->customStubPath . '/migration.update.stub')
58
            ? $customPath
59
            : $this->stubPath() . '/migration.update.stub';
60
61
        return $this->files->get($stub);
62
    }
63
64
    /**
65
     * Create a new migration at the given path.
66
     *
67
     * @param  string  $name
68
     * @param  string  $path
69
     * @param  string|null  $table
70
     * @param  bool  $create
71
     * @param  bool  $edge
72
     * @return string
73
     *
74
     * @throws \Exception
75
     */
76
    /**  @phpstan-ignore-next-line  @SuppressWarnings(PHPMD.BooleanArgumentFlag) */
77
    public function create($name, $path, $table = null, $create = false, $edge = false)
78
    {
79
        $this->ensureMigrationDoesntAlreadyExist($name, $path);
80
81
        // First we will get the stub file for the migration, which serves as a type
82
        // of template for the migration. Once we have those we will populate the
83
        // various place-holders, save the file, and run the post create event.
84
        $stub = $this->getStub($table, $create, $edge);
85
86
        $path = $this->getPath($name, $path);
87
88
        $this->files->ensureDirectoryExists(dirname($path));
89
90
        $this->files->put(
91
            $path,
92
            $this->populateStub($stub, $table)
93
        );
94
95
        // Next, we will fire any hooks that are supposed to fire after a migration is
96
        // created. Once that is done we'll be ready to return the full path to the
97
        // migration file so it can be used however it's needed by the developer.
98
        $this->firePostCreateHooks($table, $path);
99
100
        return $path;
101
    }
102
}
103