Completed
Push — master ( cb1113...b09b3a )
by Maarten
16s queued 11s
created

ModelsController::getDeleteAction()   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 1
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Http\Controllers;
4
5
use Mtolhuys\LaravelSchematics\Actions\Model\EditModelAction;
6
use Mtolhuys\LaravelSchematics\Actions\Resource\CreateResourceControllerAction;
7
use Mtolhuys\LaravelSchematics\Actions\Migration\CreateColumnsMigrationAction;
8
use Mtolhuys\LaravelSchematics\Actions\Migration\CreateModelMigrationAction;
9
use Mtolhuys\LaravelSchematics\Actions\FormRequest\CreateFormRequestAction;
10
use Mtolhuys\LaravelSchematics\Actions\Migration\DeleteMigrationAction;
11
use Mtolhuys\LaravelSchematics\Http\Requests\EditModelRequest;
12
use Mtolhuys\LaravelSchematics\Http\Requests\CreateModelRequest;
13
use Mtolhuys\LaravelSchematics\Http\Requests\DeleteModelRequest;
14
use Mtolhuys\LaravelSchematics\Actions\Model\CreateModelAction;
15
use Mtolhuys\LaravelSchematics\Actions\Model\DeleteModelAction;
16
use Illuminate\Support\Facades\Cache;
17
use Illuminate\Routing\Controller;
18
use Illuminate\Http\Response;
19
use ReflectionException;
20
21
class ModelsController extends Controller
22
{
23
    /**
24
     * @param $request
25
     * @return Response
26
     */
27
    public function create(CreateModelRequest $request)
28
    {
29
        (new CreateModelAction())->execute($request);
30
        (new CreateModelMigrationAction())->execute($request);
31
32
        $this->createOptional($request);
33
34
        Cache::forget('schematics');
35
36
        return response('Model created', 200);
37
    }
38
39
    /**
40
     * @param DeleteModelRequest $request
41
     * @return Response
42
     * @throws ReflectionException
43
     */
44
    public function delete(DeleteModelRequest $request)
45
    {
46
        (new DeleteModelAction())->execute($request);
47
        (new DeleteMigrationAction())->execute($request);
48
49
        Cache::forget('schematics');
50
51
        return response('Model deleted', 200);
52
    }
53
54
    /**
55
     * @param EditModelRequest $request
56
     * @return Response
57
     * @throws ReflectionException
58
     */
59
    public function edit(EditModelRequest $request)
60
    {
61
        (new EditModelAction())->execute($request);
62
        (new CreateColumnsMigrationAction())->execute($request);
63
64
        Cache::forget('schematics');
65
66
        return response('Model changed', 200);
67
    }
68
69
    /**
70
     * @param $request
71
     */
72
    public function createOptional($request)
73
    {
74
        foreach ($request['actions'] as $option => $shouldUse) {
75
            if (json_decode($shouldUse, false)) {
76
                $this->getCreateAction($option)->execute($request);
77
            }
78
        }
79
    }
80
81
    /**
82
     * @param $option
83
     * @return mixed
84
     */
85
    private function getCreateAction($option)
86
    {
87
        return [
88
            'hasFormRequest' => new CreateFormRequestAction,
89
            'hasResource' => new CreateResourceControllerAction,
90
        ][$option];
91
    }
92
}
93