DeleteMigrationAction::isRelatedMigration()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Actions\Migration;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Support\Facades\File;
7
use Mtolhuys\LaravelSchematics\Http\Requests\DeleteRelationRequest;
8
use Mtolhuys\LaravelSchematics\Actions\Migration\Traits\DeletesMigrations;
9
10
class DeleteMigrationAction
11
{
12
    use DeletesMigrations;
13
14
    /**
15
     * @param $request
16
     * @return string
17
     */
18
    public function execute($request)
19
    {
20
        $migrations = scandir($this->path);
21
22
        foreach ($migrations as $migration) {
23
            if ($this->isRelatedMigration($migration, $request)) {
24
                if ($this->autoMigrate) {
25
                    $this->down($migration);
26
                }
27
28
                File::delete("$this->path/$migration");
29
            }
30
        }
31
    }
32
33
    /**
34
     * @param $migration
35
     * @param $request
36
     * @return bool
37
     */
38
    private function isRelatedMigration($migration, $request): bool
39
    {
40
        $content = file_get_contents("$this->path/$migration");
41
42
        if ($request instanceof DeleteRelationRequest) {
43
            $this->autoMigrate = true;
44
45
            return strpos($content, "laravel-schematics-{$request['table']}-relation") !== false;
46
        }
47
48
        $table = Str::plural(Str::snake(
49
            substr($request['name'], strrpos($request['name'], '\\') + 1)
50
        ));
51
52
        return strpos($content, "laravel-schematics-$table-model") !== false
53
            || strpos($content, "laravel-schematics-$table-relation") !== false;
54
    }
55
}
56