Completed
Push — master ( 31e550...936008 )
by Maarten
01:19
created

SchematicsController::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Http\Controllers;
4
5
use Illuminate\Contracts\View\Factory;
6
use Illuminate\Routing\Controller;
7
use Illuminate\View\View;
8
use Mtolhuys\LaravelSchematics\Services\ModelMapper;
9
use Mtolhuys\LaravelSchematics\Services\RelationMapper;
10
use ReflectionException;
11
use Illuminate\Support\Facades\Cache;
12
13
class SchematicsController extends Controller
14
{
15
    /**
16
     * @return Factory|View
17
     * @throws ReflectionException
18
     */
19
    public function index()
20
    {
21
        return view('schematics::index', $this->modelsWithRelations(
22
            ModelMapper::map()
23
        ));
24
    }
25
26
    public function clearCache()
27
    {
28
        Cache::forget('schematics');
29
30
        return response('cache cleared', 200);
31
    }
32
33
    /**
34
     * @param array $models
35
     * @return array
36
     * @throws ReflectionException
37
     */
38
    private function modelsWithRelations(array $models): array
39
    {
40
        if (Cache::has('schematics')) {
41
            return Cache::get('schematics');
42
        }
43
44
        $data = [
45
            'models' => $models,
46
            'relations' => RelationMapper::map($models),
47
        ];
48
49
        Cache::put('schematics', $data, 1440);
50
51
        return $data;
52
    }
53
}
54