| Conditions | 15 |
| Paths | 154 |
| Total Lines | 106 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 11 | ||
| Bugs | 5 | Features | 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 |
||
| 53 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 54 | { |
||
| 55 | $hieranchy = [self::DEVELOP, self::STAGING, self::QUALITY, self::MASTER]; |
||
| 56 | $validBranchs = [self::STAGING, self::QUALITY, self::MASTER]; |
||
| 57 | $nameOfProject = $input->getArgument('project'); |
||
| 58 | $branchOfProject = $input->getArgument('branch'); |
||
| 59 | $helper = $this->getHelper('question'); |
||
| 60 | |||
| 61 | $nameOfProject = $this->getNameOfProject($input, $output, $helper, $nameOfProject); |
||
| 62 | |||
| 63 | if (!$branchOfProject) { |
||
| 64 | $question = new Question('<question>What is the branch to deploy?</question>: '); |
||
| 65 | do { |
||
| 66 | $branchOfProject = trim($helper->ask($input, $output, $question)); |
||
| 67 | } while (empty($branchOfProject) || !in_array($branchOfProject, $validBranchs)); |
||
| 68 | } |
||
| 69 | |||
| 70 | $jsonDb = $this->databaseService->getProjects(); |
||
| 71 | |||
| 72 | if (is_null($jsonDb)) { |
||
| 73 | $output->writeln(''); |
||
| 74 | $output->writeln('<info>0 projects configurated</info>'); |
||
| 75 | } else { |
||
| 76 | if (isset($jsonDb[$nameOfProject])) { |
||
| 77 | $merge = self::DEVELOP; |
||
| 78 | $final = self::STAGING; |
||
| 79 | foreach ($hieranchy as $k => $v) { |
||
| 80 | if ($v == $branchOfProject) { |
||
| 81 | $merge = $hieranchy[$k-1]; |
||
| 82 | $final = $v; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | chdir($jsonDb[$nameOfProject]); |
||
| 87 | |||
| 88 | $pipetask = [ |
||
| 89 | 'git checkout ' .$merge, |
||
| 90 | 'git pull', |
||
| 91 | 'git checkout ' .$final, |
||
| 92 | 'git pull', |
||
| 93 | 'git merge '.$merge, |
||
| 94 | 'git push --progress 2>&1', |
||
| 95 | 'git checkout develop', |
||
| 96 | ]; |
||
| 97 | |||
| 98 | $progressBar = new ProgressBar($output, count($pipetask)); |
||
| 99 | $progressBar->setBarCharacter('<fg=magenta>=</>'); |
||
| 100 | $progressBar->setFormat("%message%\n %current%/%max% [%bar%] %percent:3s%%"); |
||
| 101 | $progressBar->setBarWidth(50); |
||
| 102 | |||
| 103 | $table = new Table($output); |
||
| 104 | $table->setHeaders(array('<fg=white>Command</>', '<fg=white>Result</>')); |
||
| 105 | |||
| 106 | $exitCode = 0; |
||
| 107 | $exitCodeMessage = ""; |
||
| 108 | foreach ($pipetask as $t){ |
||
| 109 | if ($exitCode == 0){ |
||
|
|
|||
| 110 | $command = new \mikehaertl\shellcommand\Command($t); |
||
| 111 | if ($command->execute()) { |
||
| 112 | $message = $command->getOutput(); |
||
| 113 | $message = trim(preg_replace('/\t+/', '', $message)); |
||
| 114 | $message = trim(preg_replace('/Ma\n+/', '', $message)); |
||
| 115 | $message = trim(preg_replace('/a\n+/', '', $message)); |
||
| 116 | $message = trim(preg_replace('/|/', '', $message)); |
||
| 117 | $exitCode = $command->getExitCode(); |
||
| 118 | } else { |
||
| 119 | $message = $command->getError(); |
||
| 120 | $message = trim(preg_replace('/\t+/', '', $message)); |
||
| 121 | $message = trim(preg_replace('/Ma\n+/', '', $message)); |
||
| 122 | $message = trim(preg_replace('/a\n+/', '', $message)); |
||
| 123 | $message = trim(preg_replace('/|/', '', $message)); |
||
| 124 | $exitCode = $command->getExitCode(); |
||
| 125 | } |
||
| 126 | } else { |
||
| 127 | $message = ''; |
||
| 128 | $exitCode = -1; |
||
| 129 | } |
||
| 130 | |||
| 131 | if ($exitCode == 0){ |
||
| 132 | $exitCodeMessage = '<fg=green>SUCCESS</>'; |
||
| 133 | } elseif ($exitCode == 1){ |
||
| 134 | $exitCodeMessage = '<fg=red>FAILED</>'; |
||
| 135 | } elseif ($exitCode == -1){ |
||
| 136 | $exitCodeMessage = '<fg=blue>ABORTED</>'; |
||
| 137 | } |
||
| 138 | |||
| 139 | $command = '<fg=magenta>'.$t.'</>'; |
||
| 140 | if (strlen(trim($message))>0){ |
||
| 141 | $command .= "\n".$message; |
||
| 142 | } |
||
| 143 | $table->addRow([$command, $exitCodeMessage]); |
||
| 144 | usleep(300000); |
||
| 145 | $progressBar->setMessage($t); |
||
| 146 | $progressBar->advance(); |
||
| 147 | } |
||
| 148 | $progressBar->setMessage(""); |
||
| 149 | |||
| 150 | $progressBar->finish(); |
||
| 151 | |||
| 152 | $output->writeln(''); |
||
| 153 | $table->render(); |
||
| 154 | } |
||
| 155 | |||
| 156 | } |
||
| 157 | |||
| 158 | } |
||
| 159 | } |
||
| 160 |