Completed
Push — master ( ed7824...4c153c )
by Maarten
01:49 queued 10s
created

DeleteMigrationAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
9
class DeleteMigrationAction
10
{
11
    public $path;
12
13
    public function __construct()
14
    {
15
        $this->path = base_path('database/migrations/');
16
    }
17
18
    /**
19
     * @param $request
20
     * @return string
21
     */
22
    public function execute($request)
23
    {
24
        $migrations = scandir($this->path);
25
26
        foreach ($migrations as $migration) {
27
            if ($this->isRelatedMigration($migration, $request)) {
28
                File::delete("$this->path/$migration");
29
            }
30
        }
31
    }
32
33
    private function isRelatedMigration($migration, $request): bool
34
    {
35
        $content = file_get_contents("$this->path/$migration");
36
37
        if ($request instanceof DeleteRelationRequest) {
38
            return strpos($content, "laravel-schematics-{$request['table']}-relation") !== false;
39
        }
40
41
        $table = Str::plural(Str::snake(
42
            substr($request['name'], strrpos($request['name'], '\\') + 1)
43
        ));
44
45
        return strpos($content, "laravel-schematics-$table-model") !== false
46
            || strpos($content, "laravel-schematics-$table-relation") !== false;
47
    }
48
}
49