1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace InnoFlash\LaraStart\Services; |
4
|
|
|
|
5
|
|
|
use InnoFlash\LaraStart\Traits\APIResponses; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
abstract class CRUDServices |
9
|
|
|
{ |
10
|
|
|
use APIResponses; |
11
|
|
|
|
12
|
|
|
protected bool $returnObject; |
13
|
|
|
|
14
|
|
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
$this->returnObject = config('larastart.return_object'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This sets the attributes to be removed from the given set for updating or creating. |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
|
abstract public function getUnsetFields(): array; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This get the model value or class of the model in the service. |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
abstract public function getModel(); |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* This gets the relationship of the given model to the parent. |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function getParentRelationship() |
37
|
|
|
{ |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Deletes the model from the database. |
42
|
|
|
* |
43
|
|
|
* @param string $message |
44
|
|
|
* @return |
45
|
|
|
*/ |
46
|
|
|
public function destroy(string $message = 'Deleted successful!') |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
$this->getModel()->delete(); |
50
|
|
|
|
51
|
|
|
if ($this->returnObject) { |
52
|
|
|
return ''; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->successResponse($message, [], 204); |
56
|
|
|
} catch (\Exception $e) { |
57
|
|
|
abort(500, $e->getMessage()); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Updates the model with the given filtered attributes. |
63
|
|
|
* |
64
|
|
|
* @param array $attributes |
65
|
|
|
* @param string $message |
66
|
|
|
* @param bool $returnObject |
67
|
|
|
* @return \Illuminate\Http\JsonResponse|mixed |
68
|
|
|
*/ |
69
|
|
|
public function update(array $attributes, string $message = 'Update successful!', bool $returnObject = false) |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
$this->getModel()->update($this->optimizeAttributes($attributes)); |
73
|
|
|
if ($returnObject || $this->returnObject) { |
74
|
|
|
return $this->getModel(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->successResponse($message); |
78
|
|
|
} catch (\Exception $e) { |
79
|
|
|
abort(500, $e->getMessage()); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Creates a new model with the given filtered attributes. |
85
|
|
|
* |
86
|
|
|
* @param array $attributes |
87
|
|
|
* @param string $message |
88
|
|
|
* @param bool $returnObject |
89
|
|
|
* @return \Illuminate\Http\JsonResponse |
90
|
|
|
*/ |
91
|
|
|
public function create(array $attributes, string $message = 'Created successfully!', bool $returnObject = false) |
92
|
|
|
{ |
93
|
|
|
try { |
94
|
|
|
$model = $this->getModel()->create($this->optimizeAttributes($attributes)); |
95
|
|
|
|
96
|
|
|
if ($returnObject || $this->returnObject) { |
97
|
|
|
return $model; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->successResponse($message, [], 201); |
101
|
|
|
} catch (\Exception $e) { |
102
|
|
|
abort(500, $e->getMessage()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Creates a new model from the given parent relationship. |
108
|
|
|
* |
109
|
|
|
* @param array $attributes |
110
|
|
|
* @param string $message |
111
|
|
|
* @param bool $returnObject |
112
|
|
|
* @return \Illuminate\Http\JsonResponse |
113
|
|
|
*/ |
114
|
|
|
public function createFromParent( |
115
|
|
|
array $attributes, |
116
|
|
|
string $message = 'Created successfully!', |
117
|
|
|
bool $returnObject = false |
118
|
|
|
) { |
119
|
|
|
$class = get_class($this->getModel()); |
120
|
|
|
$model = new $class($this->optimizeAttributes($attributes)); |
121
|
|
|
|
122
|
|
|
try { |
123
|
|
|
$this->getParent()->save($model); |
124
|
|
|
if ($returnObject || $this->returnObject) { |
125
|
|
|
return $model; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->successResponse($message, [], 201); |
129
|
|
|
} catch (\Exception $e) { |
130
|
|
|
abort(500, $e->getMessage()); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* This removes unwanted fields from the incoming create/update requests. |
136
|
|
|
* |
137
|
|
|
* @param array $attributes |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
protected function optimizeAttributes(array $attributes) |
141
|
|
|
{ |
142
|
|
|
foreach ($this->getUnsetFields() as $field) { |
143
|
|
|
unset($attributes[$field]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return $attributes; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Retrieves the parent to child relationship between this model and its parent. |
151
|
|
|
*/ |
152
|
|
|
private function getParent() |
153
|
|
|
{ |
154
|
|
|
if (\is_object($this->getParentRelationship())) { |
|
|
|
|
155
|
|
|
return $this->getParentRelationship(); |
156
|
|
|
} elseif (\is_array($this->getParentRelationship())) { |
|
|
|
|
157
|
|
|
$class = $this->getParentRelationship()['0']; |
158
|
|
|
$relationship = $this->getParentRelationship()['1']; |
159
|
|
|
|
160
|
|
|
if (count($this->getParentRelationship()) > 2) { |
161
|
|
|
$parent = $class::findOrFail(request($this->getParentRelationship()['2'])); |
162
|
|
|
} else { |
163
|
|
|
$_class = new $class(); |
164
|
|
|
$parent = $class::findOrFail(request($_class->getForeignKey())); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $parent->$relationship(); |
168
|
|
|
} else { |
169
|
|
|
throw new InvalidArgumentException('You have set an invalid parent for this model'); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.