Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
95.92% |
47 / 49 |
|
87.50% |
14 / 16 |
CRAP | |
0.00% |
0 / 1 |
| DynamicModule | |
95.92% |
47 / 49 |
|
87.50% |
14 / 16 |
31 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
7 | |||
| getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getModule | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getBundle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDescription | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getCategory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPosition | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isActive | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setActive | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTags | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| hasTag | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
1 | |||
| fromArray | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
| validateRequiredData | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
7 | |||
| 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) 2016-2017 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\modules; |
| 22 | |
| 23 | use JsonSerializable; |
| 24 | use common_exception_InconsistentData; |
| 25 | |
| 26 | /** |
| 27 | * A class that represents a frontend module. |
| 28 | * |
| 29 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 30 | * @author Jean-Sébastien Conan <jean-sebastien@taotesting.com> |
| 31 | */ |
| 32 | class DynamicModule implements JsonSerializable |
| 33 | { |
| 34 | /** |
| 35 | * @var string $id the module identifier |
| 36 | */ |
| 37 | private $id; |
| 38 | |
| 39 | /** |
| 40 | * @var string $module the AMD module path |
| 41 | */ |
| 42 | private $module; |
| 43 | |
| 44 | /** |
| 45 | * @var string|null $bundle the bundle the module belongs to |
| 46 | */ |
| 47 | private $bundle; |
| 48 | |
| 49 | /** |
| 50 | * @var string|int|null $position to sort modules together |
| 51 | */ |
| 52 | private $position; |
| 53 | |
| 54 | /** |
| 55 | * @var string $description describes what the modules is doing |
| 56 | */ |
| 57 | private $description = ''; |
| 58 | |
| 59 | /** |
| 60 | * @var string $name a human readable module name |
| 61 | */ |
| 62 | private $name = ''; |
| 63 | |
| 64 | /** |
| 65 | * @var boolean $active if the module is activated |
| 66 | */ |
| 67 | private $active = true; |
| 68 | |
| 69 | /** |
| 70 | * @var string $category the module belongs to a category, to group them |
| 71 | */ |
| 72 | private $category; |
| 73 | |
| 74 | /** |
| 75 | * @var string[] $tags tags to add labels to modules |
| 76 | */ |
| 77 | private $tags = []; |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * Creates a frontend module |
| 82 | * @param string $id the module identifier |
| 83 | * @param string $module the module AMD module |
| 84 | * @param string $category the category the module belongs to |
| 85 | * @param array $data optional other properties |
| 86 | * @throws common_exception_InconsistentData |
| 87 | */ |
| 88 | public function __construct($id, $module, $category, $data = []) |
| 89 | { |
| 90 | |
| 91 | self::validateRequiredData($id, $module, $category); |
| 92 | |
| 93 | $this->id = (string) $id; |
| 94 | $this->module = (string) $module; |
| 95 | $this->category = (string) $category; |
| 96 | |
| 97 | if (isset($data['bundle'])) { |
| 98 | $this->bundle = (string) $data['bundle']; |
| 99 | } |
| 100 | if (isset($data['position'])) { |
| 101 | $this->position = $data['position']; |
| 102 | } |
| 103 | if (isset($data['description'])) { |
| 104 | $this->description = (string) $data['description']; |
| 105 | } |
| 106 | if (isset($data['name'])) { |
| 107 | $this->name = (string) $data['name']; |
| 108 | } |
| 109 | if (isset($data['active'])) { |
| 110 | $this->active = (bool) $data['active']; |
| 111 | } |
| 112 | if (isset($data['tags'])) { |
| 113 | $this->tags = (array) $data['tags']; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | public function getId() |
| 118 | { |
| 119 | return $this->id; |
| 120 | } |
| 121 | |
| 122 | public function getModule() |
| 123 | { |
| 124 | return $this->module; |
| 125 | } |
| 126 | |
| 127 | public function getBundle() |
| 128 | { |
| 129 | return $this->bundle; |
| 130 | } |
| 131 | |
| 132 | public function getName() |
| 133 | { |
| 134 | return $this->name; |
| 135 | } |
| 136 | |
| 137 | public function getDescription() |
| 138 | { |
| 139 | return $this->description; |
| 140 | } |
| 141 | |
| 142 | public function getCategory() |
| 143 | { |
| 144 | return $this->category; |
| 145 | } |
| 146 | |
| 147 | public function getPosition() |
| 148 | { |
| 149 | return $this->position; |
| 150 | } |
| 151 | |
| 152 | public function isActive() |
| 153 | { |
| 154 | return $this->active; |
| 155 | } |
| 156 | |
| 157 | public function setActive($active) |
| 158 | { |
| 159 | $this->active = (bool) $active; |
| 160 | } |
| 161 | |
| 162 | public function getTags() |
| 163 | { |
| 164 | return $this->tags; |
| 165 | } |
| 166 | |
| 167 | public function hasTag($tag) |
| 168 | { |
| 169 | return in_array($this->tags, $tag); |
| 170 | } |
| 171 | |
| 172 | public function jsonSerialize(): array |
| 173 | { |
| 174 | return $this->toArray(); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Convenient method to convert the members to an assoc array |
| 179 | * @return array the data |
| 180 | */ |
| 181 | public function toArray() |
| 182 | { |
| 183 | return [ |
| 184 | 'id' => $this->id, |
| 185 | 'module' => $this->module, |
| 186 | 'bundle' => $this->bundle, |
| 187 | 'position' => $this->position, |
| 188 | 'name' => $this->name, |
| 189 | 'description' => $this->description, |
| 190 | 'category' => $this->category, |
| 191 | 'active' => $this->active, |
| 192 | 'tags' => $this->tags |
| 193 | ]; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Create a test module from an assoc array |
| 198 | * @param array $data |
| 199 | * @return DynamicModule the new instance |
| 200 | * @throws common_exception_InconsistentData |
| 201 | */ |
| 202 | public static function fromArray(array $data) |
| 203 | { |
| 204 | |
| 205 | if (!isset($data['id']) || !isset($data['module']) || !isset($data['category'])) { |
| 206 | throw new common_exception_InconsistentData('The module requires an id, a module and a category'); |
| 207 | } |
| 208 | return new static($data['id'], $data['module'], $data['category'], $data); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Validate required data to construct a module |
| 213 | * @param mixed $id |
| 214 | * @param mixed $module |
| 215 | * @param mixed $category |
| 216 | * @return boolean true |
| 217 | * @throws common_exception_InconsistentData |
| 218 | */ |
| 219 | private static function validateRequiredData($id, $module, $category) |
| 220 | { |
| 221 | |
| 222 | if (! is_string($id) || empty($id)) { |
| 223 | throw new common_exception_InconsistentData('The module needs an id'); |
| 224 | } |
| 225 | if (! is_string($module) || empty($module)) { |
| 226 | throw new common_exception_InconsistentData('The module needs a path'); |
| 227 | } |
| 228 | if (! is_string($category) || empty($category)) { |
| 229 | throw new common_exception_InconsistentData('The module needs a category'); |
| 230 | } |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | } |