Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
61.54% |
16 / 26 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| RequiredActionAbstract | |
61.54% |
16 / 26 |
|
75.00% |
6 / 8 |
20.19 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| execute | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| mustBeExecuted | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| completed | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getRules | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setRule | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toPhpCode | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| checkRules | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| 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; |
| 24 | |
| 25 | use Exception; |
| 26 | |
| 27 | /** |
| 28 | * Class RequiredAction |
| 29 | * |
| 30 | * RequiredAction is action which should be executed by user before performing any activities in the TAO |
| 31 | * |
| 32 | * @package oat\tao\model\requiredAction\implementation |
| 33 | * @author Aleh Hutnilau <hutnikau@1pt.com> |
| 34 | */ |
| 35 | abstract class RequiredActionAbstract implements RequiredActionInterface |
| 36 | { |
| 37 | /** |
| 38 | * @var string |
| 39 | */ |
| 40 | protected $name; |
| 41 | |
| 42 | /** |
| 43 | * @var RequiredActionRuleInterface[] |
| 44 | */ |
| 45 | protected $rules = []; |
| 46 | |
| 47 | /** |
| 48 | * RequiredAction constructor. |
| 49 | * @param string $name |
| 50 | * @param RequiredActionRuleInterface[] $rules |
| 51 | * @throws Exception |
| 52 | */ |
| 53 | public function __construct($name, array $rules = []) |
| 54 | { |
| 55 | $this->name = $name; |
| 56 | foreach ($rules as $rule) { |
| 57 | $this->setRule($rule); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Execute an action |
| 63 | * @param array $params |
| 64 | * @return mixed |
| 65 | */ |
| 66 | abstract public function execute(array $params = []); |
| 67 | |
| 68 | /** |
| 69 | * Whether the action must be executed. |
| 70 | * @param null $context |
| 71 | * @return boolean |
| 72 | */ |
| 73 | public function mustBeExecuted($context = null) |
| 74 | { |
| 75 | $result = $this->checkRules($context); |
| 76 | |
| 77 | return $result; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Mark action as completed. |
| 82 | * @param null $context |
| 83 | * @return mixed |
| 84 | */ |
| 85 | public function completed($context = null) |
| 86 | { |
| 87 | $rules = $this->getRules(); |
| 88 | foreach ($rules as $rule) { |
| 89 | $rule->completed($context); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get array of rules |
| 95 | * @return RequiredActionRuleInterface[] |
| 96 | */ |
| 97 | public function getRules() |
| 98 | { |
| 99 | return $this->rules; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Add rule to rules list |
| 104 | * @param RequiredActionRuleInterface $rule |
| 105 | * @return void |
| 106 | */ |
| 107 | public function setRule(RequiredActionRuleInterface $rule) |
| 108 | { |
| 109 | $rule->setRequiredAction($this); |
| 110 | $this->rules[] = $rule; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get action name |
| 115 | * @return string |
| 116 | */ |
| 117 | public function getName() |
| 118 | { |
| 119 | return $this->name; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @see \oat\oatbox\PhpSerializable::__toPhpCode() |
| 124 | */ |
| 125 | public function __toPhpCode() |
| 126 | { |
| 127 | $class = get_class($this); |
| 128 | $name = $this->name; |
| 129 | $rules = \common_Utils::toHumanReadablePhpString($this->getRules()); |
| 130 | return "new $class( |
| 131 | '$name', |
| 132 | $rules |
| 133 | )"; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Check rules whether action must be performed. |
| 138 | * If at least one rule returns true the action will be performed. |
| 139 | * If result is `true` then action must be performed. |
| 140 | * @param null $context |
| 141 | * @return bool |
| 142 | */ |
| 143 | protected function checkRules($context = null) |
| 144 | { |
| 145 | $rules = $this->getRules(); |
| 146 | $result = false; |
| 147 | |
| 148 | foreach ($rules as $rule) { |
| 149 | if ($rule->check($context)) { |
| 150 | $result = true; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return $result; |
| 156 | } |
| 157 | } |