| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Bitbucket\API\Teams; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Bitbucket\API\Api; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Hooks extends Api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * @param string $team The team's name or uuid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @param array $hookConfiguration The hook configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function create($team, array $hookConfiguration) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $mandatory = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             'url' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'active' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'events' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $diff = array_diff(array_keys($mandatory), array_keys($hookConfiguration)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if (count($diff) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             throw new \InvalidArgumentException('Missing parameters for creating a new webhook.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         if (false === array_key_exists('events', $hookConfiguration) || 0 === count($hookConfiguration['events'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             throw new \InvalidArgumentException('Missing events for creating a new webhook.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         return $this->getClient()->setApiVersion('2.0')->post( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             sprintf('/teams/%s/hooks', $team), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             json_encode($hookConfiguration), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             ['Content-Type' => 'application/json'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param string $team The team's name or uuid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param string $uuid The universally unique identifier of the webhook. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param array $hookConfiguration The hook configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function update($team, $uuid, array $hookConfiguration) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $mandatory = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             'url' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             'active' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'events' => [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $diff = array_diff(array_keys($mandatory), array_keys($hookConfiguration)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if (count($diff) > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             throw new \InvalidArgumentException('Missing parameters for updating a webhook.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (false === array_key_exists('events', $hookConfiguration) || 0 === count($hookConfiguration['events'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             throw new \InvalidArgumentException('Missing events for updating a new webhook.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $this->getClient()->setApiVersion('2.0')->put( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             sprintf('/teams/%s/hooks/%s', $team, $uuid), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             json_encode($hookConfiguration), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ['Content-Type' => 'application/json'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $team The team's name or uuid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function all($team) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $this->getClient()->setApiVersion('2.0')->get( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             sprintf('/teams/%s/hooks', $team) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @param string $team The team's name or uuid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param string $uuid The universally unique identifier of the webhook. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     public function get($team, $uuid) | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         return $this->getClient()->setApiVersion('2.0')->get( | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             sprintf('/teams/%s/hooks/%s', $team, $uuid) | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param string $team The team's name or uuid. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param string $uuid The universally unique identifier of the webhook. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function delete($team, $uuid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         return $this->getClient()->setApiVersion('2.0')->delete( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             sprintf('/teams/%s/hooks/%s', $team, $uuid) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  |