MigrationCreator::getStub()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 19.125

Importance

Changes 0
Metric Value
cc 9
eloc 21
nc 9
nop 3
dl 0
loc 32
ccs 11
cts 22
cp 0.5
crap 19.125
rs 8.0555
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
use Illuminate\Filesystem\Filesystem;
10
use LaravelFreelancerNL\Aranguent\Console\Concerns\ArangoCommands;
11
12
class MigrationCreator extends IlluminateMigrationCreator
13
{
14
    use ArangoCommands;
15
16
    /**
17
     * The custom app stubs directory.
18
     *
19
     * @var string
20
     */
21
    protected $customStubPath;
22
23
    protected string $arangoStubPath = __DIR__ . '/stubs';
24
25
    /**
26
     * Create a new migration creator instance.
27
     *
28
     * @param  \Illuminate\Filesystem\Filesystem  $files
29
     * @param  string  $customStubPath
30
     * @return void
31
     */
32 498
    public function __construct(Filesystem $files, $customStubPath = __DIR__ . '/stubs')
33
    {
34 498
        parent::__construct($files, $customStubPath);
35
    }
36
37
38
    /**
39
     * Get the path to the stubs.
40
     *
41
     * @return string
42
     */
43
    public function stubPath()
44
    {
45
        if ($this->useFallback()) {
46
            return parent::stubPath();
47
        }
48
49
        return __DIR__ . '/stubs';
50
    }
51
52
    /**
53
     * Get the migration stub file.
54
     *
55
     * @param string|null $table
56
     * @param bool $create
57
     * @param bool $edge
58
     * @return string
59
     *
60
     * @throws FileNotFoundException
61
     *
62
     * @SuppressWarnings("PHPMD.BooleanArgumentFlag")
63
     */
64 4
    protected function getStub($table, $create, $edge = false)
65
    {
66 4
        if ($this->useFallback()) {
67
            return parent::getStub($table, $create);
68
        }
69
70 4
        if (is_null($table)) {
71
            $stub = $this->files->exists($customPath = $this->customStubPath . '/migration.stub')
72
                ? $customPath
73
                : $this->stubPath() . '/migration.stub';
74
            return $this->files->get($stub);
75 4
        } elseif ($edge) {
76 2
            $stub = $this->files->exists($customPath = $this->customStubPath . '/migration.create-edge.stub')
77 2
                ? $customPath
78
                : $this->stubPath() . '/migration.create-edge.stub';
79
80
81 2
            return $this->files->get($stub);
82 2
        } elseif ($create) {
83 2
            $stub = $this->files->exists($customPath = $this->customStubPath . '/migration.create.stub')
84 2
                ? $customPath
85
                : $this->stubPath() . '/migration.create.stub';
86
87 2
            return $this->files->get($stub);
88
        }
89
90
91
        $stub = $this->files->exists($customPath = $this->customStubPath . '/migration.update.stub')
92
            ? $customPath
93
            : $this->stubPath() . '/migration.update.stub';
94
95
        return $this->files->get($stub);
96
    }
97
98
    /**
99
     * Create a new migration at the given path.
100
     *
101
     * @param  string  $name
102
     * @param  string  $path
103
     * @param  string|null  $table
104
     * @param  bool  $create
105
     * @param  bool  $edge
106
     * @return string
107
     *
108
     * @throws \Exception
109
     */
110
    /**  @phpstan-ignore-next-line  @SuppressWarnings("PHPMD.BooleanArgumentFlag") */
111 4
    public function create($name, $path, $table = null, $create = false, $edge = false)
112
    {
113 4
        if ($this->useFallback()) {
114
            return parent::create($name, $path, $table = null, $create = false);
115
        }
116
117 4
        $this->ensureMigrationDoesntAlreadyExist($name, $path);
118
119
        // First we will get the stub file for the migration, which serves as a type
120
        // of template for the migration. Once we have those we will populate the
121
        // various place-holders, save the file, and run the post create event.
122 4
        $stub = $this->getStub($table, $create, $edge);
123
124 4
        $path = $this->getPath($name, $path);
125
126 4
        $this->files->ensureDirectoryExists(dirname($path));
127
128 4
        $this->files->put(
129 4
            $path,
130 4
            $this->populateStub($stub, $table),
131 4
        );
132
133
        // Next, we will fire any hooks that are supposed to fire after a migration is
134
        // created. Once that is done we'll be ready to return the full path to the
135
        // migration file so it can be used however it's needed by the developer.
136 4
        $this->firePostCreateHooks($table, $path);
137
138 4
        return $path;
139
    }
140
141 4
    public function getDefaultIlluminateCustomStubPath(): string
142
    {
143 4
        return base_path('vendor/laravel/framework/src/Illuminate/Database/Migrations/stubs');
144
    }
145
146
    public function setIlluminateCustomStubPath(): void
147
    {
148
        if ($this->customStubPath === $this->arangoStubPath) {
149
            $this->customStubPath = $this->getDefaultIlluminateCustomStubPath();
150
        }
151
    }
152
153 4
    public function setArangoCustomStubPath(): void
154
    {
155 4
        if ($this->customStubPath === $this->getDefaultIlluminateCustomStubPath()) {
156
            $this->customStubPath = __DIR__ . '/stubs';
157
        }
158
    }
159
}
160