| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Chapi\Component\RemoteClients; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Chapi\Component\Http\HttpClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Chapi\Entity\Chronos\ChronosJobEntity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Chapi\Entity\JobEntityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Chapi\Exception\HttpConnectionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Config\Definition\Exception\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class MarathonApiClient implements ApiClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** @var HttpClientInterface  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $oHttpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 9 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         HttpClientInterface $oHttpClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 9 |  |         $this->oHttpClient = $oHttpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @link: https://mesos.github.io/chronos/docs/api.html#listing-jobs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 1 |  |     public function listingJobs() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 1 |  |         $oResponse = $this->oHttpClient->get('/v2/apps'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |         if (200 == $oResponse->getStatusCode()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |             return $oResponse->json(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param JobEntityInterface $oJobEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 44 | 1 | View Code Duplication |     public function addingJob(JobEntityInterface $oJobEntity) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |         $_sTargetEndpoint = '/v2/apps'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $_oResponse = $this->oHttpClient->postJsonData($_sTargetEndpoint, $oJobEntity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         return ($_oResponse->getStatusCode() == 201); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param JobEntityInterface|ChronosJobEntity $oJobEntity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 56 | 1 | View Code Duplication |     public function updatingJob(JobEntityInterface $oJobEntity) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         $_sTargetEndpoint = '/v2/apps'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         $_oResponse = $this->oHttpClient->putJsonData($_sTargetEndpoint, $oJobEntity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         return ($_oResponse->getStatusCode() == 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param string $sJobName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |     public function removeJob($sJobName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         $_sTargetEndpoint = '/v2/apps/'.$sJobName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |         $_oResponse = $this->oHttpClient->delete($_sTargetEndpoint); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 1 |  |         return ($_oResponse->getStatusCode() == 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $sJobName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     public function getJobStats($sJobName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Returns true if the client can be connected to. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 89 | 5 | View Code Duplication |     public function ping() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         try { | 
            
                                                        
            
                                    
            
            
                | 92 | 5 |  |             $this->oHttpClient->get('/v2/info'); | 
            
                                                        
            
                                    
            
            
                | 93 | 4 |  |         } catch (HttpConnectionException $e) | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |         { | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |             if ( | 
            
                                                        
            
                                    
            
            
                | 96 | 4 |  |                 $e->getCode() == HttpConnectionException::ERROR_CODE_REQUEST_EXCEPTION || | 
            
                                                        
            
                                    
            
            
                | 97 | 4 |  |                 $e->getCode() == HttpConnectionException::ERROR_CODE_CONNECT_EXCEPTION | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |             ) | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |             { | 
            
                                                        
            
                                    
            
            
                | 100 | 2 |  |                 return false; | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 103 | 3 |  |         return true; | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 105 |  |  | } | 
            
                        
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.