Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
53.12% |
17 / 32 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
RequiredActionService | |
53.12% |
17 / 32 |
|
66.67% |
4 / 6 |
42.37 | |
0.00% |
0 / 1 |
getRequiredActions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
attachAction | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
detachAction | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getRequiredAction | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
getActionToBePerformed | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 | |||
checkRequiredActions | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2015 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * |
21 | */ |
22 | |
23 | namespace oat\tao\model\requiredAction\implementation; |
24 | |
25 | use oat\oatbox\event\Event; |
26 | use oat\tao\model\requiredAction\RequiredActionServiceInterface; |
27 | use oat\tao\model\requiredAction\RequiredActionInterface; |
28 | use oat\oatbox\service\ConfigurableService; |
29 | use oat\oatbox\service\ServiceManager; |
30 | |
31 | /** |
32 | * Class RequiredActionService |
33 | * |
34 | * RequiredActionService is the service for work with required actions |
35 | * @see oat\tao\models\services\requiredAction\RequiredActionInterface |
36 | * |
37 | * @package oat\tao\models\services\requiredAction |
38 | * @author Aleh Hutnilau <hutnikau@1pt.com> |
39 | */ |
40 | class RequiredActionService extends ConfigurableService implements RequiredActionServiceInterface |
41 | { |
42 | /** |
43 | * Get list of required actions |
44 | * @return RequiredActionAbstract[] array of required action instances |
45 | */ |
46 | public function getRequiredActions() |
47 | { |
48 | $actions = $this->getOption(self::OPTION_REQUIRED_ACTIONS); |
49 | return $actions ? $actions : []; |
50 | } |
51 | |
52 | /** |
53 | * Attach new action |
54 | * @param RequiredActionInterface $action |
55 | */ |
56 | public function attachAction(RequiredActionInterface $action) |
57 | { |
58 | $actions = $this->getRequiredActions(); |
59 | $actions[] = $action; |
60 | $this->setOption(self::OPTION_REQUIRED_ACTIONS, $actions); |
61 | } |
62 | |
63 | /** |
64 | * Detach old action by name |
65 | * @param string $name name of action |
66 | */ |
67 | public function detachAction($name) |
68 | { |
69 | $actions = $this->getRequiredActions(); |
70 | |
71 | foreach ($actions as $key => $action) { |
72 | if ($action->getName() === $name) { |
73 | unset($actions[$key]); |
74 | } |
75 | } |
76 | |
77 | $this->setOption(self::OPTION_REQUIRED_ACTIONS, $actions); |
78 | } |
79 | |
80 | /** |
81 | * Get required action by name |
82 | * @param string $name name of action |
83 | * @return RequiredActionInterface array of required action instances |
84 | */ |
85 | public function getRequiredAction($name) |
86 | { |
87 | $result = null; |
88 | $actions = $this->getOption(self::OPTION_REQUIRED_ACTIONS); |
89 | foreach ($actions as $action) { |
90 | if ($action->getName() === $name) { |
91 | $result = $action; |
92 | break; |
93 | } |
94 | } |
95 | return $result; |
96 | } |
97 | |
98 | /** |
99 | * Get first action which should be executed (one of action's rules return true). |
100 | * @param string[] array of action names which should be checked. If array is empty all action will be checked. |
101 | * @param Event $contextEvent |
102 | * @return null|RequiredActionInterface |
103 | */ |
104 | public function getActionToBePerformed($names = [], Event $contextEvent = null) |
105 | { |
106 | $result = null; |
107 | if (empty($names)) { |
108 | $actionsToCheck = $this->getRequiredActions(); |
109 | } else { |
110 | $actionsToCheck = []; |
111 | foreach ($names as $name) { |
112 | $actionsToCheck[] = $this->getRequiredAction($name); |
113 | } |
114 | } |
115 | /** @var RequiredActionInterface $requiredAction */ |
116 | foreach ($actionsToCheck as $requiredAction) { |
117 | if ($requiredAction->mustBeExecuted($contextEvent)) { |
118 | $result = $requiredAction; |
119 | break; |
120 | } |
121 | } |
122 | return $result; |
123 | } |
124 | |
125 | /** |
126 | * Check if any action must be executed and execute first of them. |
127 | * @param Event $event |
128 | */ |
129 | public static function checkRequiredActions(Event $event = null) |
130 | { |
131 | /** @var RequiredActionService $service */ |
132 | $service = ServiceManager::getServiceManager()->get(self::CONFIG_ID); |
133 | $action = $service->getActionToBePerformed([], $event); |
134 | if ($action !== null) { |
135 | $action->execute([], $event); |
136 | } |
137 | } |
138 | } |