Completed
Push — master ( efdc7e...8e7c5b )
by Maarten
01:54 queued 12s
created

SchematicsController::migrations()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Http\Controllers;
4
5
use Mtolhuys\LaravelSchematics\Models\Migration;
6
use Mtolhuys\LaravelSchematics\Services\RelationMapper;
7
use Mtolhuys\LaravelSchematics\Services\ModelMapper;
8
use Illuminate\Contracts\Routing\ResponseFactory;
9
use Symfony\Component\HttpFoundation\Response;
10
use Illuminate\Contracts\View\Factory;
11
use Illuminate\Support\Facades\Schema;
12
use Illuminate\Support\Facades\Cache;
13
use Illuminate\Routing\Controller;
14
use Illuminate\Support\Facades\DB;
15
use Illuminate\View\View;
16
use ReflectionException;
17
18
class SchematicsController extends Controller
19
{
20
    /**
21
     * @return Factory|View
22
     * @throws ReflectionException
23
     */
24
    public function index()
25
    {
26
        $schema = $this->schematics(ModelMapper::map());
27
        $schema['migrations'] = $this->migrations();
28
29
        return view('schematics::index', $schema);
30
    }
31
32
    /**
33
     * @return ResponseFactory|\Illuminate\Http\Response|Response
34
     */
35
    public function clearCache()
36
    {
37
        Cache::forget('schematics');
38
39
        return response('Cache cleared', 200);
40
    }
41
42
    /**
43
     * @param $table
44
     * @return array
45
     */
46
    public function details($table)
47
    {
48
         $exists = Schema::hasTable($table);
49
50
         return $exists ? DB::select(DB::raw("SHOW COLUMNS FROM {$table}")) : [];
51
    }
52
53
    /**
54
     * @return array
55
     */
56
    public function migrations(): array
57
    {
58
        $redundant = [];
59
        $ran = Migration::all();
60
        $created = glob(base_path('database/migrations').'/*.php');
61
62
        foreach($ran as $migration) {
63
            if (! in_array(base_path("database/migrations/$migration->migration.php"), $created, true)) {
64
                $redundant[] = $migration;
65
            }
66
        }
67
68
        return [
69
            'redundant' => count($redundant),
70
            'created' => count($created),
71
            'run' => count($ran),
72
        ];
73
    }
74
75
    /**
76
     * @param array $models
77
     * @return array
78
     * @throws ReflectionException
79
     */
80
    public function schematics(array $models = []): array
81
    {
82
        if (Cache::has('schematics')) {
83
            return Cache::get('schematics');
84
        }
85
86
        if (empty($models)) {
87
            $models = ModelMapper::map();
88
        }
89
90
        $data = [
91
            'models' => $models,
92
            'relations' => RelationMapper::map($models)
93
        ];
94
95
        Cache::put('schematics', $data, 1440);
96
97
        return $data;
98
    }
99
}
100