| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Acquia\LiftClient\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Acquia\LiftClient\Entity\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use GuzzleHttp\Psr7\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class RuleManager extends ManagerBase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * Get a list of Rules. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * Example of how to structure the $options parameter: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * <code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * $options = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      *     'visible_on_page'  => 'node/1/*', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      *     'prefetch'  => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      *     'sort'  => 'asc', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      *     'start'  => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      *     'rows'  => 0, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      *     'sort_field'  => 'updated', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      *     'status'  => 'published' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * </code> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @see http://docs.decision-api.acquia.com/#rules_get | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @throws \GuzzleHttp\Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return \Acquia\LiftClient\Entity\Rule[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function query($options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $url = '/rules'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $url .= isset($options['visible_on_page']) ? "&visible_on_page={$options['visible_on_page']}" : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $url .= isset($options['prefetch']) ? "&prefetch={$options['prefetch']}" : true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $url .= isset($options['sort']) ? "&sort={$options['sort']}" : 'asc'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $url .= isset($options['start']) ? "&start={$options['start']}" : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $url .= isset($options['rows']) ? "&rows={$options['rows']}" : 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $url .= isset($options['sort_field']) ? "&sort_field={$options['sort_field']}" : 'updated'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $url .= isset($options['status']) ? "&status={$options['status']}" : 'published'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         // Now make the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $request = new Request('GET', $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $data = $this->client->getResponseJson($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         // Get them as Rule objects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $rules = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         foreach ($data as $dataItem) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $rules[] = new Rule($dataItem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return $rules; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * Get a specific rule. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @see http://docs.decision-api.acquia.com/#rules__ruleId__get | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param array $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @throws \GuzzleHttp\Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return \Acquia\LiftClient\Entity\Rule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 69 |  | View Code Duplication |     public function get( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |       $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $url = "/rules/{$id}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         // Now make the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $request = new Request('GET', $url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $data = $this->client->getResponseJson($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return new Rule($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Add a rule. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @see http://docs.decision-api.acquia.com/#rules_post | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param \Acquia\LiftClient\Entity\Rule $rule | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @throws \GuzzleHttp\Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return \Acquia\LiftClient\Entity\Rule | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 92 |  | View Code Duplication |     public function add( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |       Rule $rule | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |         $body = $rule->json(); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         $url = '/rules'; | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         $request = new Request('POST', $url, [], $body); | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         $data = $this->client->getResponseJson($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         return new Rule($data); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * Deletes a rule by ID. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @see http://docs.decision-api.acquia.com/#rules__ruleId__delete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @throws \GuzzleHttp\Exception\RequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     public function delete( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |       $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $url = "/rules/{$id}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $this->client->delete($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 122 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |  | 
            
                        
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.