| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace FlexyProject\GitHub\Receiver\Organizations; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use FlexyProject\GitHub\AbstractApi; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Hooks API class allow you to receive HTTP POST payloads whenever certain events happen within the organization. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @package FlexyProject\GitHub\Receiver\Organizations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Hooks extends AbstractOrganizations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * List hooks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @link https://developer.github.com/v3/orgs/hooks/#list-hooks | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @param string $org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function listHooks(string $org): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks', $org)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * Get single hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @link https://developer.github.com/v3/orgs/hooks/#get-single-hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param string $org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param int    $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws \Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     public function getSingleHook(string $org, int $id): array | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks/:id', $org, (string)$id)); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * Create a hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @link https://developer.github.com/v3/orgs/hooks/#create-a-hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param string       $org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string       $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param string|array $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param array        $events | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param bool         $active | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function createHook(string $org, string $name, $config, array $events = [AbstractApi::EVENTS_PUSH], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                                bool $active = false): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks', $org), Request::METHOD_POST, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 'name'   => $name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 'config' => $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 'events' => $events, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 'active' => $active | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Edit a hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @link https://developer.github.com/v3/orgs/hooks/#edit-a-hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param string       $org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param int          $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @param string|array $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param array        $events | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param bool         $active | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function editHook(string $org, int $id, $config, array $events = [AbstractApi::EVENTS_PUSH], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                              bool $active = false): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks/:id', $org, (string)$id), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             Request::METHOD_PATCH, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 'config' => $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 'events' => $events, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 'active' => $active | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * Ping a hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @link https://developer.github.com/v3/orgs/hooks/#ping-a-hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param int    $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 107 |  | View Code Duplication |     public function pingHook(string $org, int $id): bool | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks/:id/pings', $org, (string)$id), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             Request::METHOD_POST); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Delete a hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @link https://developer.github.com/v3/orgs/hooks/#delete-a-hook | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param string $org | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param int    $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 130 |  | View Code Duplication |     public function deleteHook(string $org, int $id): bool | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $this->getApi()->request($this->getApi()->sprintf('/orgs/:org/hooks/:id', $org, (string)$id), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             Request::METHOD_DELETE); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if ($this->getApi()->getHeaders()['Status'] == '204 No Content') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         return false; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 141 |  |  | } | 
            
                        
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.