1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/mvc package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Mvc\Controller; |
11
|
|
|
|
12
|
|
|
use Slick\Common\Log; |
13
|
|
|
use Slick\Form\FormInterface; |
14
|
|
|
use Slick\Mvc\ControllerInterface; |
15
|
|
|
use Slick\Mvc\Exception\Service\InvalidFormDataException; |
16
|
|
|
use Slick\Mvc\Form\EntityForm; |
17
|
|
|
use Slick\Mvc\Service\Entity\EntityUpdateService; |
18
|
|
|
use Slick\Orm\Entity; |
19
|
|
|
use Slick\Orm\EntityInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Entity Create Methods |
23
|
|
|
* |
24
|
|
|
* @package Slick\Mvc\Controller |
25
|
|
|
* @author Filipe Silva <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
trait EntityCreateMethods |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Handle the add entity request |
32
|
|
|
*/ |
33
|
|
|
public function add() |
34
|
|
|
{ |
35
|
|
|
$form = $this->getForm(); |
36
|
|
|
$this->set(compact('form')); |
37
|
|
|
|
38
|
|
|
if (!$form->wasSubmitted()) { |
39
|
|
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
try { |
43
|
|
|
$this->getUpdateService() |
44
|
|
|
->setForm($form) |
|
|
|
|
45
|
|
|
->update(); |
46
|
|
|
; |
47
|
|
|
} catch (InvalidFormDataException $caught) { |
48
|
|
|
Log::logger()->addNotice($caught->getMessage(), $form->getData()); |
|
|
|
|
49
|
|
|
$this->addErrorMessage($this->getInvalidFormDataMessage()); |
50
|
|
|
return; |
51
|
|
|
} catch (\Exception $caught) { |
52
|
|
|
Log::logger()->addCritical( |
|
|
|
|
53
|
|
|
$caught->getMessage(), |
54
|
|
|
$form->getData() |
55
|
|
|
); |
56
|
|
|
$this->addErrorMessage($this->getGeneralErrorMessage($caught)); |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->addSuccessMessage( |
61
|
|
|
$this->getCreateSuccessMessage( |
62
|
|
|
$this->getUpdateService()->getEntity() |
63
|
|
|
) |
64
|
|
|
); |
65
|
|
|
$this->redirectFromCreated($this->getUpdateService()->getEntity()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the create successful entity message |
70
|
|
|
* |
71
|
|
|
* @param EntityInterface $entity |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
protected function getCreateSuccessMessage(EntityInterface $entity) |
76
|
|
|
{ |
77
|
|
|
$singleName = $this->getEntityNameSingular(); |
78
|
|
|
$message = "The {$singleName} '%s' was successfully created."; |
79
|
|
|
return sprintf($this->translate($message), $entity); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Redirect after successful entity creation |
84
|
|
|
* |
85
|
|
|
* @param EntityInterface $entity |
86
|
|
|
* |
87
|
|
|
* @return $this|ControllerInterface|static |
88
|
|
|
*/ |
89
|
|
|
protected function redirectFromCreated(EntityInterface $entity) |
90
|
|
|
{ |
91
|
|
|
return $this->redirect( |
92
|
|
|
$this->getBasePath().'/show/'.$entity->getId() |
|
|
|
|
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get update service |
98
|
|
|
* |
99
|
|
|
* @return EntityUpdateService |
100
|
|
|
*/ |
101
|
|
|
abstract public function getUpdateService(); |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return FormInterface|EntityForm |
105
|
|
|
*/ |
106
|
|
|
abstract function getForm(); |
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get invalid form data message |
110
|
|
|
* |
111
|
|
|
* @param \Exception $caught |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
abstract protected function getGeneralErrorMessage(\Exception $caught); |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get invalid form data message |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
abstract protected function getInvalidFormDataMessage(); |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sets a value to be used by render |
126
|
|
|
* |
127
|
|
|
* The key argument can be an associative array with values to be set |
128
|
|
|
* or a string naming the passed value. If an array is given then the |
129
|
|
|
* value will be ignored. |
130
|
|
|
* |
131
|
|
|
* Those values must be set in the request attributes so they can be used |
132
|
|
|
* latter by any other middle ware in the stack. |
133
|
|
|
* |
134
|
|
|
* @param string|array $key |
135
|
|
|
* @param mixed $value |
136
|
|
|
* |
137
|
|
|
* @return ControllerInterface |
138
|
|
|
*/ |
139
|
|
|
abstract public function set($key, $value = null); |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Redirects the flow to another route/path |
143
|
|
|
* |
144
|
|
|
* @param string $path the route or path to redirect to |
145
|
|
|
* |
146
|
|
|
* @return ControllerInterface|self|$this |
147
|
|
|
*/ |
148
|
|
|
abstract public function redirect($path); |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Add an error flash message |
152
|
|
|
* |
153
|
|
|
* @param string $message |
154
|
|
|
* @return self |
155
|
|
|
*/ |
156
|
|
|
abstract public function addErrorMessage($message); |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Add a success flash message |
160
|
|
|
* |
161
|
|
|
* @param string $message |
162
|
|
|
* @return self |
163
|
|
|
*/ |
164
|
|
|
abstract public function addSuccessMessage($message); |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the translation for the provided message |
168
|
|
|
* |
169
|
|
|
* @param string $message |
170
|
|
|
* @param string $domain |
171
|
|
|
* @param string $locale |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
abstract public function translate( |
176
|
|
|
$message, $domain = null, $locale = null |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get entity singular name used on controller actions |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
abstract protected function getEntityNameSingular(); |
185
|
|
|
} |
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.