| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Consolidation\SiteProcess\Transport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Drush\Drush; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Robo\Common\IO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Style\OutputStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Consolidation\SiteProcess\Util\RealtimeOutputHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Consolidation\SiteProcess\Util\Escape; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Output\ConsoleOutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Consolidation\SiteAlias\AliasRecord; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Consolidation\SiteProcess\Util\Shell; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * DockerComposeTransport knows how to wrap a command such that it executes on a Docker Compose service. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class DockerComposeTransport implements TransportInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $tty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $siteAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $cd; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function __construct(AliasRecord $siteAlias) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->siteAlias = $siteAlias; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function configure(Process $process) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->tty = $process->isTty(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 41 |  | View Code Duplication |     public function wrap($args) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $transport = ['docker-compose', 'exec']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $transportOptions = $this->getTransportOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $commandToExecute = $this->getCommandToExecute($args); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $transport, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             $transportOptions, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $commandToExecute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function addChdir($cd, $args) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->cd = $cd; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * getTransportOptions returns the transport options for the tranport | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * mechanism itself | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     protected function getTransportOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $transportOptions[] = $this->siteAlias->get('service'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if ($options = $this->siteAlias->get('exec.options')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             array_unshift($transportOptions, Shell::preEscaped($options)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         // Adding -T breaks sql:cli and php:cli so we never do it for now. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (false && $this->tty) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             array_unshift($transportOptions, '-T'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if ($this->cd) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             array_unshift($transportOptions, ['--workdir', $this->cd]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return array_filter($transportOptions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * getCommandToExecute processes the arguments for the command to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * be executed such that they are appropriate for the transport mechanism. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Nothing to do for this transport. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |     protected function getCommandToExecute($args) | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         return $args; | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.