Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
79.05% |
117 / 148 |
|
85.71% |
12 / 14 |
CRAP | |
0.00% |
0 / 1 |
| common_configuration_ComponentFactory | |
79.05% |
117 / 148 |
|
85.71% |
12 / 14 |
59.99 | |
0.00% |
0 / 1 |
| buildPHPRuntime | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| buildPHPExtension | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| buildPHPINIValue | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| buildPHPDatabaseDriver | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| buildFileSystemComponent | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
1 | |||
| buildCustom | |
70.59% |
12 / 17 |
|
0.00% |
0 / 1 |
4.41 | |||
| buildMock | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| buildFromArray | |
71.74% |
66 / 92 |
|
0.00% |
0 / 1 |
43.45 | |||
| getFileSystemCount | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setFileSystemCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| incrementFileSystemCount | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getMockCount | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setMockCount | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| incrementMockCount | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * Short description of class common_configuration_ComponentFactory |
| 29 | * |
| 30 | * @access public |
| 31 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 32 | * @package generis |
| 33 | |
| 34 | */ |
| 35 | class common_configuration_ComponentFactory |
| 36 | { |
| 37 | // --- ASSOCIATIONS --- |
| 38 | |
| 39 | |
| 40 | // --- ATTRIBUTES --- |
| 41 | |
| 42 | /** |
| 43 | * Short description of attribute fileSystemCount |
| 44 | * |
| 45 | * @access private |
| 46 | * @var int |
| 47 | */ |
| 48 | private static $fileSystemCount = 0; |
| 49 | |
| 50 | /** |
| 51 | * Short description of attribute mockCount |
| 52 | * |
| 53 | * @access private |
| 54 | * @var int |
| 55 | */ |
| 56 | private static $mockCount = 0; |
| 57 | |
| 58 | // --- OPERATIONS --- |
| 59 | |
| 60 | /** |
| 61 | * Short description of method buildPHPRuntime |
| 62 | * |
| 63 | * @access public |
| 64 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 65 | * @param string min |
| 66 | * @param string max |
| 67 | * @param boolean optional |
| 68 | * @return common_configuration_PHPRuntime |
| 69 | */ |
| 70 | public static function buildPHPRuntime($min, $max = null, $optional = false) |
| 71 | { |
| 72 | $returnValue = null; |
| 73 | |
| 74 | |
| 75 | $returnValue = new common_configuration_PHPRuntime($min, $max, $optional); |
| 76 | |
| 77 | |
| 78 | return $returnValue; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Short description of method buildPHPExtension |
| 83 | * |
| 84 | * @access public |
| 85 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 86 | * @param string name |
| 87 | * @param string min |
| 88 | * @param string max |
| 89 | * @param boolean optional |
| 90 | * @return common_configuration_PHPExtension |
| 91 | */ |
| 92 | public static function buildPHPExtension($name, $min = null, $max = null, $optional = false) |
| 93 | { |
| 94 | $returnValue = null; |
| 95 | |
| 96 | |
| 97 | $returnValue = new common_configuration_PHPExtension($min, $max, $name, $optional); |
| 98 | |
| 99 | |
| 100 | return $returnValue; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Short description of method buildPHPINIValue |
| 105 | * |
| 106 | * @access public |
| 107 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 108 | * @param string name |
| 109 | * @param string expectedValue |
| 110 | * @param boolean optional |
| 111 | * @return common_configuration_PHPINIValue |
| 112 | */ |
| 113 | public static function buildPHPINIValue($name, $expectedValue, $optional = false) |
| 114 | { |
| 115 | $returnValue = null; |
| 116 | |
| 117 | |
| 118 | $returnValue = new common_configuration_PHPINIValue($expectedValue, $name, $optional); |
| 119 | |
| 120 | |
| 121 | return $returnValue; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Short description of method buildPHPDatabaseDriver |
| 126 | * |
| 127 | * @access public |
| 128 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 129 | * @param string name |
| 130 | * @param boolean optional |
| 131 | * @return common_configuration_PHPDatabaseDriver |
| 132 | */ |
| 133 | public static function buildPHPDatabaseDriver($name, $optional = false) |
| 134 | { |
| 135 | $returnValue = null; |
| 136 | |
| 137 | |
| 138 | $returnValue = new common_configuration_PHPDatabaseDriver(null, null, $name, $optional); |
| 139 | |
| 140 | |
| 141 | return $returnValue; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Short description of method buildFileSystemComponent |
| 146 | * |
| 147 | * @access public |
| 148 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 149 | * @param string location |
| 150 | * @param string expectedRights |
| 151 | * @param boolean optional |
| 152 | * @param boolean recursive |
| 153 | * @param boolean mustCheckIfEmpty |
| 154 | * @throws common_configuration_MalformedRightsException |
| 155 | * @return common_configuration_FileSystemComponent |
| 156 | */ |
| 157 | public static function buildFileSystemComponent( |
| 158 | $location, |
| 159 | $expectedRights, |
| 160 | $optional = false, |
| 161 | $recursive = false, |
| 162 | $mustCheckIfEmpty = false |
| 163 | ) { |
| 164 | $returnValue = new common_configuration_FileSystemComponent( |
| 165 | $location, |
| 166 | $expectedRights, |
| 167 | $optional, |
| 168 | $recursive, |
| 169 | $mustCheckIfEmpty |
| 170 | ); |
| 171 | |
| 172 | self::incrementFileSystemCount(); |
| 173 | |
| 174 | $returnValue->setName('FileSystemComponentCheck_' . self::getFileSystemCount()); |
| 175 | |
| 176 | return $returnValue; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Short description of method buildCustom |
| 181 | * |
| 182 | * @access public |
| 183 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 184 | * @param string name |
| 185 | * @param string extension |
| 186 | * @param boolean optional |
| 187 | * @return common_configuration_Component |
| 188 | */ |
| 189 | public static function buildCustom($name, $extension, $optional = false) |
| 190 | { |
| 191 | $returnValue = null; |
| 192 | |
| 193 | |
| 194 | // Camelize the name to find it in the checks folder. |
| 195 | $name = explode('_', $name); |
| 196 | for ($i = 0; $i < count($name); $i++) { |
| 197 | $name[$i] = ucfirst($name[$i]); |
| 198 | } |
| 199 | $name = implode('', $name); |
| 200 | $checkClassName = "${extension}_install_checks_${name}"; |
| 201 | |
| 202 | // Instanciate the Component. |
| 203 | try { |
| 204 | $checkClass = new ReflectionClass($checkClassName); |
| 205 | $returnValue = $checkClass->newInstanceArgs(["custom_${extension}_${name}", $optional]); |
| 206 | } catch (LogicException $e) { |
| 207 | $msg = "Cannot instantiate custom check '${name}' for extension '${extension}': "; |
| 208 | $msg .= $e->getMessage(); |
| 209 | throw new common_configuration_ComponentFactoryException($msg); |
| 210 | } catch (ReflectionException $e) { |
| 211 | $msg = "Cannot instantiate custom check '${name}' for extension '${extension}': "; |
| 212 | $msg .= $e->getMessage(); |
| 213 | throw new common_configuration_ComponentFactoryException($msg); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | return $returnValue; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Short description of method buildMock |
| 222 | * |
| 223 | * @access public |
| 224 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 225 | * @param int expectedStatus |
| 226 | * @param boolean optional |
| 227 | * @return common_configuration_Mock |
| 228 | */ |
| 229 | public static function buildMock($expectedStatus, $optional = false) |
| 230 | { |
| 231 | $returnValue = null; |
| 232 | |
| 233 | |
| 234 | self::incrementMockCount(); |
| 235 | $returnValue = new common_configuration_Mock($expectedStatus, 'MockComponentCheck_' . self::getMockCount()); |
| 236 | $returnValue->setOptional($optional); |
| 237 | |
| 238 | |
| 239 | return $returnValue; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Short description of method buildFromArray |
| 244 | * |
| 245 | * @access public |
| 246 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 247 | * @param array array |
| 248 | * @return common_configuration_Component |
| 249 | */ |
| 250 | public static function buildFromArray($array) |
| 251 | { |
| 252 | $returnValue = null; |
| 253 | |
| 254 | |
| 255 | if (!empty($array)) { |
| 256 | if (!empty($array['type'])) { |
| 257 | $acceptedTypes = [ |
| 258 | 'PHPRuntime', |
| 259 | 'PHPINIValue', |
| 260 | 'PHPExtension', |
| 261 | 'PHPDatabaseDriver', |
| 262 | 'FileSystemComponent', |
| 263 | 'Custom', |
| 264 | 'Mock' |
| 265 | ]; |
| 266 | $cleanType = preg_replace('/^Check/i', '', $array['type']); |
| 267 | if (in_array($cleanType, $acceptedTypes)) { |
| 268 | if (!empty($array['value'])) { |
| 269 | $values = $array['value']; |
| 270 | |
| 271 | // Optional parameter is always used. |
| 272 | $optional = false; |
| 273 | if (!empty($values['optional'])) { |
| 274 | $optional = $values['optional']; |
| 275 | } |
| 276 | |
| 277 | switch ($cleanType) { |
| 278 | case 'PHPRuntime': |
| 279 | $max = null; |
| 280 | if (!empty($values['max'])) { |
| 281 | $max = $values['max']; |
| 282 | } |
| 283 | |
| 284 | if (empty($values['min'])) { |
| 285 | $msg = "Mandatory attribute 'min' is missing."; |
| 286 | throw new common_configuration_ComponentFactoryException($msg); |
| 287 | } |
| 288 | |
| 289 | $returnValue = self::buildPHPRuntime($values['min'], $max, $optional); |
| 290 | break; |
| 291 | |
| 292 | case 'PHPINIValue': |
| 293 | if (empty($values['name'])) { |
| 294 | $msg = "Mandatory attribute 'name' is missing."; |
| 295 | throw new common_configuration_ComponentFactoryException($msg); |
| 296 | } |
| 297 | |
| 298 | if (empty($values['value']) && $values['value'] !== '0') { |
| 299 | $msg = "Mandatory attribute 'value' is missing."; |
| 300 | throw new common_configuration_ComponentFactoryException($msg); |
| 301 | } |
| 302 | |
| 303 | $returnValue = self::buildPHPINIValue($values['name'], $values['value'], $optional); |
| 304 | break; |
| 305 | |
| 306 | case 'PHPExtension': |
| 307 | if (empty($values['name'])) { |
| 308 | $msg = "Mandatory attribute 'name' is missing."; |
| 309 | throw new common_configuration_ComponentFactoryException($msg); |
| 310 | } |
| 311 | |
| 312 | $min = null; |
| 313 | if (!empty($values['min'])) { |
| 314 | $min = $values['min']; |
| 315 | } |
| 316 | |
| 317 | $max = null; |
| 318 | if (!empty($values['max'])) { |
| 319 | $max = $values['max']; |
| 320 | } |
| 321 | |
| 322 | $returnValue = self::buildPHPExtension($values['name'], $min, $max, $optional); |
| 323 | break; |
| 324 | |
| 325 | case 'PHPDatabaseDriver': |
| 326 | if (empty($values['name'])) { |
| 327 | $msg = "Mandatory attribute 'name' is missing."; |
| 328 | throw new common_configuration_ComponentFactoryException($msg); |
| 329 | } |
| 330 | |
| 331 | $returnValue = self::buildPHPDatabaseDriver($values['name'], $optional); |
| 332 | break; |
| 333 | |
| 334 | case 'FileSystemComponent': |
| 335 | if (empty($values['location'])) { |
| 336 | $msg = "Mandatory attribute 'location' is missing."; |
| 337 | throw new common_configuration_ComponentFactoryException($msg); |
| 338 | } |
| 339 | |
| 340 | if (empty($values['rights'])) { |
| 341 | $msg = "Mandatory attribute 'rights' is missing."; |
| 342 | throw new common_configuration_ComponentFactoryException($msg); |
| 343 | } |
| 344 | |
| 345 | $returnValue = self::buildFileSystemComponent( |
| 346 | $values['location'], |
| 347 | $values['rights'], |
| 348 | $optional |
| 349 | ); |
| 350 | break; |
| 351 | |
| 352 | case 'Custom': |
| 353 | if (empty($values['name'])) { |
| 354 | $msg = "Mandatory attribute 'name' is missing."; |
| 355 | throw new common_configuration_ComponentFactoryException($msg); |
| 356 | } |
| 357 | |
| 358 | $extension = 'generis'; |
| 359 | if (!empty($values['extension'])) { |
| 360 | $extension = $values['extension']; |
| 361 | } |
| 362 | |
| 363 | $returnValue = self::buildCustom($values['name'], $extension, $optional); |
| 364 | break; |
| 365 | |
| 366 | case 'Mock': |
| 367 | $status = common_configuration_Report::VALID; |
| 368 | if (!empty($values['status'])) { |
| 369 | $status = $values['status']; |
| 370 | } |
| 371 | |
| 372 | $returnValue = self::buildMock($status, $optional); |
| 373 | break; |
| 374 | } |
| 375 | } else { |
| 376 | $msg = "No 'value' array provided."; |
| 377 | throw new common_configuration_ComponentFactoryException($msg); |
| 378 | } |
| 379 | } else { |
| 380 | $msg = "Unknown 'type' = '${cleanType}'."; |
| 381 | throw new common_configuration_ComponentFactoryException($msg); |
| 382 | } |
| 383 | } else { |
| 384 | $msg = "Cannot build a Configuration Component without 'type'."; |
| 385 | throw new common_configuration_ComponentFactoryException($msg); |
| 386 | } |
| 387 | } else { |
| 388 | $msg = 'Cannot build a Configuration Component with an empty array.'; |
| 389 | throw new common_configuration_ComponentFactoryException($msg); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | return $returnValue; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Short description of method getFileSystemCount |
| 398 | * |
| 399 | * @access private |
| 400 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 401 | * @return int |
| 402 | */ |
| 403 | private static function getFileSystemCount() |
| 404 | { |
| 405 | $returnValue = self::$fileSystemCount; |
| 406 | |
| 407 | return (int) $returnValue; |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Short description of method setFileSystemCount |
| 412 | * |
| 413 | * @access private |
| 414 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 415 | * @param int fileSystemCount |
| 416 | * @return void |
| 417 | */ |
| 418 | private static function setFileSystemCount($fileSystemCount) |
| 419 | { |
| 420 | |
| 421 | self::$fileSystemCount = $fileSystemCount; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * Short description of method incrementFileSystemCount |
| 426 | * |
| 427 | * @access private |
| 428 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 429 | * @return void |
| 430 | */ |
| 431 | private static function incrementFileSystemCount() |
| 432 | { |
| 433 | |
| 434 | $count = self::getFileSystemCount(); |
| 435 | $count++; |
| 436 | self::setFileSystemCount($count); |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Short description of method getMockCount |
| 441 | * |
| 442 | * @access private |
| 443 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 444 | * @return int |
| 445 | */ |
| 446 | private static function getMockCount() |
| 447 | { |
| 448 | $returnValue = self::$mockCount; |
| 449 | |
| 450 | return (int) $returnValue; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Short description of method setMockCount |
| 455 | * |
| 456 | * @access private |
| 457 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 458 | * @param int mockCount |
| 459 | * @return void |
| 460 | */ |
| 461 | private static function setMockCount($mockCount) |
| 462 | { |
| 463 | |
| 464 | self::$mockCount = $mockCount; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Short description of method incrementMockCount |
| 469 | * |
| 470 | * @access private |
| 471 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 472 | * @return void |
| 473 | */ |
| 474 | private static function incrementMockCount() |
| 475 | { |
| 476 | |
| 477 | $count = self::getMockCount(); |
| 478 | $count++; |
| 479 | self::setMockCount($count); |
| 480 | } |
| 481 | } |