|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sfneal\CrudModelActions; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
use Illuminate\Http\Response; |
|
9
|
|
|
use Sfneal\Actions\Action; |
|
10
|
|
|
use Sfneal\CrudModelActions\Utils\HttpResponses; |
|
11
|
|
|
use Sfneal\CrudModelActions\Utils\ModelEvents; |
|
12
|
|
|
use Sfneal\CrudModelActions\Utils\ModelQueries; |
|
13
|
|
|
use Sfneal\CrudModelActions\Utils\ResponseMessages; |
|
14
|
|
|
|
|
15
|
|
|
abstract class CrudModelAction extends Action |
|
16
|
|
|
{ |
|
17
|
|
|
// todo: artisan command to create new CrudModelAction? (with model & event params) |
|
18
|
|
|
// todo: change model type hinting to Model |
|
19
|
|
|
|
|
20
|
|
|
use HttpResponses; |
|
21
|
|
|
use ModelEvents; |
|
22
|
|
|
use ModelQueries; |
|
23
|
|
|
use ResponseMessages; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var Request |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $request; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* BaseSaveModelAction constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param Request|null $request |
|
34
|
|
|
* @param int|EloquentModel|null $model |
|
35
|
|
|
* @param int|null $related_model_key |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(Request $request = null, $model = null, int $related_model_key = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->request = $request ?? request(); |
|
40
|
|
|
$this->model = $this->resolveModel($model); |
|
41
|
|
|
$this->related_model_key = $related_model_key; |
|
42
|
|
|
$this->setTrackingEventFromConfig(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create or update the Model. |
|
47
|
|
|
* |
|
48
|
|
|
* @return Response |
|
49
|
|
|
* |
|
50
|
|
|
* @throws Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
public function execute(): Response |
|
53
|
|
|
{ |
|
54
|
|
|
// Attempt to pass validation check & execute CRUD action on $model |
|
55
|
|
|
try { |
|
56
|
|
|
// Model passes validation checks |
|
57
|
|
|
if ( |
|
58
|
|
|
(method_exists($this, 'validate') && $this->validate()) |
|
59
|
|
|
|| ! method_exists($this, 'validate') |
|
60
|
|
|
) { |
|
61
|
|
|
// Save the model |
|
62
|
|
|
$this->model = $this->handle(); |
|
63
|
|
|
|
|
64
|
|
|
// Flash a success message |
|
65
|
|
|
session()->flash('success', $this->successMessage()); |
|
66
|
|
|
|
|
67
|
|
|
// Fire TrackActivity Event |
|
68
|
|
|
if (method_exists($this, 'fireEvent')) { |
|
69
|
|
|
$this->fireEvent(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Return success response |
|
73
|
|
|
return response($this->successResponse(), 200); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Validation failed - throw exception |
|
77
|
|
|
else { |
|
78
|
|
|
// todo: add exception logging? |
|
79
|
|
|
throw new Exception($this->failMessage()); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Validation checks failed or model CRUD action failed to execute |
|
84
|
|
|
catch (Exception $exception) { |
|
85
|
|
|
// Flash a success message |
|
86
|
|
|
session()->flash('fail', $this->failMessage()); |
|
87
|
|
|
|
|
88
|
|
|
// Return failure response if the interface is implemented |
|
89
|
|
|
if (method_exists($this, 'failResponse')) { |
|
90
|
|
|
return response($this->failResponse(), 500); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
// Throw exception |
|
94
|
|
|
throw $exception; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Save or update the Model. |
|
100
|
|
|
* |
|
101
|
|
|
* @return EloquentModel |
|
102
|
|
|
*/ |
|
103
|
|
|
abstract protected function handle(): EloquentModel; |
|
104
|
|
|
} |
|
105
|
|
|
|