| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dock\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dock\Docker\Compose\ComposeExecutableFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Dock\Docker\Compose\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Dock\IO\Process\InteractiveProcessBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Dock\IO\Process\InteractiveProcessManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Dock\IO\ProcessRunner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Dock\IO\UserInteraction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class DockerComposeProjectManager implements ProjectManager, ProjectBuildManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var InteractiveProcessBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private $interactiveProcessBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var UserInteraction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $userInteraction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var ProcessRunner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $processRunner; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var ComposeExecutableFinder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $composeExecutableFinder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param InteractiveProcessBuilder $interactiveProcessBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param UserInteraction           $userInteraction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param ProcessRunner             $processRunner | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param ComposeExecutableFinder   $composeExecutableFinder | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     public function __construct(InteractiveProcessBuilder $interactiveProcessBuilder, UserInteraction $userInteraction, ProcessRunner $processRunner, ComposeExecutableFinder $composeExecutableFinder) | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $this->interactiveProcessBuilder = $interactiveProcessBuilder; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $this->userInteraction = $userInteraction; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $this->processRunner = $processRunner; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->composeExecutableFinder = $composeExecutableFinder; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function start(Project $project) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->userInteraction->writeTitle('Starting application containers'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->interactiveProcessBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             ->forCommand(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |                 '%s up -d', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 $this->composeExecutableFinder->find() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             )) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             ->disableOutput() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             ->withoutTimeout() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->ifTakesMoreThan(5000, function (InteractiveProcessManager $processManager) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $processManager->enableOutput(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ->getManager() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function stop(Project $project) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->userInteraction->writeTitle('Stopping application containers'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->processRunner->followsUpWith($this->composeExecutableFinder->find(), ['stop']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     public function reset(Project $project, array $containers = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $composePath = $this->composeExecutableFinder->find(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 86 |  | View Code Duplication |         $this->processRunner->run(implode(' && ', array_map(function ($action) use ($composePath, $containers) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             return implode(' ', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 $composePath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 $action, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 implode(' ', $containers), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         }, ['kill', 'rm -f', 'up -d']))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function build(Project $project, array $containers = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $composePath = $this->composeExecutableFinder->find(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 102 |  | View Code Duplication |         $this->processRunner->run(implode(' && ', array_map(function ($action) use ($composePath, $containers) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             return implode(' ', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $composePath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                 $action, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 implode(' ', $containers), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         }, ['build']))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->reset($project, $containers); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                        
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.