| 1 |  |  | <?php declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace ekinhbayar\GitAmp\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Amp\Promise; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Amp\Artax\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Amp\Artax\ClientException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Amp\Artax\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Amp\Success; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use ekinhbayar\GitAmp\Events\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use ekinhbayar\GitAmp\Events\GithubEventType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use ekinhbayar\GitAmp\Github\Credentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use ekinhbayar\GitAmp\Response\Results; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Class GitAmp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package ekinhbayar\GitAmp\Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class GitAmp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     const API_ENDPOINT = 'https://api.github.com/events'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $credentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $eventFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * GitAmp constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param Client $client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 4 |  |     public function __construct(Client $client, Credentials $credentials, Factory $eventFactory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 4 |  |         $this->client       = $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 4 |  |         $this->credentials  = $credentials; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 4 |  |         $this->eventFactory = $eventFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return Promise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @throws RequestFailedException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 | 4 |  |     private function request(): Promise | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 4 |  |             $request = (new Request) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 4 |  |                 ->setMethod('GET') | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 4 |  |                 ->setUri(self::API_ENDPOINT) | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 4 |  |                 ->setAllHeaders($this->getAuthHeader()); | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 4 |  |             $promise = $this->client->request($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $promise->when(function($error, $result) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 3 |  |                 if($result->getStatus() !== 200) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 1 |  |                     throw new RequestFailedException( | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                         'A non-200 response status (' | 
            
                                                                        
                            
            
                                    
            
            
                | 59 | 1 |  |                         . $result->getStatus() . ' - ' | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 1 |  |                         . $result->getReason() .') was encountered' | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 | 3 |  |             }); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 2 |  |             return $promise; | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 2 |  |         } catch (ClientException $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |             throw new RequestFailedException('Failed to send GET request to API endpoint', $e->getCode(), $e); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function listen(): Promise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  |         return \Amp\resolve(function() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  |             $response = yield $this->request(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |             $results = new Results($this->eventFactory); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |             $results->appendResponse($response); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |             return new Success($results); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 4 |  |     private function getAuthHeader(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 4 |  |         return ['Authorization' => sprintf('Basic %s', $this->credentials->getAuthenticationString())]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |