| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Docker\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Docker\API\Model\AuthConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Docker\API\Model\BuildInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Docker\API\Model\CreateImageInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Docker\API\Model\PushImageInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Docker\API\Resource\ImageResource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Docker\Stream\BuildStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Docker\Stream\CreateImageStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Docker\Stream\PushStream; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Joli\Jane\Swagger\Client\QueryParam; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class ImageManager extends ImageResource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const FETCH_STREAM = 'stream'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return \Psr\Http\Message\ResponseInterface|BuildInfo[]|BuildStream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 24 | 4 | View Code Duplication |     public function build($inputStream, $parameters = [], $fetch = self::FETCH_OBJECT) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 4 |  |         $response = parent::build($inputStream, $parameters, self::FETCH_RESPONSE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 4 |  |         if (200 === $response->getStatusCode()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 4 |  |             if (self::FETCH_STREAM === $fetch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 1 |  |                 return new BuildStream($response->getBody(), $this->serializer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 3 |  |             if (self::FETCH_OBJECT === $fetch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |                 $buildInfoList = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 3 |  |                 $stream = new BuildStream($response->getBody(), $this->serializer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 $stream->onFrame(function (BuildInfo $buildInfo) use (&$buildInfoList) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 3 |  |                     $buildInfoList[] = $buildInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 3 |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  |                 $stream->wait(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |                 return $buildInfoList; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return \Psr\Http\Message\ResponseInterface|CreateImageInfo[]|CreateImageStream | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 54 | 3 | View Code Duplication |     public function create($parameters = [], $fetch = self::FETCH_OBJECT) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |         $response = parent::create($parameters, self::FETCH_RESPONSE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 3 |  |         if (200 === $response->getStatusCode()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |             if (self::FETCH_STREAM === $fetch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |                 return new CreateImageStream($response->getBody(), $this->serializer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 2 |  |             if (self::FETCH_OBJECT === $fetch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |                 $createImageInfoList = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |                 $stream = new CreateImageStream($response->getBody(), $this->serializer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 $stream->onFrame(function (CreateImageInfo $createImageInfo) use (&$createImageInfoList) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 2 |  |                     $createImageInfoList[] = $createImageInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 2 |  |                 }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 2 |  |                 $stream->wait(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |                 return $createImageInfoList; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return \Psr\Http\Message\ResponseInterface|PushImageInfo[]|CreateImageStream | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 84 | 2 |  |     public function push($name, $parameters = [], $fetch = self::FETCH_OBJECT) | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 86 | 2 |  |         if (isset($parameters['X-Registry-Auth']) && $parameters['X-Registry-Auth'] instanceof AuthConfig) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 87 | 2 |  |             $parameters['X-Registry-Auth'] = base64_encode($this->serializer->serialize($parameters['X-Registry-Auth'], 'json')); | 
            
                                                        
            
                                    
            
            
                | 88 | 2 |  |         } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 90 | 2 |  |         $queryParam = new QueryParam(); | 
            
                                                        
            
                                    
            
            
                | 91 | 2 |  |         $queryParam->setDefault('tag', null); | 
            
                                                        
            
                                    
            
            
                | 92 | 2 |  |         $queryParam->setDefault('X-Registry-Auth', null); | 
            
                                                        
            
                                    
            
            
                | 93 | 2 |  |         $queryParam->setHeaderParameters(['X-Registry-Auth']); | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 95 | 2 |  |         $url      = 'http://localhost/images/{name}/push'; | 
            
                                                        
            
                                    
            
            
                | 96 | 2 |  |         $url      = str_replace('{name}', $name, $url); | 
            
                                                        
            
                                    
            
            
                | 97 | 2 |  |         $url      = $url . ('?' . $queryParam->buildQueryString($parameters)); | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 99 | 2 |  |         $headers  = array_merge(['Host' => 'localhost'], $queryParam->buildHeaders($parameters)); | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 101 | 2 |  |         $body     = $queryParam->buildFormDataString($parameters); | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 103 | 2 |  |         $request  = $this->messageFactory->createRequest('POST', $url, $headers, $body); | 
            
                                                        
            
                                    
            
            
                | 104 | 2 |  |         $response = $this->httpClient->sendRequest($request); | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 106 | 2 |  |         if (200 === $response->getStatusCode()) { | 
            
                                                        
            
                                    
            
            
                | 107 | 2 |  |             if (self::FETCH_STREAM === $fetch) { | 
            
                                                        
            
                                    
            
            
                | 108 | 1 |  |                 return new PushStream($response->getBody(), $this->serializer); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 111 | 1 |  |             if (self::FETCH_OBJECT === $fetch) { | 
            
                                                        
            
                                    
            
            
                | 112 | 1 |  |                 $pushImageInfoList = []; | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 114 | 1 |  |                 $stream = new PushStream($response->getBody(), $this->serializer); | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |                 $stream->onFrame(function (PushImageInfo $pushImageInfo) use (&$pushImageInfoList) { | 
            
                                                        
            
                                    
            
            
                | 116 | 1 |  |                     $pushImageInfoList[] = $pushImageInfo; | 
            
                                                        
            
                                    
            
            
                | 117 | 1 |  |                 }); | 
            
                                                        
            
                                    
            
            
                | 118 | 1 |  |                 $stream->wait(); | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 120 | 1 |  |                 return $pushImageInfoList; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |         return $response; | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |  | 
            
                        
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.