Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| tao_install_services_CheckDatabaseConnectionService | |
0.00% |
0 / 72 |
|
0.00% |
0 / 6 |
702 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| onError | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| checkData | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
272 | |||
| buildComponent | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| buildResult | |
0.00% |
0 / 44 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | |
| 3 | use oat\generis\persistence\PersistenceManager; |
| 4 | |
| 5 | /* |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; under version 2 |
| 9 | * of the License (non-upgradable). |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | * |
| 20 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 21 | * (under the project TAO & TAO2); |
| 22 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 23 | * (under the project TAO-TRANSFER); |
| 24 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 25 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * A Service implementation aiming at checking that it is possible to connect to |
| 31 | * a database with particular driver, user and password. |
| 32 | * |
| 33 | * Please refer to tao/install/api.php for more information about how to call this service. |
| 34 | * |
| 35 | * @access public |
| 36 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 37 | * @package tao |
| 38 | |
| 39 | */ |
| 40 | class tao_install_services_CheckDatabaseConnectionService extends tao_install_services_Service implements |
| 41 | tao_install_services_CheckService |
| 42 | { |
| 43 | /** |
| 44 | * Creates a new instance of the service. |
| 45 | * @param tao_install_services_Data $data The input data to be handled by the service. |
| 46 | * @throws InvalidArgumentException If the input data structured is malformed or is missing data. |
| 47 | */ |
| 48 | public function __construct($data) |
| 49 | { |
| 50 | parent::__construct($data); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Executes the main logic of the service. |
| 55 | * @return tao_install_services_Data The result of the service execution. |
| 56 | */ |
| 57 | public function execute() |
| 58 | { |
| 59 | $ext = self::buildComponent($this->getData()); |
| 60 | $report = $ext->check(); |
| 61 | $this->setResult(self::buildResult($this->getData(), $report, $ext)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Custom error handler to prevent noisy output at bad connection time. |
| 66 | * @return boolean |
| 67 | */ |
| 68 | public static function onError($errno, $errstr, $errfile, $errline) |
| 69 | { |
| 70 | // Do not call PHP internal error handler ! |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | protected function checkData() |
| 75 | { |
| 76 | $content = json_decode($this->getData()->getContent(), true); |
| 77 | if (!isset($content['type']) || empty($content['type']) || $content['type'] != 'CheckDatabaseConnection') { |
| 78 | throw new InvalidArgumentException("Unexpected type: 'type' must be equal to 'CheckDatabaseConnection'."); |
| 79 | } elseif (!isset($content['value']) || empty($content['value'])) { |
| 80 | throw new InvalidArgumentException("Missing data: 'value' must be provided."); |
| 81 | } elseif (!isset($content['value']['driver']) || empty($content['value']['driver'])) { |
| 82 | throw new InvalidArgumentException("Missing data: 'driver' must be provided."); |
| 83 | } elseif (!isset($content['value']['user'])) { |
| 84 | throw new InvalidArgumentException("Missing data: 'user' must be provided."); |
| 85 | } elseif (!isset($content['value']['password'])) { |
| 86 | throw new InvalidArgumentException("Missing data: 'password' must be provided."); |
| 87 | } elseif (!isset($content['value']['host']) || empty($content['value']['host'])) { |
| 88 | throw new InvalidArgumentException("Missing data: 'host' must be provided."); |
| 89 | } elseif (!isset($content['value']['overwrite']) || !is_bool($content['value']['overwrite'])) { |
| 90 | throw new InvalidArgumentException("Missing data: 'overwrite' must be provided."); |
| 91 | } elseif (!isset($content['value']['database']) || empty($content['value']['database'])) { |
| 92 | throw new InvalidArgumentException("Missing data: 'database' must be provided."); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public static function buildComponent(tao_install_services_Data $data) |
| 97 | { |
| 98 | $content = json_decode($data->getContent(), true); |
| 99 | $driver = $content['value']['driver']; |
| 100 | if (isset($content['value']['optional'])) { |
| 101 | $optional = $content['value']['optional']; |
| 102 | } else { |
| 103 | $optional = false; |
| 104 | } |
| 105 | |
| 106 | // Try such a driver. Because the provided driver name should |
| 107 | // comply with a PHP Extension name (e.g. mysql, pgsql), we test its |
| 108 | // existence. |
| 109 | return common_configuration_ComponentFactory::buildPHPDatabaseDriver($driver, $optional); |
| 110 | } |
| 111 | |
| 112 | public static function buildResult( |
| 113 | tao_install_services_Data $data, |
| 114 | common_configuration_Report $report, |
| 115 | common_configuration_Component $component |
| 116 | ) { |
| 117 | |
| 118 | $content = json_decode($data->getContent(), true); |
| 119 | $driver = $content['value']['driver']; |
| 120 | $user = $content['value']['user']; |
| 121 | $password = $content['value']['password']; |
| 122 | $host = $content['value']['host']; |
| 123 | $overwrite = $content['value']['overwrite']; |
| 124 | $database = $content['value']['database']; |
| 125 | $message = ''; |
| 126 | if ($report->getStatus() == common_configuration_Report::VALID) { |
| 127 | // Great the driver is there, we can try a connection. |
| 128 | try { |
| 129 | set_error_handler(['tao_install_services_CheckDatabaseConnectionService', 'onError']); |
| 130 | //$dbCreatorClassName = tao_install_utils_DbCreator::getClassNameForDriver($driver); |
| 131 | //$dbCreator = new $dbCreatorClassName($host, $user, $password, $driver); |
| 132 | |
| 133 | $installParams = [ |
| 134 | 'db_driver' => $driver, |
| 135 | 'db_host' => $host, |
| 136 | 'db_name' => $database, |
| 137 | 'db_user' => $user, |
| 138 | 'db_pass' => $password |
| 139 | ]; |
| 140 | $dbalConfigCreator = new tao_install_utils_DbalConfigCreator(); |
| 141 | $persistenceManager = new PersistenceManager(); |
| 142 | $persistenceManager->registerPersistence( |
| 143 | 'default', |
| 144 | $dbalConfigCreator->createDbalConfig($installParams) |
| 145 | ); |
| 146 | $persistence = $persistenceManager->getPersistenceById('default'); |
| 147 | // If we are here, we are connected. |
| 148 | if ($overwrite == false && !empty($persistence->getSchemaManager()->getTables())) { |
| 149 | $message = "The database with name '${database}' is not empty."; |
| 150 | $status = 'invalid-overwrite'; |
| 151 | } else { |
| 152 | $message = "Database connection successfully established with '${host}' using driver '${driver}'."; |
| 153 | $status = 'valid'; |
| 154 | } |
| 155 | |
| 156 | restore_error_handler(); |
| 157 | } catch (Exception $e) { |
| 158 | $message = "Unable to connect to database '${database}' at '${host}' using driver '${driver}': " |
| 159 | . $e->getMessage(); |
| 160 | $status = 'invalid-noconnection'; |
| 161 | restore_error_handler(); |
| 162 | } |
| 163 | } else { |
| 164 | // No driver found. |
| 165 | $status = 'invalid-nodriver'; |
| 166 | $message = "Database driver '${driver}' is not available."; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | $value = ['status' => $status, |
| 171 | 'message' => $message, |
| 172 | 'optional' => $component->isOptional(), |
| 173 | 'name' => $component->getName()]; |
| 174 | $data = ['type' => 'DatabaseConnectionReport', |
| 175 | 'value' => $value]; |
| 176 | return new tao_install_services_Data(json_encode($data)); |
| 177 | } |
| 178 | } |