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