| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Acquia\LiftClient\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Acquia\LiftClient\Entity\Entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Acquia\LiftClient\Entity\Goal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Acquia\LiftClient\Entity\GoalAddResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Acquia\LiftClient\Exception\LiftSdkException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use GuzzleHttp\Psr7\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class GoalManager extends ManagerBase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $queryParameters = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         'global' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         'limit_by_site' => null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array Valid boolean query strings. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $validBooleanQueryStrings = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         'true', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         'false', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Get a list of Goals. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Example of how to structure the $options parameter: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * <code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * $options = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      *     'global'  => 'false', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      *     'limit_by_site'  => 'true', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 10 |  |      * ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * </code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 6 |  |      * Note: the options "global" and "limit_by_site" work together to determine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 6 |  |      * what goals are coming back. Please see the truth table on following page. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @see http://docs.decision-api.acquia.com/#goals_get | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 6 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 6 |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @throws \GuzzleHttp\Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 3 |  |      * @return \Acquia\LiftClient\Entity\Goal[] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 | 3 |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 50 | 2 | View Code Duplication |     public function query($options = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 3 |  |         $url = '/goals'; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 8 |  |         $url .= $this->getQueryString($options); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         // Now make the request. | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $request = new Request('GET', $url); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $data = $this->getResponseJson($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         // Get them as Goal objects | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $goals = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         foreach ($data as $dataItem) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             $goals[] = new Goal($dataItem); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         return $goals; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 6 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * Get a specific goal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 6 |  |      * @see http://docs.decision-api.acquia.com/#goals__goal_id__get | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 6 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param array $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @throws \GuzzleHttp\Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @return \Acquia\LiftClient\Entity\Goal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function get($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $url = "/goals/{$id}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // Now make the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $request = new Request('GET', $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $data = $this->getResponseJson($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return new Goal($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * Add a goal. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 9 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 9 |  |      * @see http://docs.decision-api.acquia.com/#goals_post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 9 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 9 |  |      * @param \Acquia\LiftClient\Entity\Goal $goal | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 9 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @throws \GuzzleHttp\Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 6 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return \Acquia\LiftClient\Entity\GoalAddResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function add(Goal $goal) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         // goals only supports adding a list of goals | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         // we do not want to support that in the SDK for consistency reasons, so | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         // we convert it to an array here. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $goals = new Entity([$goal->getArrayCopy()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $body = $goals->json(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $url = '/goals'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $request = new Request('POST', $url, [], $body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $data = $this->getResponseJson($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 6 |  |         return new GoalAddResponse($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 6 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 6 |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * Deletes a goal by ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 3 |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @see http://docs.decision-api.acquia.com/#goals__goal_id__delete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @throws \GuzzleHttp\Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     public function delete($id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $url = "/goals/{$id}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $this->client->delete($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * Get query string of using the options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @param $options The options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @throws \Acquia\LiftClient\Exception\LiftSdkException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @return string  The query string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     protected function getQueryString($options) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 144 |  | View Code Duplication |         if (isset($options['global']) && !in_array($options['global'], $this->validBooleanQueryStrings)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             throw new LiftSdkException('The "global" parameter must be a string value of "true" or "false", or absent.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 147 |  | View Code Duplication |         if (isset($options['limit_by_site']) && !in_array($options['limit_by_site'], $this->validBooleanQueryStrings)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             throw new LiftSdkException('The "limit_by_site" parameter must be a string value of "true" or "false", or absent.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         return parent::getQueryString($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 153 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 154 |  |  |  | 
            
                        
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.