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\ActionQuery; |
11
|
|
|
use keeko\core\model\GroupQuery; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
*/ |
15
|
|
|
class ActionGroupAddAction extends AbstractAction { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param OptionsResolver $resolver |
19
|
|
|
*/ |
20
|
|
|
public function configureParams(OptionsResolver $resolver) { |
21
|
|
|
$resolver->setRequired(['id']); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Automatically generated run method |
26
|
|
|
* |
27
|
|
|
* @param Request $request |
28
|
|
|
* @return Response |
29
|
|
|
*/ |
30
|
|
|
public function run(Request $request) { |
31
|
|
|
$body = $request->getContent(); |
32
|
|
|
if (!isset($body['data'])) { |
33
|
|
|
throw new InvalidParameterException(); |
34
|
|
|
} |
35
|
|
|
$data = $body['data']; |
36
|
|
|
|
37
|
|
|
$id = $this->getParam('id'); |
38
|
|
|
$action = ActionQuery::create()->findOneById($id); |
39
|
|
|
|
40
|
|
|
if ($action === null) { |
41
|
|
|
throw new ResourceNotFoundException('action with id ' . $id . ' does not exist'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($data as $entry) { |
45
|
|
|
if (!isset($entry['id'])) { |
46
|
|
|
throw new InvalidParameterException(); |
47
|
|
|
} |
48
|
|
|
$group = GroupQuery::create()->findOneById($entry['id']); |
49
|
|
|
$action->addGroup($group); |
50
|
|
|
$action->save(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// run response |
54
|
|
|
return $this->response->run($request, $action); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
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: