1 | <?php |
||
12 | abstract class CRUDController extends JSONController |
||
13 | { |
||
14 | /** |
||
15 | * Make sure that the data of a form is valid, only called when creating a |
||
16 | * new object |
||
17 | * @param Form $form The submitted form |
||
18 | * @return void |
||
19 | */ |
||
20 | 1 | protected function validateNew($form) |
|
23 | |||
24 | /** |
||
25 | * Make sure that the data of a form is valid, only called when editing an |
||
26 | * existing object |
||
27 | * @param Form $form The submitted form |
||
28 | * @param PermissionModel $model The model being edited |
||
29 | * @return void |
||
30 | */ |
||
31 | protected function validateEdit($form, $model) |
||
32 | { |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Make sure that the data of a form is valid |
||
37 | * @param Form $form The submitted form |
||
38 | * @return void |
||
39 | */ |
||
40 | protected function validate($form) |
||
41 | { |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Delete a model |
||
46 | * @param PermissionModel $model The model we want to delete |
||
47 | * @param Player $me The user who wants to delete the model |
||
48 | * @param Closure|null $onSuccess Something to do when the model is deleted |
||
49 | * @throws ForbiddenException |
||
50 | * @return mixed The response to show to the user |
||
51 | */ |
||
52 | 1 | protected function delete(PermissionModel $model, Player $me, $onSuccess = null) |
|
53 | { |
||
54 | 1 | if ($model->isDeleted()) { |
|
55 | // We will have to hard delete the model |
||
56 | $hard = true; |
||
57 | $message = 'hardDelete'; |
||
58 | $action = 'Erase forever'; |
||
59 | } else { |
||
60 | 1 | $hard = false; |
|
61 | 1 | $message = 'softDelete'; |
|
62 | 1 | $action = 'Delete'; |
|
63 | } |
||
64 | |||
65 | 1 | if (!$this->canDelete($me, $model, $hard)) { |
|
66 | throw new ForbiddenException($this->getMessage($model, $message, 'forbidden')); |
||
67 | } |
||
68 | |||
69 | 1 | $successMessage = $this->getMessage($model, $message, 'success'); |
|
70 | 1 | $redirection = $this->redirectToList($model); |
|
71 | |||
72 | 1 | return $this->showConfirmationForm(function () use ($model, $hard, $redirection, $onSuccess) { |
|
73 | 1 | if ($hard) { |
|
74 | $model->wipe(); |
||
75 | } else { |
||
76 | 1 | $model->delete(); |
|
77 | } |
||
78 | |||
79 | 1 | if ($onSuccess) { |
|
80 | 1 | $response = $onSuccess(); |
|
81 | 1 | if ($response instanceof Response) { |
|
82 | return $response; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | 1 | return $redirection; |
|
87 | 1 | }, $this->getMessage($model, $message, 'confirm'), $successMessage, $action); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Create a model |
||
92 | * |
||
93 | * This method requires that you have implemented enter() and a form creator |
||
94 | * for the model |
||
95 | * |
||
96 | * @param Player $me The user who wants to create the model |
||
97 | * @param Closure|null $onSuccess The function to call on success |
||
98 | * @throws ForbiddenException |
||
99 | * @return mixed The response to show to the user |
||
100 | */ |
||
101 | 1 | protected function create(Player $me, $onSuccess = null) |
|
102 | { |
||
103 | 1 | if (!$this->canCreate($me)) { |
|
104 | 1 | throw new ForbiddenException($this->getMessage($this->getName(), 'create', 'forbidden')); |
|
105 | } |
||
106 | |||
107 | 1 | $creator = $this->getFormCreator(); |
|
108 | 1 | $form = $creator->create()->handleRequest($this->getRequest()); |
|
109 | |||
110 | 1 | if ($form->isSubmitted()) { |
|
111 | 1 | $this->validate($form); |
|
|
|||
112 | 1 | $this->validateNew($form); |
|
113 | 1 | if ($form->isValid()) { |
|
114 | 1 | $model = $creator->enter($form); |
|
115 | 1 | $this->getFlashBag()->add("success", |
|
116 | 1 | $this->getMessage($model, 'create', 'success')); |
|
117 | |||
118 | 1 | if ($onSuccess) { |
|
119 | 1 | $response = $onSuccess($model); |
|
120 | 1 | if ($response instanceof Response) { |
|
121 | return $response; |
||
122 | } |
||
123 | } |
||
124 | |||
125 | 1 | return $this->redirectTo($model); |
|
126 | } |
||
127 | } |
||
128 | |||
129 | 1 | return array("form" => $form->createView()); |
|
130 | } |
||
131 | |||
132 | /** |
||
133 | * Edit a model |
||
134 | * |
||
135 | * This method requires that you have implemented update() and a form creator |
||
136 | * for the model |
||
137 | * |
||
138 | * @param PermissionModel $model The model we want to edit |
||
139 | * @param Player $me The user who wants to edit the model |
||
140 | * @param string $type The name of the variable to pass to the view |
||
141 | * @throws ForbiddenException |
||
142 | * @return mixed The response to show to the user |
||
143 | */ |
||
144 | protected function edit(PermissionModel $model, Player $me, $type) |
||
145 | { |
||
146 | if (!$this->canEdit($me, $model)) { |
||
147 | throw new ForbiddenException($this->getMessage($model, 'edit', 'forbidden')); |
||
148 | } |
||
149 | |||
150 | $creator = $this->getFormCreator($model); |
||
151 | $form = $creator->create()->handleRequest($this->getRequest()); |
||
152 | |||
153 | if ($form->isSubmitted()) { |
||
154 | $this->validate($form); |
||
155 | $this->validateEdit($form, $model); |
||
156 | if ($form->isValid()) { |
||
157 | $creator->update($form, $model); |
||
158 | $this->getFlashBag()->add("success", |
||
159 | $this->getMessage($model, 'edit', 'success')); |
||
160 | |||
161 | return $this->redirectTo($model); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return array("form" => $form->createView(), $type => $model); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Find whether a player can delete a model |
||
170 | * |
||
171 | * @param Player $player The player who wants to delete the model |
||
172 | * @param PermissionModel $model The model that will be deleted |
||
173 | * @param bool $hard Whether to hard-delete the model instead of soft-deleting it |
||
174 | * @return bool |
||
175 | */ |
||
176 | 1 | protected function canDelete($player, $model, $hard = false) |
|
180 | |||
181 | /** |
||
182 | * Find whether a player can create a model |
||
183 | * |
||
184 | * @param Player $player The player who wants to create a model |
||
185 | * @return bool |
||
186 | */ |
||
187 | 1 | protected function canCreate($player) |
|
193 | |||
194 | /** |
||
195 | * Find whether a player can edit a model |
||
196 | * |
||
197 | * @param Player $player The player who wants to delete the model |
||
198 | * @param PermissionModel $model The model which will be edited |
||
199 | * @return bool |
||
200 | */ |
||
201 | protected function canEdit($player, $model) |
||
205 | |||
206 | /** |
||
207 | * Get a redirection response to a model |
||
208 | * |
||
209 | * Goes to a list of models of the same type if the provided model does not |
||
210 | * have a URL |
||
211 | * |
||
212 | * @param ModelInterface $model The model to redirect to |
||
213 | * @return Response |
||
214 | */ |
||
215 | 1 | protected function redirectTo($model) |
|
223 | |||
224 | /** |
||
225 | * Get a redirection response to a list of models |
||
226 | * |
||
227 | * @param ModelInterface $model The model to whose list we should redirect |
||
228 | * @return Response |
||
229 | */ |
||
230 | 1 | protected function redirectToList($model) |
|
237 | |||
238 | /** |
||
239 | * Dynamically get the form to show to the user |
||
240 | * |
||
241 | * @param \Model|null $model The model being edited, `null` if we're creating one |
||
242 | * @return ModelFormCreator |
||
243 | */ |
||
244 | 1 | private function getFormCreator($model = null) |
|
254 | |||
255 | /** |
||
256 | * Get a message to show to the user |
||
257 | * @todo Use the $escape parameter |
||
258 | * @param \ModelInterface|string $model The model (or type) to show a message for |
||
259 | * @param string $action The action that will be performed (softDelete, hardDelete, create or edit) |
||
260 | * @param string $status The message's status (confirm, error or success) |
||
261 | * @return string |
||
262 | */ |
||
263 | 1 | private function getMessage($model, $action, $status, $escape = true) |
|
289 | |||
290 | /** |
||
291 | * Get a list of messages to show to the user |
||
292 | * @param string $type The type of the model that the message refers to |
||
293 | * @param string $name The name of the model |
||
294 | * @return array |
||
295 | */ |
||
296 | 1 | protected function getMessages($type, $name = '') |
|
355 | } |
||
356 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.