|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\core\action\base; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
7
|
|
|
use phootwork\json\Json; |
|
8
|
|
|
use keeko\core\model\Module; |
|
9
|
|
|
use keeko\core\model\ModuleQuery; |
|
10
|
|
|
use keeko\framework\exceptions\ValidationException; |
|
11
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Base methods for keeko\core\action\ModuleUpdateAction |
|
15
|
|
|
* |
|
16
|
|
|
* This code is automatically created. Modifications will probably be overwritten. |
|
17
|
|
|
* |
|
18
|
|
|
* @author gossi |
|
19
|
|
|
*/ |
|
20
|
|
|
trait ModuleUpdateActionTrait { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param OptionsResolver $resolver |
|
24
|
|
|
*/ |
|
25
|
|
|
public function configureParams(OptionsResolver $resolver) { |
|
26
|
|
|
$resolver->setRequired(['id']); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Automatically generated run method |
|
31
|
|
|
* |
|
32
|
|
|
* @param Request $request |
|
33
|
|
|
* @return Response |
|
34
|
|
|
*/ |
|
35
|
|
|
public function run(Request $request) { |
|
36
|
|
|
// read |
|
37
|
|
|
$id = $this->getParam('id'); |
|
|
|
|
|
|
38
|
|
|
$module = ModuleQuery::create()->findOneById($id); |
|
39
|
|
|
|
|
40
|
|
|
// check existence |
|
41
|
|
|
if ($module === null) { |
|
42
|
|
|
throw new ResourceNotFoundException('Module not found.'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// hydrate |
|
46
|
|
|
$data = Json::decode($request->getContent()); |
|
|
|
|
|
|
47
|
|
|
$serializer = Module::getSerializer(); |
|
48
|
|
|
$module = $serializer->hydrate($module, $data); |
|
49
|
|
|
|
|
50
|
|
|
// validate |
|
51
|
|
|
if (!$module->validate()) { |
|
52
|
|
|
throw new ValidationException($module->getValidationFailures()); |
|
53
|
|
|
} else { |
|
54
|
|
|
$module->save(); |
|
55
|
|
|
return $this->response->run($request, $module); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.