Completed
Push — master ( 847194...9ff36d )
by Maarten
01:44
created

SchematicsController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 6
dl 0
loc 48
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A clearCache() 0 6 1
A details() 0 6 2
A modelsWithRelations() 0 15 2
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Http\Controllers;
4
5
use Illuminate\Contracts\View\Factory;
6
use Illuminate\Routing\Controller;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Facades\Schema;
9
use Illuminate\View\View;
10
use Mtolhuys\LaravelSchematics\Services\ModelMapper;
11
use Mtolhuys\LaravelSchematics\Services\RelationMapper;
12
use ReflectionException;
13
use Illuminate\Support\Facades\Cache;
14
15
class SchematicsController extends Controller
16
{
17
    /**
18
     * @return Factory|View
19
     * @throws ReflectionException
20
     */
21
    public function index()
22
    {
23
        return view('schematics::index', $this->modelsWithRelations(
24
            ModelMapper::map()
25
        ));
26
    }
27
28
    public function clearCache()
29
    {
30
        Cache::forget('schematics');
31
32
        return response('cache cleared', 200);
33
    }
34
35
    public function details($table)
36
    {
37
         $exists = Schema::hasTable($table);
38
39
         return $exists ? DB::select(DB::raw("SHOW COLUMNS FROM {$table}")) : [];
40
    }
41
42
    /**
43
     * @param array $models
44
     * @return array
45
     * @throws ReflectionException
46
     */
47
    private function modelsWithRelations(array $models): array
48
    {
49
        if (Cache::has('schematics')) {
50
            return Cache::get('schematics');
51
        }
52
53
        $data = [
54
            'models' => $models,
55
            'relations' => RelationMapper::map($models),
56
        ];
57
58
        Cache::put('schematics', $data, 1440);
59
60
        return $data;
61
    }
62
}
63