Completed
Push — master ( 6ad859...3ea301 )
by Maarten
13s queued 10s
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
        return view('schematics::index', $this->schematics());
27
    }
28
29
    /**
30
     * @return ResponseFactory|\Illuminate\Http\Response|Response
31
     */
32
    public function clearCache()
33
    {
34
        Cache::forget('schematics');
35
        Cache::forget('schematics-exception');
36
37
        return response('Cache cleared', 200);
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    public function migrations(): array
44
    {
45
        $redundant = [];
46
        $ran = Migration::all();
47
        $created = glob(base_path('database/migrations') . '/*.php');
48
49
        foreach ($ran as $migration) {
50
            if (!in_array(base_path("database/migrations/$migration->migration.php"), $created, true)) {
51
                $redundant[] = $migration;
52
            }
53
        }
54
55
        return [
56
            'redundant' => count($redundant),
57
            'created' => count($created),
58
            'run' => count($ran),
59
        ];
60
    }
61
62
    /**
63
     * @return array
64
     * @throws ReflectionException
65
     */
66
    public function refresh(): array
67
    {
68
        return $this->schematics(false);
69
    }
70
71
    /**
72
     * @param bool $cache
73
     * @return array
74
     * @throws ReflectionException
75
     */
76
    public function schematics($cache = true): array
77
    {
78
        if ($cache && Cache::has('schematics')) {
79
            return Cache::get('schematics');
80
        }
81
82
        $models = ModelMapper::map();
83
84
        $data = [
85
            'models' => $models,
86
            'relations' => RelationMapper::map($models),
87
            'migrations' => $this->migrations(),
88
        ];
89
90
        Cache::put('schematics', $data, 1440);
91
92
        return $data;
93
    }
94
}
95