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\Action; |
9
|
|
|
use keeko\core\model\ActionQuery; |
10
|
|
|
use keeko\framework\exceptions\ValidationException; |
11
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Base methods for keeko\core\action\ActionUpdateAction |
15
|
|
|
* |
16
|
|
|
* This code is automatically created. Modifications will probably be overwritten. |
17
|
|
|
* |
18
|
|
|
* @author gossi |
19
|
|
|
*/ |
20
|
|
|
trait ActionUpdateActionTrait { |
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
|
|
|
$action = ActionQuery::create()->findOneById($id); |
39
|
|
|
|
40
|
|
|
// check existence |
41
|
|
|
if ($action === null) { |
42
|
|
|
throw new ResourceNotFoundException('Action not found.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// hydrate |
46
|
|
|
$data = Json::decode($request->getContent()); |
|
|
|
|
47
|
|
|
$serializer = Action::getSerializer(); |
48
|
|
|
$action = $serializer->hydrate($action, $data); |
49
|
|
|
|
50
|
|
|
// validate |
51
|
|
|
if (!$action->validate()) { |
52
|
|
|
throw new ValidationException($action->getValidationFailures()); |
53
|
|
|
} else { |
54
|
|
|
$action->save(); |
55
|
|
|
return $this->response->run($request, $action); |
|
|
|
|
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
Idable
provides a methodequalsId
that 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.