| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Docker\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Docker\API\Resource\ContainerResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Docker\Stream\AttachWebsocketStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Docker\Stream\DockerRawStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Joli\Jane\Swagger\Client\QueryParam; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class ContainerManager extends ContainerResource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     const FETCH_STREAM = 'stream'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @return \Psr\Http\Message\ResponseInterface|DockerRawStream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 19 | 1 | View Code Duplication |     public function attach($id, $parameters = [], $fetch = self::FETCH_OBJECT) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 1 |  |         $response = parent::attach($id, $parameters, $fetch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 1 |  |         if ($response->getStatusCode() == 200 && DockerRawStream::HEADER == $response->getHeaderLine('Content-Type')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 1 |  |             if ($fetch == self::FETCH_STREAM) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 1 |  |                 return new DockerRawStream($response->getBody()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @return \Psr\Http\Message\ResponseInterface|AttachWebsocketStream | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 37 | 1 |  |     public function attachWebsocket($id, $parameters = [], $fetch = self::FETCH_OBJECT) | 
            
                                                        
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 39 | 1 |  |         $queryParam = new QueryParam(); | 
            
                                                        
            
                                    
            
            
                | 40 | 1 |  |         $queryParam->setDefault('logs', null); | 
            
                                                        
            
                                    
            
            
                | 41 | 1 |  |         $queryParam->setDefault('stream', null); | 
            
                                                        
            
                                    
            
            
                | 42 | 1 |  |         $queryParam->setDefault('stdin', null); | 
            
                                                        
            
                                    
            
            
                | 43 | 1 |  |         $queryParam->setDefault('stdout', null); | 
            
                                                        
            
                                    
            
            
                | 44 | 1 |  |         $queryParam->setDefault('stderr', null); | 
            
                                                        
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 46 | 1 |  |         $url      = '/containers/{id}/attach/ws'; | 
            
                                                        
            
                                    
            
            
                | 47 | 1 |  |         $url      = str_replace('{id}', $id, $url); | 
            
                                                        
            
                                    
            
            
                | 48 | 1 |  |         $url      = $url . ('?' . $queryParam->buildQueryString($parameters)); | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 | 1 |  |         $headers  = array_merge([ | 
            
                                                        
            
                                    
            
            
                | 51 | 1 |  |             'Host' => 'localhost', | 
            
                                                        
            
                                    
            
            
                | 52 | 1 |  |             'Origin' => 'php://docker-php', | 
            
                                                        
            
                                    
            
            
                | 53 | 1 |  |             'Upgrade' => 'websocket', | 
            
                                                        
            
                                    
            
            
                | 54 | 1 |  |             'Connection' => 'Upgrade', | 
            
                                                        
            
                                    
            
            
                | 55 | 1 |  |             'Sec-WebSocket-Version' => '13', | 
            
                                                        
            
                                    
            
            
                | 56 | 1 |  |             'Sec-WebSocket-Key' => base64_encode(uniqid()), | 
            
                                                        
            
                                    
            
            
                | 57 | 1 |  |         ], $queryParam->buildHeaders($parameters)); | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 59 | 1 |  |         $body     = $queryParam->buildFormDataString($parameters); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 | 1 |  |         $request  = $this->messageFactory->createRequest('GET', $url, $headers, $body); | 
            
                                                        
            
                                    
            
            
                | 62 | 1 |  |         $response = $this->httpClient->sendRequest($request); | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 | 1 |  |         if ($response->getStatusCode() == 101) { | 
            
                                                        
            
                                    
            
            
                | 65 | 1 |  |             if ($fetch == self::FETCH_STREAM) { | 
            
                                                        
            
                                    
            
            
                | 66 | 1 |  |                 return new AttachWebsocketStream($response->getBody()); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         return $response; | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                        
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.