| Conditions | 19 |
| Paths | 111 |
| Total Lines | 131 |
| Code Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 41 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 42 | { |
||
| 43 | $start = microtime(true); |
||
| 44 | |||
| 45 | // as per https://www.php.net/manual/en/function.ignore-user-abort.php: for cli scripts, it is probably a good idea |
||
| 46 | // to use ignore_user_abort |
||
| 47 | ignore_user_abort(true); |
||
| 48 | |||
| 49 | $this->setOutput($output); |
||
| 50 | $this->setVerbosity($output->getVerbosity()); |
||
| 51 | |||
| 52 | $instanceList = $this->parseCommonOptions($input); |
||
| 53 | |||
| 54 | $sql = $input->getOption('sql'); |
||
| 55 | $file = $input->getOption('file'); |
||
| 56 | $dbName = $input->getOption('database'); |
||
| 57 | $userName = $input->getOption('user'); |
||
| 58 | $password = $input->getOption('password'); |
||
| 59 | |||
| 60 | if ($sql == null && $file == null) { |
||
| 61 | throw new \Exception("Please provide an sql command or file to be executed"); |
||
| 62 | } |
||
| 63 | if ($sql != null && $file != null) { |
||
| 64 | throw new \Exception("Please provide either an sql command or file to be executed, not both"); |
||
| 65 | } |
||
| 66 | |||
| 67 | if (($dbName == null) xor ($userName == null)) { |
||
| 68 | |||
| 69 | /// @todo is it possible to let the user log in to the 'root' db, regardless of db type ? |
||
| 70 | |||
| 71 | throw new \Exception("Please provide both a custom database name and associated user account"); |
||
| 72 | } |
||
| 73 | |||
| 74 | $createDB = ($dbName == null); |
||
| 75 | |||
| 76 | if ($createDB) { |
||
| 77 | // create temp databases |
||
| 78 | |||
| 79 | if ($this->outputFormat === 'text') { |
||
| 80 | $this->writeln('<info>Creating temporary databases...</info>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 81 | } |
||
| 82 | |||
| 83 | /// @todo inject more randomness in the username, by allowing more chars than bin2hex produces |
||
| 84 | |||
| 85 | $userName = 'db3v4l_' . substr(bin2hex(random_bytes(5)), 0, 9); // some mysql versions have a limitation of 16 chars for usernames |
||
| 86 | $password = bin2hex(random_bytes(15)); // some oracle versions have a limit of 30 chars on passwords |
||
| 87 | //$dbName = bin2hex(random_bytes(31)); |
||
| 88 | $dbName = $userName; // $userName will be used as db name |
||
| 89 | |||
| 90 | $tempDbSpecs = []; |
||
| 91 | foreach($instanceList as $instanceName => $instanceSpecs) { |
||
| 92 | $tempDbSpecs[$instanceName] = [ |
||
| 93 | 'dbname' => $dbName, |
||
| 94 | 'user' => $userName, |
||
| 95 | 'password' => $password |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | |||
| 99 | if (($charset = $input->getOption('charset')) != '') { |
||
| 100 | foreach($instanceList as $instanceName) { |
||
| 101 | $tempDbSpecs[$instanceName]['charset'] = $charset; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | // for now, we always use NativeClient for creating/dropping temp dbs |
||
| 106 | $previousStrategy = $this->executionStrategy; |
||
| 107 | $this->executionStrategy = NativeClient::EXECUTION_STRATEGY; |
||
| 108 | $creationResults = $this->createDatabases($instanceList, $tempDbSpecs); |
||
| 109 | $this->executionStrategy = $previousStrategy; |
||
| 110 | |||
| 111 | $dbConnectionSpecs = $creationResults['data']; |
||
| 112 | |||
| 113 | } else { |
||
| 114 | // use existing databases |
||
| 115 | |||
| 116 | if ($this->outputFormat === 'text') { |
||
| 117 | $this->writeln('<info>Using existing databases...</info>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 118 | } |
||
| 119 | |||
| 120 | $dbConfig = [ |
||
| 121 | 'user' => $userName, |
||
| 122 | 'password' => $password, |
||
| 123 | 'dbname' => $dbName, |
||
| 124 | ]; |
||
| 125 | |||
| 126 | $dbConnectionSpecs = []; |
||
| 127 | foreach($instanceList as $instanceName => $instanceSpecs) { |
||
| 128 | $dbConnectionSpecs[$instanceName] = array_merge($instanceSpecs, $dbConfig); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | if (count($dbConnectionSpecs)) { |
||
| 133 | |||
| 134 | if ($this->outputFormat === 'text') { |
||
| 135 | $this->writeln('<info>Executing sql command/file...</info>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 136 | } |
||
| 137 | |||
| 138 | $results = $this->executeSQL($dbConnectionSpecs, $sql, $file); |
||
| 139 | |||
| 140 | if ($this->outputFormat === 'text') { |
||
| 141 | $this->writeln('<info>Dropping temporary databases...</info>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($createDB) { |
||
| 145 | $results['failed'] += $creationResults['failed']; |
||
| 146 | |||
| 147 | foreach($instanceList as $instanceName => $instanceSpecs) { |
||
| 148 | if (!isset($dbConnectionSpecs[$instanceName])) { |
||
| 149 | /// @todo retrieve the actual temp db creation error instead of doing this... |
||
| 150 | $results['data'][$instanceName] = [ |
||
| 151 | 'exitcode' => '1', |
||
| 152 | 'stderr' => 'Error in creation of temporary database' |
||
| 153 | ]; |
||
| 154 | unset($instanceList[$instanceName]); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | // for now, we always use NativeClient for creating/dropping temp dbs |
||
| 158 | $previousStrategy = $this->executionStrategy; |
||
| 159 | $this->executionStrategy = NativeClient::EXECUTION_STRATEGY; |
||
| 160 | $this->dropDatabases($instanceList, $dbConnectionSpecs, true); |
||
| 161 | $this->executionStrategy = $previousStrategy; |
||
| 162 | } |
||
| 163 | } else { |
||
| 164 | $results = ['succeeded' => 0, 'failed' => 0, 'data' => null]; |
||
| 165 | } |
||
| 166 | |||
| 167 | $time = microtime(true) - $start; |
||
| 168 | |||
| 169 | $this->writeResults($results, $time); |
||
| 170 | |||
| 171 | return (int)$results['failed']; |
||
| 172 | } |
||
| 297 |