| Conditions | 18 |
| Paths | 63 |
| Total Lines | 112 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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 |
||
| 40 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 41 | { |
||
| 42 | $start = microtime(true); |
||
| 43 | |||
| 44 | // as per https://www.php.net/manual/en/function.ignore-user-abort.php: for cli scripts, it is probably a good idea |
||
| 45 | // to use ignore_user_abort |
||
| 46 | ignore_user_abort(true); |
||
| 47 | |||
| 48 | $this->setOutput($output); |
||
| 49 | $this->setVerbosity($output->getVerbosity()); |
||
| 50 | |||
| 51 | $instanceList = $this->parseCommonOptions($input); |
||
| 52 | |||
| 53 | $sql = $input->getOption('sql'); |
||
| 54 | $file = $input->getOption('file'); |
||
| 55 | $dbName = $input->getOption('database'); |
||
| 56 | $userName = $input->getOption('user'); |
||
| 57 | $password = $input->getOption('password'); |
||
| 58 | |||
| 59 | if ($sql == null && $file == null) { |
||
| 60 | throw new \Exception("Please provide an sql command or file to be executed"); |
||
| 61 | } |
||
| 62 | if ($sql != null && $file != null) { |
||
| 63 | throw new \Exception("Please provide either an sql command or file to be executed, not both"); |
||
| 64 | } |
||
| 65 | |||
| 66 | if (($dbName == null) xor ($userName == null)) { |
||
| 67 | |||
| 68 | /// @todo is it possible to let the user log in to the 'root' db, regardless of db type ? |
||
| 69 | |||
| 70 | throw new \Exception("Please provide both a custom database name and associated user account"); |
||
| 71 | } |
||
| 72 | |||
| 73 | $createDB = ($dbName == null); |
||
| 74 | |||
| 75 | if ($createDB) { |
||
| 76 | // create temp databases |
||
| 77 | |||
| 78 | if ($this->outputFormat === 'text') { |
||
| 79 | $this->writeln('<info>Creating temporary databases...</info>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 80 | } |
||
| 81 | |||
| 82 | /// @todo inject more randomness in the username, by allowing more chars than bin2hex produces |
||
| 83 | |||
| 84 | $userName = 'db3v4l_' . substr(bin2hex(random_bytes(5)), 0, 9); // some mysql versions have a limitation of 16 chars for usernames |
||
| 85 | $password = bin2hex(random_bytes(16)); |
||
| 86 | //$dbName = bin2hex(random_bytes(31)); |
||
| 87 | $dbName = $userName; // $userName will be used as db name |
||
| 88 | |||
| 89 | $tempDbSpecs = []; |
||
| 90 | foreach($instanceList as $instanceName => $instanceSpecs) { |
||
| 91 | $tempDbSpecs[$instanceName] = [ |
||
| 92 | 'dbname' => $dbName, |
||
| 93 | 'user' => $userName, |
||
| 94 | 'password' => $password |
||
| 95 | ]; |
||
| 96 | } |
||
| 97 | |||
| 98 | if (($charset = $input->getOption('charset')) != '') { |
||
| 99 | foreach($instanceList as $instanceName) { |
||
| 100 | $tempDbSpecs[$instanceName]['charset'] = $charset; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | $creationResults = $this->createDatabases($instanceList, $tempDbSpecs); |
||
| 105 | $dbConnectionSpecs = $creationResults['data']; |
||
| 106 | |||
| 107 | } else { |
||
| 108 | // use existing databases |
||
| 109 | |||
| 110 | if ($this->outputFormat === 'text') { |
||
| 111 | $this->writeln('<info>Using existing databases...</info>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 112 | } |
||
| 113 | |||
| 114 | $dbConfig = [ |
||
| 115 | 'user' => $userName, |
||
| 116 | 'password' => $password, |
||
| 117 | 'dbname' => $dbName, |
||
| 118 | ]; |
||
| 119 | |||
| 120 | $dbConnectionSpecs = []; |
||
| 121 | foreach($instanceList as $instanceName => $instanceSpecs) { |
||
| 122 | $dbConnectionSpecs[$instanceName] = array_merge($instanceSpecs, $dbConfig); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | if (count($dbConnectionSpecs)) { |
||
| 127 | $results = $this->executeSQL($dbConnectionSpecs, $sql, $file); |
||
| 128 | |||
| 129 | if ($this->outputFormat === 'text') { |
||
| 130 | $this->writeln('<info>Dropping temporary databases...</info>', OutputInterface::VERBOSITY_VERBOSE); |
||
| 131 | } |
||
| 132 | |||
| 133 | if ($createDB) { |
||
| 134 | $results['failed'] += $creationResults['failed']; |
||
| 135 | |||
| 136 | foreach($instanceList as $instanceName => $instanceSpecs) { |
||
| 137 | if (!isset($dbConnectionSpecs[$instanceName])) { |
||
| 138 | unset($instanceList[$instanceName]); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | $this->dropDatabases($instanceList, $dbConnectionSpecs, true); |
||
| 142 | } |
||
| 143 | } else { |
||
| 144 | $results = ['succeeded' => 0, 'failed' => 0, 'data' => null]; |
||
| 145 | } |
||
| 146 | |||
| 147 | $time = microtime(true) - $start; |
||
| 148 | |||
| 149 | $this->writeResults($results, $time); |
||
| 150 | |||
| 151 | return (int)$results['failed']; |
||
| 152 | } |
||
| 254 |