| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Acquia\LiftClient\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\ClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Http\Message\RequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | abstract class ManagerBase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @var array A list of query parameters that the URL could possibly have | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $queryParameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 96 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var \GuzzleHttp\ClientInterface The request client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 96 |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 96 |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param \GuzzleHttp\ClientInterface $client The request client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function __construct(ClientInterface $client) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->client = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Make the given Request and return as JSON Decoded PHP object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param \Psr\Http\Message\RequestInterface $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return mixed the value encoded in <i>json</i> in appropriate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      *               PHP type. Values true, false and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      *               null (case-insensitive) are returned as <b>TRUE</b>, <b>FALSE</b> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      *               and <b>NULL</b> respectively. <b>NULL</b> is returned if the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      *               <i>json</i> cannot be decoded or if the encoded | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      *               data is deeper than the recursion limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     protected function getResponseJson(RequestInterface $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $response = $this->client->send($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $body = (string) $response->getBody(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return json_decode($body, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * Get query string of using the options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param $options The options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @return string  The query string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |     protected function getQueryString($options) { | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |       $queries = []; | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |       foreach ($this->queryParameters as $queryName => $queryDefaultValue) { | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |         // Use user value if possible. | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 58 |  | View Code Duplication |         if (isset($options[$queryName])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |           $queries[] = $queryName . '=' . $options[$queryName]; | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |           continue; | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |         // Use default value if possible. | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 63 |  | View Code Duplication |         if (!isset($options[$queryName]) && is_string($queryDefaultValue)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |           $queries[] = $queryName . '=' . $queryDefaultValue; | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |           continue; | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |       } | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |       $queryString = implode('&', $queries); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |       return empty($queryString) ? '' : '?' . $queryString; | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  | 
            
                        
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.