| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace WyriHaximus\Travis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use React\EventLoop\LoopInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use React\Promise\PromiseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Rx\Observable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Rx\ObservableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Rx\React\Promise; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use WyriHaximus\ApiClient\Transport\Client as Transport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use WyriHaximus\ApiClient\Transport\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function React\Promise\resolve; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class AsyncClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $transport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __construct(LoopInterface $loop, string $token = '', Transport $transport = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 21 |  | View Code Duplication |         if (!($transport instanceof Transport)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             $options = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |                 'resource_namespace' => 'Async', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             ] + ApiSettings::TRANSPORT_OPTIONS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             if ($token !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 $options['headers']['Authorization'] = 'token ' . $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $transport = Factory::create($loop, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->transport = $transport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function repository(string $repository): PromiseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         return $this->transport->request('repos/' . $repository)->then(function ($json) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             return resolve($this->transport->getHydrator()->hydrate('Repository', $json['repo'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function user(): PromiseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         return $this->transport->request('users')->then(function ($json) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             return resolve($this->transport->getHydrator()->hydrate('User', $json['user'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function sshKey(int $id): PromiseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $this->transport->request('settings/ssh_key/' . $id)->then(function ($json) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return resolve($this->transport->getHydrator()->hydrate('SSHKey', $json['ssh_key'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 56 |  | View Code Duplication |     public function hooks(): ObservableInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         return Promise::toObservable( | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             $this->transport->request('hooks') | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         )->flatMap(function ($response) { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |             return Observable::fromArray($response['hooks']); | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         })->map(function ($hook) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             return $this->transport->getHydrator()->hydrate('Hook', $hook); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         }); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 67 |  | View Code Duplication |     public function accounts(): ObservableInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return Promise::toObservable( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->transport->request('accounts') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         )->flatMap(function ($response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return Observable::fromArray($response['accounts']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         })->map(function ($account) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             return $this->transport->getHydrator()->hydrate('Account', $account); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 78 |  | View Code Duplication |     public function broadcasts(): ObservableInterface | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return Promise::toObservable( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->transport->request('broadcasts') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         )->flatMap(function ($response) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             return Observable::fromArray($response['broadcasts']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         })->map(function ($broadcast) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             return $this->getTransport()->getHydrator()->hydrate('Broadcast', $broadcast); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |  | 
            
                        
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.