| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace WyriHaximus\Travis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use React\EventLoop\Factory as LoopFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Rx\React\Promise; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use WyriHaximus\Travis\Resource\RepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use WyriHaximus\Travis\Resource\SSHKeyInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use WyriHaximus\Travis\Resource\Sync\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use WyriHaximus\ApiClient\Transport\Client as Transport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use WyriHaximus\ApiClient\Transport\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use function Clue\React\Block\await; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function React\Promise\resolve; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use WyriHaximus\Travis\Resource\UserInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $transport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function __construct(string $token = '', Transport $transport = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $loop = LoopFactory::create(); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 25 |  | View Code Duplication |         if (!($transport instanceof Transport)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             $options = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                     'resource_namespace' => 'Async', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 ] + ApiSettings::TRANSPORT_OPTIONS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             if ($token !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |                 $options['headers']['Authorization'] = 'token ' . $token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $transport = Factory::create($loop, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->transport = $transport; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->client = new AsyncClient($loop, $token, $this->transport); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function repository(string $repository): RepositoryInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return await( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $this->client->repository($repository), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $this->transport->getLoop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function user(): UserInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return await( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             $this->client->user(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $this->transport->getLoop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function sshKey(int $id): SSHKeyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return await( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $this->client->sshKey($id), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->transport->getLoop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function hooks(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         return await( | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             Promise::fromObservable( | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |                 $this->client->hooks()->toArray() | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |             $this->transport->getLoop() | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function accounts(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return await( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             Promise::fromObservable( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $this->client->accounts()->toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->transport->getLoop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function broadcasts(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return await( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             Promise::fromObservable( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 $this->client->broadcasts()->toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $this->transport->getLoop() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  | 
            
                        
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.