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

DeleteMigrationAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 40
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 10 3
A isRelatedMigration() 0 15 3
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