1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\action; |
3
|
|
|
|
4
|
|
|
use keeko\framework\foundation\AbstractAction; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
9
|
|
|
use Tobscure\JsonApi\Exception\InvalidParameterException; |
10
|
|
|
use keeko\core\model\GroupActionQuery; |
11
|
|
|
use keeko\core\model\ActionQuery; |
12
|
|
|
use keeko\core\model\GroupQuery; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
*/ |
16
|
|
|
class ActionGroupUpdateAction extends AbstractAction { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param OptionsResolver $resolver |
20
|
|
|
*/ |
21
|
|
|
public function configureParams(OptionsResolver $resolver) { |
22
|
|
|
$resolver->setRequired(['id']); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Automatically generated run method |
27
|
|
|
* |
28
|
|
|
* @param Request $request |
29
|
|
|
* @return Response |
30
|
|
|
*/ |
31
|
|
|
public function run(Request $request) { |
32
|
|
|
$body = $request->getContent(); |
33
|
|
|
if (!isset($body['data'])) { |
34
|
|
|
throw new InvalidParameterException(); |
35
|
|
|
} |
36
|
|
|
$data = $body['data']; |
37
|
|
|
|
38
|
|
|
$id = $this->getParam('id'); |
39
|
|
|
$action = ActionQuery::create()->findOneById($id); |
40
|
|
|
|
41
|
|
|
if ($action === null) { |
42
|
|
|
throw new ResourceNotFoundException('action with id ' . $id . ' does not exist'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// remove all relationships before |
46
|
|
|
GroupActionQuery::create()->filterByAction($action)->delete(); |
47
|
|
|
|
48
|
|
|
// add them |
49
|
|
|
foreach ($data as $entry) { |
50
|
|
|
if (!isset($entry['id'])) { |
51
|
|
|
throw new InvalidParameterException(); |
52
|
|
|
} |
53
|
|
|
$group = GroupQuery::create()->findOneById($entry['id']); |
54
|
|
|
$action->addGroup($group); |
55
|
|
|
$action->save(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// run response |
59
|
|
|
return $this->response->run($request, $action); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: