1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Http\Controllers\Api; |
13
|
|
|
|
14
|
|
|
use Jitamin\Policy\ActionPolicy; |
15
|
|
|
use Jitamin\Policy\ProjectPolicy; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Action API controller. |
19
|
|
|
*/ |
20
|
|
|
class ActionController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Get available automatic actions. |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function getAvailableActions() |
28
|
|
|
{ |
29
|
|
|
return $this->actionManager->getAvailableActions(); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get the list of events and description that can be used from the user interface. |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
|
|
public function getAvailableActionEvents() |
38
|
|
|
{ |
39
|
|
|
return $this->eventManager->getAll(); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get list of compatible events for a given action. |
44
|
|
|
* |
45
|
|
|
* @param string $name |
|
|
|
|
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getCompatibleActionEvents($action_name) |
50
|
|
|
{ |
51
|
|
|
return $this->actionManager->getCompatibleEvents($action_name); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Remove an action. |
56
|
|
|
* |
57
|
|
|
* @param int $action_id |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function removeAction($action_id) |
62
|
|
|
{ |
63
|
|
|
ActionPolicy::getInstance($this->container)->check($this->getClassName(), 'removeAction', $action_id); |
64
|
|
|
|
65
|
|
|
return $this->actionModel->remove($action_id); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return actions and parameters for a given project. |
70
|
|
|
* |
71
|
|
|
* @param int $project_id |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function getActions($project_id) |
76
|
|
|
{ |
77
|
|
|
ProjectPolicy::getInstance($this->container)->check($this->getClassName(), 'getActions', $project_id); |
78
|
|
|
|
79
|
|
|
return $this->actionModel->getAllByProject($project_id); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create an action. |
84
|
|
|
* |
85
|
|
|
* @param int $project_id |
86
|
|
|
* @param string $event_name |
87
|
|
|
* @param string $action_name |
88
|
|
|
* @param array $params |
89
|
|
|
* |
90
|
|
|
* @return bool|int |
91
|
|
|
*/ |
92
|
|
|
public function createAction($project_id, $event_name, $action_name, array $params) |
93
|
|
|
{ |
94
|
|
|
ProjectPolicy::getInstance($this->container)->check($this->getClassName(), 'createAction', $project_id); |
95
|
|
|
$values = [ |
96
|
|
|
'project_id' => $project_id, |
97
|
|
|
'event_name' => $event_name, |
98
|
|
|
'action_name' => $action_name, |
99
|
|
|
'params' => $params, |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
list($valid) = $this->actionValidator->validateCreation($values); |
|
|
|
|
103
|
|
|
|
104
|
|
|
if (!$valid) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// Check if the action exists |
109
|
|
|
$actions = $this->actionManager->getAvailableActions(); |
|
|
|
|
110
|
|
|
|
111
|
|
|
if (!isset($actions[$action_name])) { |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
// Check the event |
116
|
|
|
$action = $this->actionManager->getAction($action_name); |
|
|
|
|
117
|
|
|
|
118
|
|
|
if (!in_array($event_name, $action->getEvents())) { |
119
|
|
|
return false; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$required_params = $action->getActionRequiredParameters(); |
123
|
|
|
|
124
|
|
|
// Check missing parameters |
125
|
|
|
foreach ($required_params as $param => $value) { |
126
|
|
|
if (!isset($params[$param])) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Check extra parameters |
132
|
|
|
foreach ($params as $param => $value) { |
133
|
|
|
if (!isset($required_params[$param])) { |
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->actionModel->create($values); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.