Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| tao_install_services_CheckCustomService | |
0.00% |
0 / 50 |
|
0.00% |
0 / 5 |
650 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| checkData | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
342 | |||
| buildComponent | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| buildResult | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
| 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 | * A Service implementation aiming at calling a custom check. |
| 29 | * |
| 30 | * Please refer to tao/install/api.php for more information about how to call this service |
| 31 | * |
| 32 | * @access public |
| 33 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 34 | * @package tao |
| 35 | |
| 36 | */ |
| 37 | class tao_install_services_CheckCustomService extends tao_install_services_Service implements |
| 38 | tao_install_services_CheckService |
| 39 | { |
| 40 | /** |
| 41 | * Creates a new instance of the service. |
| 42 | * @param tao_install_services_Data $data The input data to be handled by the service. |
| 43 | * @throws InvalidArgumentException If the input data structured is malformed or is missing data. |
| 44 | */ |
| 45 | public function __construct(tao_install_services_Data $data) |
| 46 | { |
| 47 | parent::__construct($data); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Executes the main logic of the service. |
| 52 | * @return tao_install_services_Data The result of the service execution. |
| 53 | */ |
| 54 | public function execute() |
| 55 | { |
| 56 | $content = json_decode($this->getData()->getContent(), true); |
| 57 | $name = $content['value']['name']; |
| 58 | $extension = $content['value']['extension']; |
| 59 | $check = self::buildComponent($this->getData()); |
| 60 | |
| 61 | if ($check !== null) { |
| 62 | $report = $check->check(); |
| 63 | $this->setResult(self::buildResult($this->getData(), $report, $check)); |
| 64 | } else { |
| 65 | throw new tao_install_services_UnknownCustomCheckException($name, $extension); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | protected function checkData() |
| 70 | { |
| 71 | $content = json_decode($this->getData()->getContent(), true); |
| 72 | if (!isset($content['type']) || empty($content['type'])) { |
| 73 | throw new InvalidArgumentException("Missing data: 'type' must be provided."); |
| 74 | } elseif ($content['type'] !== 'CheckCustom') { |
| 75 | throw new InvalidArgumentException("Unexpected type: 'type' must be equal to 'CheckCustom'."); |
| 76 | } elseif ( |
| 77 | !isset($content['value']) |
| 78 | || empty($content['value']) |
| 79 | || !is_array($content['value']) |
| 80 | || count($content['value']) == 0 |
| 81 | ) { |
| 82 | throw new InvalidArgumentException("Missing data: 'value' must be provided as a not empty array."); |
| 83 | } elseif (!isset($content['value']['id']) || empty($content['value']['id'])) { |
| 84 | throw new InvalidArgumentException("Missing data: 'id' must be provided."); |
| 85 | } elseif (!isset($content['value']['name']) || empty($content['value']['name'])) { |
| 86 | throw new InvalidArgumentException("Missing data: 'name' must be provided."); |
| 87 | } elseif (!isset($content['value']['extension']) || empty($content['value']['extension'])) { |
| 88 | throw new InvalidArgumentException("Missing data: 'extension' must be provided"); |
| 89 | } elseif (isset($content['value']['parameters'])) { |
| 90 | // If parameters are given to the custom check... |
| 91 | if ( |
| 92 | empty($content['value']['parameters']) |
| 93 | || !is_array($content['value']['parameters']) |
| 94 | || count($content['value']['parameters']) == 0 |
| 95 | ) { |
| 96 | throw new InvalidArgumentException("Missing data: 'parameters' must be provided as a not empty array."); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | public static function buildComponent(tao_install_services_Data $data) |
| 102 | { |
| 103 | $content = json_decode($data->getContent(), true); |
| 104 | $name = $content['value']['name']; |
| 105 | |
| 106 | if (isset($content['value']['optional'])) { |
| 107 | $optional = $content['value']['optional']; |
| 108 | } else { |
| 109 | $optional = false; |
| 110 | } |
| 111 | $extension = $content['value']['extension']; |
| 112 | |
| 113 | try { |
| 114 | return common_configuration_ComponentFactory::buildCustom($name, $extension, $optional); |
| 115 | } catch (common_configuration_ComponentFactoryException $e) { |
| 116 | return null; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public static function buildResult( |
| 121 | tao_install_services_Data $data, |
| 122 | common_configuration_Report $report, |
| 123 | common_configuration_Component $component |
| 124 | ) { |
| 125 | |
| 126 | $content = json_decode($data->getContent(), true); |
| 127 | |
| 128 | $id = $content['value']['id']; |
| 129 | $extension = $content['value']['extension']; |
| 130 | |
| 131 | $data = ['type' => 'CheckCustomReport', |
| 132 | 'value' => ['status' => $report->getStatusAsString(), |
| 133 | 'message' => $report->getMessage(), |
| 134 | 'name' => $component->getName(), |
| 135 | 'extension' => $extension, |
| 136 | 'optional' => $component->isOptional(), |
| 137 | 'id' => $id]]; |
| 138 | |
| 139 | return new tao_install_services_Data(json_encode($data)); |
| 140 | } |
| 141 | } |